8051 3 wire lcd
Transcript of 8051 3 wire lcd
-
8/9/2019 8051 3 wire lcd
1/4
3-Wire LCD Interface For 8051 SystemsSubmitted by: Vidyasagaran P. ([email protected])
; ------------- READS51 generated header --------------
; module : C:\Rigel\Reads51\Work\LCD\LCD.asm; created : 20:34:08, Thursday, August 15, 2002; author : Vidyasagaran P. ([email protected]); This file contains all the routines needed to manipulate the LCD display; Please note that these routines are hardware wiring specific and will need; changes if the wiring changes. You may have to adjust the delay (DLY_IDX) depending on; the processor speed.; +-------+ +---------------+; P0.0+-------------------------------------->+E LCD ; +----------+ ; P0.1+-------------->+CLK D5+----------->+RS R/W+---+
; P0.2+-------------->+INPUT D0-4+--------\-->+D4-D7 ; +--\-->+D0-D3 - Gnd; +-------+ +----------+ +---------------+; 8051 74HC164 -Gnd LCD display; How data is loaded into the LCD;; 1. clock the upper nibble in and use E bit to latch the higher 4 bits; 2. clock the lower nibble in similarly; -----------------------------------------------------;; The following definitions are as per the ABOVE hardware wiring.; It assumes 3 port pins used for display functions. First one directly connecte
d to LCD; E (enable) bit and reset two are connected to an 8 bit output shift register as below.
#include
;----------------------- PORTING Changes to be done in the following ---------#define LCD_PORT P3 ;Port used for LCD interface#define BASE 000 ;where the code this located#define DLY_IDX 2FH ;depends on speed of processor
#define EBIT LCD_PORT.0 ;LCD Ebit pin
#define S2P_CLK LCD_PORT.1 ;Serial to parallel convertor 74HC164 clock pin#define S2P_IN LCD_PORT.2 ;Serial to parallel convertor 74HC164 serial input
CodeSeg segment code
Public _Init_LCDPublic _display_string
rseg Codesegorg BASE
_Init_LCD:
clr EBIT ;reset the LCD E bit used. Other pins need not be cleared.
-
8/9/2019 8051 3 wire lcd
2/4
acall init_lcdmov DPTR,#Init_msgacall _display_stringret
;-----------------------------------------------------------;clear_home_cursor - put the cursor back to first char
clear_home_cursor:;clear displayclr cmov acc,#00000001bacall _write_char
;home cursorclr cmov acc,#00000010bacall _write_charret
;-----------------------------------------------------------;_display_string is a function which assumes the address of the string;to be passed in DPTR, in the code memory. Strin must have a NULL at the end._display_string:
mov acc,r1 ;save R1push accmov acc,r0 ;save R0, as this is destroyed in _write_charpush acc
acall clear_home_cursormov r1,#0 ;index count for next character
display_string_loop:mov a,r1 ;move index count to accumulator
movc a,@a+DPTR ;next char to gojz display_string_out ;is it a null?setb c ;if not null set carry to write characall _write_char ;call write_char functioninc r1 ;next char to gosjmp display_string_loop ;repeat till all chars are done
display_string_out:
pop acc ;restore R0 as write_char destroys thismov r0,accpop acc ;restore R1mov r1,accret
;-----------------------------------------------------------;A character is passed in to this routine in accumulator;this routine assumes the following wiring from uP to the LCD.;R0 is destroyed. But exported functions "display_string" saves this,;(a precausion which is optimised also!)_write_char:
mov ov,c ;store state of c for second nibble writemov r0,#5 ;we need to shift these many timessetb ac ;this indicates second nibble needs to be writte
n yet
write_char_loop:mov S2P_IN,c ;serial input bitclr S2P_CLK ;clock once
-
8/9/2019 8051 3 wire lcd
3/4
setb S2P_CLKrlc a ;get c flag set as per data bits from next timedjnz r0,write_char_loop
;make the LCD latch the valuesetb EBIT ;pulse Ebit for one time for the first nibbleclr EBIT
jnb ac,write_char_overclr ac ;get out after the second nibble written
rrc a ;remove that extra rotationmov c,ov ;restore the original carry to write second nibb
lemov r0,#5 ;we need to write these many timessjmp write_char_loop
write_char_over:jb ov,write_char_nodelay ;for command writes delay is needed, not
for chars!acall big_dlywrite_char_nodelay:
ret
;----------------------------------------------------------;write_one_nibble : writes one Most significant nibble in a passed char;to the LCD. Caller should set the C to indicate write to LCD command;Assumes left roation of bits and hence wiring of data bits from Latch;to be accordingly;contents of R0 is destroyedwrite_one_nibble:
mov r0,#5 ;we need to write these many times
write_one_nibble_loop:mov S2P_IN,c ;serial input bitclr S2P_CLK ;clock oncesetb S2P_CLKrlc a ;get c flag set as per data bits from second tim
edjnz r0,write_one_nibble_loop
;make the LCD latch the valuesetb EBIT ;pulse Ebit for one time for the first nibbleclr EBITacall big_dlyret
;----------------------------------------------------------;a big delay for the LCD to settle after each init stuff. Some places this;delay seems to be very critical. I have put more since it doesn't take much;time and also since it is only one time init.big_dly:
mov r0,#DLY_IDXodly:
mov acc,#FFHdly:
djnz acc,dly ;Simulater virtually hangs here. So comment this
during simulationdjnz r0,odlyret
-
8/9/2019 8051 3 wire lcd
4/4
;----------------------------------------------------------;init_lcd : the following routine works fine from the first write after;the power is applied. "write_one_nibble" is used to change the LCD mode;from 8 bit interface to 4 bit one. For this write alone the D0-D3 is taken;as 0000 as they are hardwired to ground.init_lcd:
;set display widthclr cmov acc,#00100000bacall write_one_nibble
;enable display and cursorclr cmov acc,#00001100b ;no cursor and no blinkacall _write_char
;clear displayclr c
mov acc,#00000001bacall _write_char
;home cursorclr cmov acc,#00000010bacall _write_charret
;-----------------------------------------------------------Init_msg:
DB "Display OK",0
end
Back to the 8051 projects section HERE