; Writes text starting from current cursor position
; @param BC Pointer to a null-terminated string first character
Lcd_print:
lda,(bc); bc is the pointer to passed string's first char
cp0; compare A content with 0 (subtract 0 from value and set zero flag Z if result is 0)
retz; if prev compare is true (Z flag set), string is finished, return
out(LCD_DATA_REG),a; output char
incbc; increment bc to move to next char
; increment x position
lda,(lcd_cur_x)
inca
ld(lcd_cur_x),a
; if x position > 19, increment y position and set x to 0
lda,(lcd_cur_x)
cp20
jpnz,Lcd_print; if x position is not 20, continue cycle
lda,0
ld(lcd_cur_x),a; else set x to 0
; and increment y
lda,(lcd_cur_y)
inca
ld(lcd_cur_y),a
; if y > 3 set y to 0
cp4; a still contains lcd_cur_y: compare with 4
jpnz,Lcd_print; if y position is not 4, continue cycle
lda,0
ld(lcd_cur_x),a; else set y pos to 0
jpLcd_print
ret
; Writes a single character at current cursror position
; @param A Value of character to print
Lcd_printc:
out(LCD_DATA_REG),a
ret
; Set cursor position
; @param B X-axis position (0 to 19)
; @param C Y-axis position (0 to 3)
Lcd_locate:
lda,b
ld(lcd_cur_x),a
lda,c
ld(lcd_cur_y),a
calllcd_locate
ret
; private
; The cursor position can seem like black magic but it makes much more sense once you know that the HD44780 is designed to control a 40 character 4-line display. So if you have a 16×2 then you will only see the first 16 characters of the top two lines. Simple enough once you get used to taking these character positions into account. For example, in a 16×2 display, the first line is position 0-15. So 0x80 is the first position, 0x80 + 12 = 0x8C is the 13th (remember, they are zero indexed). The second line is a little tricky since it shows positions 64-79. Just add the position number (in decimal) to 0x80 (in hex) to get the hex address of the cursor position. The address is the command to move the cursor to that location. So, to move to the 13th position of the top line in a 16×2 Sparkfun Display, I would send “0xFE 0x8C”. The first byte warns the onboard microcontroller that a command is coming, and the second byte is the command.
lcd_locate:
; warns the lcd microcontroller that a command is coming
lda,0xFE
out(LCD_INSTR_REG),a
; get line start command code from array
ldhl,LCD_LINES_LEFT; load array pointer
ldbc,lcd_cur_y; load line number
ldb,0; since line number is only 8 bit, clean garbage on the upper bit
addhl,bc; sum first array element to line number to access correct array element. Now hl contains array pointer
; now sum x offset to the start line code to obtain lcd controller complete position code
lda,(lcd_cur_x); load cursor x position
adda,(hl); sum cursor x pos to line start code. Result is in a
out(LCD_INSTR_REG),a; send cursor position to lcd controller