#define b_LCD_CS1 PORTA,2
#define b_LCD_CS2 PORTA,3
#define b_LCD_RS PORTC,5
#define b_LCD_RW PORTC,0
#define b_LCD_E PORTC,2
InitLCD bsf b_LCD_CS1
bsf b_LCD_CS2
movlw 0x3f
call WriteCMD
movlw 0xc0
call WriteCMD
movlw 0x40
call WriteCMD
movlw 0xb8
call WriteCMD
return
WaitNotBusy bsf STATUS,RP0
movlw 0xff
movwf TRISB
bcf STATUS,RP0
bsf b_LCD_RW
bcf b_LCD_RS
btfss b_LCD_CS1
goto Skip_CS1
bcf b_CS2
btfsc b_LCD_CS2
bsf b_CS2
bcf b_LCD_CS2
WNB1_Loop bsf b_LCD_E
btfsc PORTB,7
goto WNB1_Loop
bcf b_LCD_E
btfsc b_CS2
bsf b_LCD_CS2
Skip_CS1 btfss b_LCD_CS2
goto Skip_CS2
bcf b_CS1
btfsc b_LCD_CS1
bsf b_CS1
bcf b_LCD_CS1
WNB2_Loop bsf b_LCD_E
btfsc PORTB,7
goto WNB2_Loop
bcf b_LCD_E
btfsc b_CS1
bsf b_LCD_CS1
Skip_CS2
SetOutput bsf STATUS,RP0
clrf TRISB
bcf STATUS,RP0
return
ReadLCD bsf b_LCD_E
movfw PORTB
bcf b_LCD_E
btfsc b_Inverted
xorlw 0xff
return
WriteCMD movwf LCDTemp
call WaitNotBusy
movfw LCDTemp
movwf PORTB
bcf b_LCD_RW
bcf b_LCD_RS
bsf b_LCD_E
bcf b_LCD_E
return
WriteData movwf LCDTemp
call WaitNotBusy
movfw LCDTemp
btfsc b_Inverted
xorlw 0xff
movwf PORTB
bcf b_LCD_RW
bsf b_LCD_RS ;rs=1 rw=0
bsf b_LCD_E
bcf b_LCD_E
return
ReadData call WaitNotBusy
bsf STATUS,RP0
movlw 0xff
movwf TRISB
bcf STATUS,RP0
bsf b_LCD_RW
bsf b_LCD_RS
bsf b_LCD_E
movfw PORTB
bcf b_LCD_E
btfsc b_Inverted
xorlw 0xff
goto SetOutput
SetLCDAddress bcf b_LCD_CS1
bcf b_LCD_CS2
btfss XPos,6
bsf b_LCD_CS1
btfsc XPos,6
bsf b_LCD_CS2
movfw XPos
andlw 0x3f
iorlw 0x40
call WriteCMD
movfw YPos
movwf Row
rrf Row,F
rrf Row,F
rrf Row,F
movfw Row
andlw 0x7
iorlw 0xb8
goto WriteCMD