Pax Writer
New Member
Hi folks
Here's a pretty ungrateful question, which I hope some of you can help me out with:
I have a 89C4051 processor controlling an LCD-display (common HD44780 type) in the 4-bit mode (see attached picture).
Now, things seem to work out fine most of the time, but occasionally, the display is initiated in a bad way, and I get only garbled text on it. If I then hit the reset-button, it usually works fine.
I suspect there may be two sources of faults:
1. The length and type of wiring, which is ribbon cable of around 40-50cm.
or
2. The code, which I wrote my own interpretation of from the guidelines in the HD44780 data sheet.
Here's the code (its a subroutine written in assembler using Keil):
NOTE: Code is in capitals, comments in italics.
-------------------------------------------------------------
LCDINIT:
;Display init in 4-bit mode
MOV A,#15d
CALL WAIT_M ;Wait routine providing apprx. 15mS of waiting time, so that VCC may reach stable level, if this subroutine is the first in program to be executed.
CALL IN4 ;IN4 is an unused transciever-circuit address, which I use as "parking area" when I need to be sure that no unintentional port activity is influencing other relevant circuits.
CLR RS ;Register Select clearing to allow instruction to lcd.
CLR RW ;Displayet is put in write mode.
MOV A,#15d ;15mS wait-routine is set up...
CALL WAIT_M ;...And then executed.
MOV P1,#110000b ;Port 1 is set up as instructed in data sheet.
CALL LCDTOGGLE_L ;Toggle using lang wait time of apprx. 4mS.
CALL LCDTOGGLE ;Followed by two short toggles of apprx. 100uS.
CALL LCDTOGGLE
MOV P1,#100000b ;P1 is set up to instruct lcd to go in 4-bit mode.
CALL LCDTOGGLE ;lcd toggled to write instruction to lcd.
;Function set - 4-bit, 2-line, 5x8-font below:
MOV P1,#100000b ;Toggle of HI and LO nibble...
CALL LCDTOGGLE
MOV P1,#10000000b
CALL LCDTOGGLE
;Display set up below:
MOV P1,#00b
CALL LCDTOGGLE
MOV P1,#11000000b
CALL LCDTOGGLE
CALL LCDCLEAR
RET
----------------------------------------------------------------
For the sake of clarity I might add that WAIT_M is a subroutine for allowing waiting for a number of milliseconds as indicated by the value in the accumulator prior to calling the routine. The same goes for WAIT_U below.
I have also included LCDTOGGLE, LCDTOGGLE_L and LCDCLEAR subroutines below:
--------------------------------------------------------------------
LCDTOGGLE:
CALL LCD ;LCD enable
MOV ACC,#100d
CALL WAIT_U
CALL IN4 ;IN4 is used as "No Select"-address in this application
RET
--------------------------------------------------------------------
--------------------------------------------------------------------
LCDTOGGLE_L:
CALL LCD ;LCD enable
MOV ACC,#4d
CALL WAIT_M
CALL IN4
RET
--------------------------------------------------------------------
--------------------------------------------------------------------
LCDCLEAR: ;Deletes all display text and returns cursor home.
CLR RS ;As instructed in datasheet.
CLR RW ;As instructed in datasheet.
MOV P1,#00b ;0 is written to HI nibble
CALL LCDTOGGLE ;And then toggled.
MOV P1,#00010000b ;The required value to LO nibble is written.
CALL LCDTOGGLE ;And then toggled.
RET
--------------------------------------------------------------------
I know this is a rather big question. All comments about the code specifically or just general advice are welcome.
All questions will be answered as best possible.
Here's a pretty ungrateful question, which I hope some of you can help me out with:
I have a 89C4051 processor controlling an LCD-display (common HD44780 type) in the 4-bit mode (see attached picture).
Now, things seem to work out fine most of the time, but occasionally, the display is initiated in a bad way, and I get only garbled text on it. If I then hit the reset-button, it usually works fine.
I suspect there may be two sources of faults:
1. The length and type of wiring, which is ribbon cable of around 40-50cm.
or
2. The code, which I wrote my own interpretation of from the guidelines in the HD44780 data sheet.
Here's the code (its a subroutine written in assembler using Keil):
NOTE: Code is in capitals, comments in italics.
-------------------------------------------------------------
LCDINIT:
;Display init in 4-bit mode
MOV A,#15d
CALL WAIT_M ;Wait routine providing apprx. 15mS of waiting time, so that VCC may reach stable level, if this subroutine is the first in program to be executed.
CALL IN4 ;IN4 is an unused transciever-circuit address, which I use as "parking area" when I need to be sure that no unintentional port activity is influencing other relevant circuits.
CLR RS ;Register Select clearing to allow instruction to lcd.
CLR RW ;Displayet is put in write mode.
MOV A,#15d ;15mS wait-routine is set up...
CALL WAIT_M ;...And then executed.
MOV P1,#110000b ;Port 1 is set up as instructed in data sheet.
CALL LCDTOGGLE_L ;Toggle using lang wait time of apprx. 4mS.
CALL LCDTOGGLE ;Followed by two short toggles of apprx. 100uS.
CALL LCDTOGGLE
MOV P1,#100000b ;P1 is set up to instruct lcd to go in 4-bit mode.
CALL LCDTOGGLE ;lcd toggled to write instruction to lcd.
;Function set - 4-bit, 2-line, 5x8-font below:
MOV P1,#100000b ;Toggle of HI and LO nibble...
CALL LCDTOGGLE
MOV P1,#10000000b
CALL LCDTOGGLE
;Display set up below:
MOV P1,#00b
CALL LCDTOGGLE
MOV P1,#11000000b
CALL LCDTOGGLE
CALL LCDCLEAR
RET
----------------------------------------------------------------
For the sake of clarity I might add that WAIT_M is a subroutine for allowing waiting for a number of milliseconds as indicated by the value in the accumulator prior to calling the routine. The same goes for WAIT_U below.
I have also included LCDTOGGLE, LCDTOGGLE_L and LCDCLEAR subroutines below:
--------------------------------------------------------------------
LCDTOGGLE:
CALL LCD ;LCD enable
MOV ACC,#100d
CALL WAIT_U
CALL IN4 ;IN4 is used as "No Select"-address in this application
RET
--------------------------------------------------------------------
--------------------------------------------------------------------
LCDTOGGLE_L:
CALL LCD ;LCD enable
MOV ACC,#4d
CALL WAIT_M
CALL IN4
RET
--------------------------------------------------------------------
--------------------------------------------------------------------
LCDCLEAR: ;Deletes all display text and returns cursor home.
CLR RS ;As instructed in datasheet.
CLR RW ;As instructed in datasheet.
MOV P1,#00b ;0 is written to HI nibble
CALL LCDTOGGLE ;And then toggled.
MOV P1,#00010000b ;The required value to LO nibble is written.
CALL LCDTOGGLE ;And then toggled.
RET
--------------------------------------------------------------------
I know this is a rather big question. All comments about the code specifically or just general advice are welcome.
All questions will be answered as best possible.