; LCD driver for 8051SBC

BUSY              EQU 80H

; below LCD's registers are mapped into external data memory

command_write     EQU 0000H
data_write        EQU 0001H
command_read      EQU 0002H
data_read         EQU 0003H

                  CSEG

; wait until LCD ready bit set

LcdReady:         PUSH ACC
                  MOV DPTR,#command_read
?ready:           MOVX A,@DPTR
                  JB ACC.7,?ready   ; loop if busy flag = 1
                  POP ACC
                  RET

LCD_command_write: CALL LcdReady
                   MOV DPTR,#command_write
                   MOVX @DPTR,A
                   RET

LCD_data_write: PUSH DPL
                PUSH DPH
                CALL LcdReady
                MOV DPTR,#data_write
                MOVX @DPTR,A
                POP DPH
                POP DPL
                RET


clr_screen:       CALL LcdReady
                  MOV A,#1
                  CALL LCD_command_write
                  RET

InitLcd:          MOV A,#38H
                  CALL LCD_command_write
                  MOV A,#0CH
                  CALL LCD_command_write
                  CALL clr_screen
                  MOV A,#00H       ; A = X
                  MOV B,#00H       ; B = Y
                  CALL goto_xy
                  RET

; goto_xy(x,y)
; entry: A = y position
;        B = x position

goto_xy:          CJNE A,#0,case1
                  MOV A,B
                  ADD A,#80H
                  CALL LCD_command_write
                  RET

case1:            CJNE A,#1,case2
                  MOV A,B
                  ADD A,#0C0H
                  CALL LCD_command_write
                  RET

case2:            RET


; send_string
; entry: DPTR

send_string:    MOVX A,@DPTR
                CJNE A,#0,send_string1
                RET
send_string1:   CALL LCD_data_write
                INC DPTR
                JMP send_string

; write ASCII code to LCD at current position
; entry: A

putch_lcd:      CALL LcdReady
                CALL LCD_data_write
                RET












