??? 01/10/07 02:00 Read: times |
#130547 - Asm code Responding to: ???'s previous message |
Why does the assembler code have to be specific for the 2051/4051? Most people interface LCDs through the ports pins, so example code for any of the other 8051 variants are probably just as applicable. I would suggest you learn to read 'c' - its easier to read than assembler! Besides, you should be looking at the method rather than the actual code.
;---------------------------------------------------------------------------- ; ; ; print messages on the lcd display. ; Dptr-> message in rom terminated by 00h ; ; ;---------------------------------------------------------------------------- pstring clr a movc a,@a+dptr jz lcdp_2 lcall lcd_data_wr inc dptr sjmp pstring lcdp_2 ret ;---------------------------------------------------------------------------- ; ; ; Print hex value in A to the lcd display ; ; ;---------------------------------------------------------------------------- phex push a swap a lcall hex pop a hex anl a,#0fh add a,#30h cjne a,#3ah,hex_1 hex_1 jc hex_2 add a,#7 hex_2 lcall lcd_data_wr ret ;---------------------------------------------------------------------------- ; ; ; Print packed bcd value in A to the lcd display ; ; ;---------------------------------------------------------------------------- pbcd push a swap a anl a,#0fh add a,#'0' lcall lcd_data_wr pop a anl a,#0fh add a,#'0' ljmp lcd_data_wr ;---------------------------------------------------------------------------- ; ; ; initialise the lcd display ; ; ;---------------------------------------------------------------------------- lcd_init mov LCD_PORT,#0C0h ; ; delay 150mS ; mov a,#150 lcall delay_1ms mov a,#3 mov LCD_PORT,#0C3h nop nop setb LCD_DEN nop nop clr LCD_DEN ; ; delay 50mS ; mov a,#50 lcall delay_1ms mov LCD_PORT,#0C3h nop nop setb LCD_DEN nop nop clr LCD_DEN ; ; delay 50mS ; mov a,#50 lcall delay_1ms mov LCD_PORT,#0C3h nop nop setb LCD_DEN nop nop clr LCD_DEN ; ; delay 10mS ; mov a,#10 lcall delay_1ms mov LCD_PORT,#0C2h nop nop setb LCD_DEN nop nop clr LCD_DEN ; ; delay 10mS ; mov a,#10 ; 10 * 100uS =1mS lcall delay_1ms mov a,#2ch ; 2 line mode lcall lcd_comm_wr ; write lcd command ; ; delay 1mS ; mov a,#100 ; 10 * 100uS =1mS lcall delay_100u mov a,#0ch ; display on,cursor off lcall lcd_comm_wr ; write lcd command ; ; delay 1mS ; mov a,#10 ; 10 * 100uS =1mS lcall delay_100u mov a,#06h ; auto inc,shift right lcall lcd_comm_wr ; write lcd command mov a,#01h lcall lcd_comm_wr ; clear the display ; ; delay 10mS ; mov a,#1 lcall delay_1ms ret ;---------------------------------------------------------------------------- ; ; Locate the lcd cursor. lcd_comm_wr routine must follow below! ; Cursor position in A ; zaps: A,B ; ; ;---------------------------------------------------------------------------- locate_cursor cjne a,#16,lc_1 lc_1 jc lc_2 ; if location < 16 (line #1) add a,#30h ; else add offset for 2nd line lc_2 setb a.7 ; set bit 7 for locate cursor cmd ;---------------------------------------------------------------------------- ; ; ; send a command to the lcd display. command in A ; Zaps: A,B ; ;---------------------------------------------------------------------------- lcd_comm_wr clr LCD_RS mov b,a ;save the command swap a ;get hi nibble into low nibble anl a,#0fh mov c,a.0 mov P0.0,c mov c,a.1 mov P0.1,c mov c,a.2 mov P0.2,c mov c,a.3 mov P0.3,c nop nop setb LCD_DEN nop nop clr LCD_DEN mov a,b anl a,#0fh mov c,a.0 mov P0.0,c mov c,a.1 mov P0.1,c mov c,a.2 mov P0.2,c mov c,a.3 mov P0.3,c nop nop setb LCD_DEN nop nop clr LCD_DEN ; ; delay for lcd controller ; mov b,#25 ;at least 40uS ldrw_1 djnz b,ldrw_1 ;waste some time ret ;****************************************************************************** ; ; ; send data to the lcd display. command in A ; zaps: A,B ; ; ;****************************************************************************** lcd_data_wr mov b,a ;save the command swap a ;get hi nibble into low nibble anl a,#0fh mov c,a.0 mov P0.0,c mov c,a.1 mov P0.1,c mov c,a.2 mov P0.2,c mov c,a.3 mov P0.3,c setb LCD_RS nop nop setb LCD_DEN nop nop clr LCD_DEN mov a,b anl a,#0fh mov c,a.0 mov P0.0,c mov c,a.1 mov P0.1,c mov c,a.2 mov P0.2,c mov c,a.3 mov P0.3,c setb LCD_RS nop nop setb LCD_DEN nop nop clr LCD_DEN ; ; delay for lcd controller ; mov b,#25 ;at least 40uS ldrw_2 djnz b,ldrw_2 ;waste some time ret ;---------------------------------------------------------------------------- ; ; ; Delay routine. A has # of 100 microseconds to delay ; zaps: A,B ; ; ;---------------------------------------------------------------------------- delay_100u mov b,#54 ;@12.xxx mhz approx! dly_1 djnz b,dly_1 djnz a,delay_100u ret ;---------------------------------------------------------------------------- ; ; ; Delay routine. A has # of 1 milliseconds to delay ; zaps: A,B ; ; ;---------------------------------------------------------------------------- delay_1ms mov b,#120 ;@12.xxx mhz approx! dly_2 nop nop nop nop nop nop djnz b,dly_2 djnz a,delay_1ms ret The lcd was connected to port0 in this instance lcd d4..7 to P0.0..3 lcd EN to P0.5 lcd RS to P0.4 lcd R/W to 0v This uses 4 bit mode - with is advisable due to the number of available port pins on a 2051/4051 Any questions :- refer to the hitachi hd44780 datasheet first. |