LCD Initialisation

rodmcm

Member
I am playing with 2 line displays and an 18X with great success, not my design sadly. The initation code is

rs = LCDInstruct
for Counter = 0 to 5
lookup Counter,($33,$32,$28,$0F,$01,$06),OutputByte
' #33 8 bit 1 line 5 x 7 dots
' $32 ???????????
' $28 4 bits 2 lines 5 x 7 dots
' $0F Display On, Cursor On, Blink On
' $01 Display Clear
' $06 Cursor Move right, no shift in display
gosub WriteToLCD 'actually send the byte
next Counter

For the life of me I cannot understand why I need $33, $32 in front of $28 in the Set Function command. If I remove either or both (and change Counter) i get garbage on the LCD. The same thing happens with 2 line 8 char and 2 line 16 char

Any ideas?
 

westaust55

Moderator
Hi rodmcm,

I take it this is the same agena LCD that I gave you a link to potential datasheets on 27th June at:

http://digital-io.com/product/products.htm

have you worked out exactly which model you have?

I did give a very similar explanation in another thread recently.

Which thread did you get your program code from or have you worked it out this far yourself?

Hard to know exactly what the codes are doing without knowing which datasheet to look at.
From you code it appears that you are controlling the LCD using a parallel connection.
 

rodmcm

Member
Yes, it is the adrana unit (and another unit), I'm trying to learn about LCDs

I have just found Hippy's treatise on this at http://www.hippy.freeserve.co.uk/picaxe2l.htm though it is not where I got my code from, though I suspect that this was the root program.
In the above Hippy uses the same initialisation, my question is why are the first $33, $32 needed. Ive used the Java Simulator as well and it doesn't even register $32. Nor have I found this in the LCD literature.
 

hippy

Ex-Staff (retired)
The $33 then $32 configure a HD44780-based LCD to operate in 4-bit bus mode rather than in 8-bit bus mode. They are Function Set commands which are described as necessary in the HD44780 datasheet, particularly in the details of the Reset Initialisation Sequence, for example Figure 24 in this one ...

http://www.sparkfun.com/datasheets/LCD/HD44780.pdf

No matter that the interface used is 4-bit the LCD doesn't know that and assumes it is 8-bit until convinced otherwise. This is the sequence required to do that.
 

westaust55

Moderator
I have to admit that I am also slightly confused by some of the information available.

Based on my reading of the Hitachi and the Agena LCD manuals (commands are the same)

$33 = %0011 NF11
……..………^^…..^^
.………..……|.|…...|.|
.…………..…|.|…...|..+- don’t care
…..………...|.|…...+---- don’t care
.….………...|.+--------- 1 = 8-bit interface 0 = 4-bit interface
……..………+----------- 1 = Function set command

N=0 = 1 line, 1 = 2line display
F=0 = 5x7 font

From this, the values $33 and $32 seem to do exactly the same thing so clearly something more underlying is occurring.

The value $28 = %0010 1000 is where bit 4 is zero (0) finally sets up the 4-bit interface.

Never having played with LCD's using parallel interface, I would be also be keen to see a bit more explanation about these first two bytes.


EDIT:
I did some searching and even found machine code examples where there were 2 and 3 characters being send at the start.
In one case the value $30 was sent 3 times.
It would be of interest to try changing the $33 and $32 both to $30 and see it is still works. ???
Its almost as if it is some form of delay otherwise why send the same command 2 or 3 times?

EDIT2:
Found a tutorial which indicates that the first 2 or 3 bytes (usually the same value) are in fact a RESET sequence.
The initialisation to 4-bit mode starts with the $28 value.

Till now whatever we discussed in the previous part of ths LCD tutorial, we were dealing with 8-bit mode. Now we are going to learn how to use LCD in 4-bit mode. There are many reasons why sometime we prefer to use LCD in 4-bit mode instead of 8-bit. One basic reason is lesser number of pins are needed to interface LCD.

In 4-bit mode the data is sent in nibbles, first we send the higher nibble and then the lower nibble. To enable the 4-bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4-bit mode of operation. We call this special sequence as resetting the LCD. Following is the reset sequence of LCD.
 Wait for abour 20mS
 Send the first init value (0x30)
 Wait for about 10mS
 Send second init value (0x30)
 Wait for about 1mS
 Send third init value (0x30)
 Wait for 1mS
 Select bus width (0x30 - for 8-bit and 0x20 for 4-bit)
 Wait for 1mS
 
Last edited:

hippy

Ex-Staff (retired)
In one case the value $30 was sent 3 times.
The initialisation to 4-bit mode starts with the $28 value.


