;:ts=8 ; ; EXAMPLE PROGRAM #2 ; ; This program has the interface routines to the LCD, being "linit" which ; initializes it, "writelcd" which writes to it, and "puts" which writes ; a sequence of control and data characters. They are demonstrated in ; a program which lets you type on the LCD and exercise its cursor and ; display shift functions. ; ; Internal memory allocation ; 00-07: Register bank 0 -- used for non-interrupt code ; 08-0F: Register bank 1 -- used for interrupt code ; 10-17: Register bank 2 \ ; 18-1F: Register bank 3 > misc. variables go here ; 20-2F: Bit addressable space / ; 30-7F: Generic memory -- used for stack .flag cdflag,0x20.0 .org 0x2800 ; CPU initialization ; ------------------ ; Initialize CPU registers. start: mov SP,#0x2f ; Stack at 30H mov PSW,#0 ; Registers at 0H mov TCON,#0 ; Disable timers mov IE,#0 ; Disable all interrupts mov IP,#0 mov PCON,#0 ; Initialize other registers. mov P1,#0xff mov P3,#0xff ; Start baud rate generator and initialize serial port. mov TH1,#0xfd setb TCON.6 mov SCON,#0x52 ; Initialize the LCD lcall linit ; Print help message mov dptr,#helpmsg lcall puts ; Receive characters, handle special ones, write printable ; ones to the LCD. waitchar: jnb SCON.0,waitchar mov a,SBUF clr SCON.0 clr ACC.7 cjne a,#8,notbs mov dptr,#bsstring lcall lputs sjmp waitchar bsstring: .byte 31,0x10,0 ; Cursor left notbs: cjne a,#13,notcr mov dptr,#crstring lcall lputs sjmp waitchar crstring: .byte 31,0x02,0 ; Cursor & display home notcr: cjne a,#'['-64,notsl mov dptr,#slstring lcall lputs sjmp waitchar slstring: .byte 31,0x1c,0 ; Scroll left notsl: cjne a,#']'-64,notsr mov dptr,#srstring lcall lputs sjmp waitchar srstring: .byte 31,0x18,0 ; Scroll right notsr: cjne a,#1,notca mov dptr,#castring lcall lputs sjmp waitchar castring: .byte 31,0x0d,0 ; Cursor off, blink cursor char notca: cjne a,#2,notcb mov dptr,#cbstring lcall lputs sjmp waitchar cbstring: .byte 31,0x0e,0 ; Cursor on, no blink ; That's it for the nonprintable characters. notcb: mov R0,a anl a,#0xe0 jz waitchar mov a,R0 setb cdflag lcall writelcd sjmp waitchar ; Help message helpmsg: .byte "LCD DEMO",13,10,10 .byte " Cursor left",13,10 .byte " Cursor & display home",13,10 .byte "CTRL-[ Scroll display left",13,10 .byte "CTRL-] Scroll display right",13,10 .byte "CTRL-A Blinking block cursor",13,10 .byte "CTRL-B Underline cursor",13,10,0 ; Subroutine to print a null-terminated string. puts: movx a,@dptr jz pdone lcall putchar inc dptr sjmp puts pdone: ret ; Subroutine to print a single character. putchar: jnb SCON.1,putchar mov SBUF,A clr SCON.1 ret ; Subroutine to initalize the LCD. This simply prints a ; string to the LCD consisting of control register escapes ; and values to write into the control register. linit: mov dptr,#linitstr ljmp lputs linitstr: .byte 31,0x30 ; 8 bit interface, 1 line, 5x7 chars .byte 31,0x0e ; display on, cursor on, no blink .byte 31,0x06 ; autoincrement address, no scroll .byte 31,0x01 ; clear display .byte 31,0x02 ; home display and cursor .byte 0 ; done ; Subroutine to write a string to the LCD. DPTR points to ; the string. Bytes are written to the LCD data register ; until a zero byte is found. Byte value "31" causes the ; next byte to be written to the command register instead. lputs: movx a,@dptr ; Get character jz lpdone ; Exit if terminating zero setb cdflag ; Data register cjne a,#31,notcmd clr cdflag ; Command register inc dptr movx a,@dptr ; Get command byte to write notcmd: lcall writelcd inc dptr sjmp lputs lpdone: ret ; Subroutine to write a command or data byte to the LCD. ; This waits for the LCD to become ready if it is busy. ; ; Inputs: A - character to write. ; cdflag - command (0) or data (1) register writelcd: push DPL push DPH push ACC mov dptr,#0x8000 ; LCD base address clr P1.1 ; Command register setb P1.0 ; Read waitlcd: movx @dptr,a ; Start E pulse movx a,@dptr ; Finish E pulse and get data jb ACC.7,waitlcd ; Loop until LCD is ready mov C,cdflag mov P1.1,C ; Select command/data register clr P1.0 ; Write pop ACC ; Data to write movx @dptr,a ; Start E pulse and write data movx a,@dptr ; Finish E pulse pop DPH pop DPL ret