Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 17:21 01 Dec 2024 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : LCD commands

Author Message
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 07:52pm 28 Feb 2014
Copy link to clipboard 
Print this post

For those who want to play with their LCD displays with respect to scrolling and cursors etc, here are a few codes that might help.

This is for 16x2 LCD only. My 20x4 have not turned up and I haven't looked at the data sheet.

Clear Display
1 Self Expanatory

Cursor Home
2 Sets Cursor to Line 1 left edge

Entry Mode Set
4 Cursor moves left, no shift display
5 Cursor moves left, Shift Display Left
6 Cursor moves right, No shift display
7 Cursor moves right, Shift display left


Display On/Off
8 Display Off, Cursor Off, Blink Off
9 Display Off, Cursor Off, Blink On
10 Display Off, Cursor On, Blink Off
11 Display off, cursor On, Blink On
12 Display On, Cursor Off, Blink Off
13 Display on, Cursor Off, Blink On
14 Display On, Cursor On, Blink Off
15 Display On, Cursor On, Blink On

Cursor Or Display Shift
16 Shift Cursor Left, Dec Address Counter
20 Shift Cursor Right, Inc Address Counter
24 Shift Display Left, Cursor Shift according to display
28 Shift Display Right, Cursor Shift according to display

Function Set
32 4 Bit Mode, 5x8, 1 Line Display
36 4 Bit Mode, 5x11, 1 Line Display
40 4 Bit Mode, 5x8, 2 Line Display
44 4 Bit Mode, 5x11, 2 Line Display
48 8 Bit Mode, 5x8, 1 Line Display
52 8 Bit Mode, 5x11, 1 Line Display
56 8 Bit Mode, 5x8, 2 Line Display
60 8 Bit Mode, 5x11, 2 Line Display

Set CGRAM Address
64 to 127

Set DDRAM Address
128 to 255

(128 is start of line 1 on 16x2 Display)
(192 is start of line 2 on 16x2 Display)

For all of the above, the RS line is low.

Read Busy Flag
and Address bits 0 to 6 (Bit 7 is Busy Flag) Not available as R/W pin is Connected to 0v.

Write Data to CGRAM or DDRAM - may not be available as RS may not be operational. May need uMBASIC commands.
Edited by MOBI 2014-03-02
David M.
 
WhiteWizzard
Guru

Joined: 05/04/2013
Location: United Kingdom
Posts: 2821
Posted: 12:32am 01 Mar 2014
Copy link to clipboard 
Print this post


I have written some code for those of you who wish to immediately directly control an LCD and hence have access to all LCD functions (i.e. blinking cursor). This is purely a temporary thing until Geoff releases any code to send LCD commands.

Two subs:
LcdCommand
LcdData


Each needs to have a 8-bit binary value passed in the form of a string. I used binary since it is easier to cross reference with values shown in data sheets which you WILL need to refer to!

The following code demonstrates how to initialise an LCD and then display the word 'HI' along with the default blinking cursor. Should be a good starting point to explore further.

Have fun . . . . .


' first we need to define the Pins that the LCD are connected too - follows Geoffs built-in LCD Init function
d4 = 2
d5 = 3
d6 = 4
d7 = 5
rs = 24
en = 25


'now we need to define all Pins connected to LCD as Digital Outputs
SetPin(d4),dout
SetPin(d5),dout
SetPin(d6),dout
SetPin(d7),dout
SetPin(rs),dout
SetPin(en),dout


'initialise LCD
Pause 10 'required after initial power-up
Pin(rs)=0 'rs low means instruction/command being sent
Pin(en)=0 'ensure en is initially low
Pin(d7) = 0 'hardcode first nibble onto Pins as per data sheet (Function Set)
Pin(d6) = 0
Pin(d5) = 1
Pin(d4) = 0
GoSub LcdEn
'call this sub to load nibble into LCD

LcdCommand("00101000") 'Function Set for 2 line (and 4line) LCDs - Refer to datasheet

LcdCommand("00001111") 'Display On/Off Control - Refer to datasheet

LcdCommand("00000110") 'Entry Mode Set - Refer to datasheet

'now write some data to LCD ('HI')
LcdData("01001000") 'Write to CGRAM/DDRAM - Letter 'H'
LcdData("01001001") 'Letter 'I'

'YOUR CODE CONTINUES HERE . . . .

End

Sub LcdCommand(arg1$)
Pin(rs)=0
'rs low means value is a Command
LcdWrite(arg1$) 'call common sub to write value
End Sub

Sub LcdData(arg1$)
Pin(rs)=1
'rs high means value is data
LcdWrite(arg1$) 'call common sub to write value
End Sub

Sub LcdWrite(arg1$)
arg1$=String$(8-Len(Right$(arg1$,8)),"0")+arg1$
'ensure binary value is length 8 - bullet proof
Print arg1$ 'display binary value sent to LCD on console - useful for debugging!
Pin(d7) = Val(Mid$(arg1$,1,1)) 'extract value for pin (high nibble first)
Pin(d6) = Val(Mid$(arg1$,2,1))
Pin(d5) = Val(Mid$(arg1$,3,1))
Pin(d4) = Val(Mid$(arg1$,4,1))
GoSub LcdEn
'first send high nibble
Pin(d7) = Val(Mid$(arg1$,5,1))
Pin(d6) = Val(Mid$(arg1$,6,1))
Pin(d5) = Val(Mid$(arg1$,7,1))
Pin(d4) = Val(Mid$(arg1$,8,1))
GoSub LcdEn
'now send low nibble
End Sub


LcdEn:
'sub loads value on Pins into LCD by pulsing en Pin high
Pin(en) = 0
Pin(en) = 1
Pin(en) = 0
Return

For everything Micromite visit micromite.org

Direct Email: whitewizzard@micromite.o
 
MOBI
Guru

Joined: 02/12/2012
Location: Australia
Posts: 819
Posted: 02:04pm 01 Mar 2014
Copy link to clipboard 
Print this post

If you have access to an i2c based LCD, the uMite coding gets even simpler as all the

data manipulation is done in the LCD i2c slave chip. This also saves you from having to

define multiple uMite (or MM) pins as besides VDD and VSS, there are only two i2c lines

(SCL and SDA) to plug in and these are fixed. They are also common to all other i2c

devices so it is easy to create a simple bus system.

The PIC based LCD Slave module I made simply receives a data byte and depending on the

contents of the accompanying control byte, either treats the data byte as a character

or a command.

In my case, there are several control codes:


1 = treat the data byte as a character
2 = treat the data byte as a command
4 = LCD reset
8 = PWM value (0 to 255) for LCD backlight

So, if we want to write "A" at The start of line 2, we first have to set the cursor

position to the start of line 2 e.g.

i2c write LCD_address,0,3,0,192,2 '2 means command and 192 is the cursor position


then we send the character to be displayed.

i2c write LCD_address,0,3,0,65,1 '1 means character, 65 = "A"


Subsequent characters will be displayed at the next position in sequence.

If for example you want to output a string of data, use a for-next loop to extract each

character from the String and send it to the LCD. It will be faster than you can blink.

Note that once the cursor position is set, you don't have to keep on defining the next cursor position, it happens automatically.


also note that not all i2c LCDs will act the same as mine, but the source and HEX code as well as the cct diagramme are freely available to those who are game/able to programme a pic or solder on a bit of vero board.

=====================
bottom note on command table re RS was not meant to be included in the first post.
RS high and RW low writes the character to the display at the current cursor position.


Edited by MOBI 2014-03-03
David M.
 
Print this page


To reply to this topic, you need to log in.

© JAQ Software 2024