LIST P=16F883, R=DEC
#include "P16F883.inc"
;
OSCFREQ = 4		; 1,2,4,8 MHz
;
	__CONFIG	_CONFIG1, _CP_OFF & _DEBUG_OFF & _PWRTE_OFF & _CPD_OFF & _LVP_OFF & _BOR_ON & _PWRTE_ON & _WDT_ON  & _INTRC_OSC_NOCLKOUT & _IESO_OFF & _FCMEN_OFF & _MCLRE_OFF
	__CONFIG	_CONFIG2, _WRT_OFF & _BOR21V
;
cblock 0x20
 temp1,temp2,temp3,temp_portsim
endc
;
trisamask 		EQU b'00001111'
trisbmask		EQU b'11111111'
triscmask 		EQU b'11110000'
;
commandport		EQU PORTA
dataport		EQU PORTC
RS				EQU .7
EN				EQU .6
D0				EQU .0
D1				EQU .1
D2				EQU .2
D3				EQU .3
;
IF OSCFREQ == 1
oscmask			EQU b'01000101'	; 1 MHz
ENDIF
;
IF OSCFREQ == 2
oscmask			EQU b'01010101'	; 2 MHz
ENDIF
;
IF OSCFREQ == 4
oscmask			EQU b'01100101'	; 4 MHz
ENDIF
;
IF OSCFREQ == 8
oscmask			EQU b'01110101'	; 8 MHz
ENDIF
;
org 0x00
	goto settings
org 0x04
	goto interrupt
settings
	banksel TRISA
			movlw trisamask
			movwf TRISA
			movlw trisbmask
			movwf TRISB
			movlw triscmask
			movwf TRISC
			movlw 0xFF
			movwf TRISE
	banksel ANSEL
			clrf ANSEL
	banksel ADCON1
			movlw b'10000000'
			movwf ADCON1
	banksel ADCON0
			clrf ADCON0
	banksel OSCCON
			movlw oscmask
			movwf OSCCON
	banksel PORTA
			clrf PORTA
			clrf PORTB
			clrf PORTC
			clrf PORTE
goto main
;
interrupt
; now it must examine which type interrupt had happened?
banksel INTCON
	btfsc INTCON,2				; is it TMR0 overflow?
	call tmr0over
banksel PIR1
	btfsc PIR1,0				; is it TMR1 overflow?
	call tmr1over
banksel PORTA
;
retfie
;
; *******************************************************************
;
tmr0over
return
;
tmr1over
return




;
init_lcd					; at first LSB after MSB
;		call wait_15msec
movlw b'00000011'			; 4 bit operation MSB-s of first 2 bytes
call lcdcommand
		call wait_5msec
movlw b'00000011'
call lcdcommand
		call wait_100mikrosec
movlw b'00110010'			; 8 bit/4 bit interface
call lcdcommand
;
movlw b'00101000'			; function set
call lcdcommand
movlw b'00001000'			; display off
call lcdcommand
movlw b'00000001'			; clear display
call lcdcommand
movlw b'00000111'			; entry mode
call lcdcommand
movlw b'00000111'			; cursor on, blinking
call lcdcommand
		call wait_15msec
return

wait_15msec		; 4 MHz
movlw .15
movwf temp1
wait15
	movlw .250
	movwf temp2
	decfsz temp2,F
		goto $-1
decfsz temp1,F
goto wait15
nop
return
;
wait_5msec		; 4 MHz
movlw .5
movwf temp1
wait5
	movlw .250
	movwf temp2
	decfsz temp2,F
		goto $-1
decfsz temp1,F
goto wait5
nop
return
;
wait_100mikrosec	; 4 MHz
movlw .2
movwf temp1
wait100
	movlw .25
	movwf temp2
	decfsz temp2,F
		goto $-1
decfsz temp1,F
goto wait100
nop
return
;
wait_40mikrosec
	movlw .20
	movwf temp2
	decfsz temp2,F
		goto $-1
nop
return




portsim
bcf dataport,D0
bcf dataport,D1
bcf dataport,D2
bcf dataport,D3
	clrf temp_portsim
	movwf temp_portsim
		btfsc temp_portsim,4
bsf dataport,D0
		btfsc temp_portsim,5
bsf dataport,D1
		btfsc temp_portsim,6
bsf dataport,D2
		btfsc temp_portsim,7
bsf dataport,D3
clrf temp_portsim
return




lcdcommand
movwf temp3
;		call wait_40mikrosec
movf temp3,0
andlw b'11110000'
		bcf PORTA,7			; RS
call portsim
		bsf PORTA,6			; EN
		bcf PORTA,6
;
;		call wait_40mikrosec
swapf temp3,F
movf temp3,0
andlw b'11110000'
		bcf PORTA,7			; RS
call portsim
		bsf PORTA,6			; EN
		bcf PORTA,6
return

lcddata
movwf temp3
;		call wait_40mikrosec
movf temp3,0
andlw b'11110000'
		bsf PORTA,7			; RS
call portsim
		bsf PORTA,6			; EN
		bcf PORTA,6
;
;		call wait_40mikrosec
swapf temp3,F
movf temp3,0
andlw b'11110000'
		bsf PORTA,7			; RS
call portsim
		bsf PORTA,6			; EN
		bcf PORTA,6
return
















main


movlw b'10110010'
call lcdcommand


movlw b'01101001'
call lcddata







end