That rings a bell. In my code there are two bytes sent as four nibbles, but because the LCD is in 8-bit mode at power-up it sees four bytes received rather than four nibbles. Thus $33, $32 then $28 are received by the LCD as ...

%0011 ????
%0011 ????
%0011 ????
%0010 ????
%0010 1000

It's that final %0010???? ($2x) which sets the 4-bit bus mode.

The subsequent $28 is sent to set two line mode ($x8) and the mode ($2x) ensures the LCD stays in 4-bit mode.

Note that there are meant to be particular delays between the initial commands and my code doesn't work exactly as per spec. It's worked though with all HD44780's I've tried.

Added :

If an LCD has been initialised and the PICAXE is re-set but LCD isn't, the $33 initialisation byte will be sent as two nibbles but treated as a byte by the LCD which will force 8-bit mode, at which point the following $32 nibbles is received as two bytes, first confirming 8-bit mode, the second being, just-kidding it's atually 4-bit. Then the $28 can be sent to undo any configuration change caused by the $3x and $2x ...

%0011 0011
%0011 ????
%0010 ????
%0010 1000
 
Last edited:

westaust55

Moderator
Thanks rodmcm,

At least we do know for future reference that the $30, $30, $28 idea does not work.

Obviously those numbers are required, even to get started from what you are saying even though from the function set command the lower 2 bits are supposedly unused.

Hippy has given an explanation based on starting in 8-bit mode and later resetting the
PICAXE when the LCD is already in 4-bit mode.

Not sure that I still fully understand.
1. why can some people start with just the $28 and it supposedly works
2. some use two or three $30 before the $28 and it supposedly works
3. The code suggests the 2 least significant bits in the function set do nothing
4. Rocmcm and Hippy give examples with $33, $32 and $28 which works
5. If rodmcm removes the first two values or changes them to $30 it does not work properly.

all a bit strange to me at this instant - why is it so . . .

in fact its a bit like a word processor package for the 6502 microprocessors back in the early 1980's. If anyone tried to dis-assemble it after about 10 instructions the code "dissapeared" into incomprehensible bytes. There were rumours that the 6502 had a 16bit mode (and references to sweet16 code). To my knowledge no-one ever did successfully disassemble that code (back then).
 
Last edited:

hippy

Ex-Staff (retired)
why is it so - Because it is :)

That's what the HD44780 manual says is needed. It may be different for other devices or other people may have been lucky. I may even be sending more $3x than needed ( because it does no harm and getting an odd number of nibbles into bytes isn't easy ).

It's like a car manual which says to start a car, "turn ignition to position 1, wait two seconds, depress the gas pedal, release, depress clutch, turn key to position 2, car will start". That's the sequence. Just because others may report success with turning the key straight through 1 into 2 doesn't mean they are doing it right or will it work for all cars.
 

westaust55

Moderator
LCD Initialisation - understood

Took the opportunity to look at the Hitachi HD44780 LCD data sheet and things are much more clear.

As Rodmcm had previously indicated that he had an Agena LCD module I was looking at the Agena datasheets which do not have all the tables given the Hiachi datashseet.

Looking at the Agena LCD data sheet was akin to looking only at Table 6 of the Hitachi HD44780 LCD datasheet and not reading right through to Tables 23 and 24 near the back of the Hitachi datasheet. Looking at the more extensive Hitachi HD44780 LCD data sheet things are far more clear.

The purpose of the first two bytes:

The LCD supply voltage much reach a specified voltage within a given time frame (must reach 4.5V for 5V operation within 10msec maximum) for the LCD to internal reset ready for operation. If this does not occur then reset by instruction is required. This is done by waiting for 40 msec (PAUSE 40) after energisation and then sending the Function Set command twice with at least a 4.1msec delay between each byte being sent (use PAUSE 5).

When the power is turned on, 8-bit operation is automatically selected and the first write is performed as an 8-bit operation. Since with 4-bit operation DB0 to DB3 are not connected, a rewrite is then required. However, since one operation is completed in two accesses for 4-bit operation, a rewrite is needed to set the functions. Thus, DB4 to DB7 of the function set instruction is written twice.

By always having the initialisation by instruction commands at the beginning of the program to use the LCD then the LCD will operate properly even if the supply voltage does not meet the internal reset requirements.

Then switching to 4-bit operation control:

For operate in 4-bit mode,, the program must set all functions after the reset mentioned above prior to the 4-bit operation commencing. The is achieved with the upper 4 bits of the function set byte $28 (that is the “2” part. The lower 4-bits set the display parameters for number of lines.




So even though the LCD rodmcm has is made by Agena, it is worth having a good read of the Hitachi data sheet as well to understand what is happening.
 
Last edited:
Top