Streamlined LCD initialisation code, need some help

reywas

Member
Streamlined LCD initialization code, need some help

Never mind, found the problem. Forgot to put the pulsout command in the loop. Doh! (I went ahead and posted the working code for future reference.)


I've been using the long-form 4-bit LCD init code copied from the manual (as I recall) and it works fine but I'd like use the more streamlined version found in the code snippets. I can't get it to work and am hoping you guys can spot the problem. I'm using an 18M2 connected as follows

B.2 = RS
B.3 = EN
B.4 - B.7 = data lines



Code:
[color=Navy]#picaxe [/color][color=Black]18m2[/color]

[color=Green];4-bit LCD routines using portB
;LCD DB4-DB7 = b.4-b.7, RS=b.2, EN=b.3[/color]

[color=Blue]setfreq m32
      
symbol [/color][color=Purple]index [/color][color=DarkCyan]= [/color][color=Purple]b1[/color]
[color=Blue]symbol RS [/color][color=DarkCyan]= [/color][color=Blue]b.2
symbol EN [/color][color=DarkCyan]= [/color][color=Blue]b.3

gosub [/color][color=Black]initB

main:[/color]

[color=Purple]b0 [/color][color=DarkCyan]= [/color][color=Navy]1[/color]
[color=Blue]gosub [/color][color=Black]LCDinst[/color]

[color=Blue]pause [/color][color=Navy]2000[/color]
[color=Blue]for [/color][color=Purple]index [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Navy]15
      [/color][color=Blue]lookup [/color][color=Purple]index[/color][color=Black],[/color][color=Blue]([/color][color=Red]"Hey! All Is Well"[/color][color=Blue])[/color][color=Black],[/color][color=Purple]b0
      [/color][color=Blue]gosub [/color][color=Black]LCDchar[/color]
[color=Blue]next [/color][color=Purple]index

b0 [/color][color=DarkCyan]= [/color][color=Navy]192[/color][color=Black]:[/color][color=Blue]gosub [/color][color=Black]LCDinst[/color]
[color=Blue]for [/color][color=Purple]index [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Navy]15
      [/color][color=Blue]lookup [/color][color=Purple]index[/color][color=Black],[/color][color=Blue]([/color][color=Red]"This Thing Works"[/color][color=Blue])[/color][color=Black],[/color][color=Purple]b0
      [/color][color=Blue]gosub [/color][color=Black]LCDchar[/color]
[color=Blue]next [/color][color=Purple]index[/color]

[color=Blue]pause [/color][color=Navy]8000[/color]
[color=Blue]goto [/color][color=Black]main[/color]
[color=Blue]end[/color]

[color=Black]LCDchar:
      [/color][color=Purple]pinsB [/color][color=DarkCyan]= [/color][color=Purple]b0 [/color][color=DarkCyan]& [/color][color=Navy]$F0              
      [/color][color=Blue]high RS
      pulsout EN[/color][color=Black],[/color][color=Navy]1            
      [/color][color=Purple]b0 [/color][color=DarkCyan]= [/color][color=Purple]b0 [/color][color=DarkCyan]* [/color][color=Navy]16            
      [/color][color=Purple]pinsB [/color][color=DarkCyan]= [/color][color=Purple]b0 [/color][color=DarkCyan]& [/color][color=Navy]$F0        
      [/color][color=Blue]high RS
      pulsout EN[/color][color=Black],[/color][color=Navy]1
      [/color][color=Blue]return[/color]

[color=Black]LCDinst:
      [/color][color=Purple]pinsB [/color][color=DarkCyan]= [/color][color=Purple]b0 [/color][color=DarkCyan]& [/color][color=Navy]$F0              
      [/color][color=Blue]pulsout EN[/color][color=Black],[/color][color=Navy]1            
      [/color][color=Purple]b0 [/color][color=DarkCyan]= [/color][color=Purple]b0 [/color][color=DarkCyan]* [/color][color=Navy]16            
      [/color][color=Purple]pinsB [/color][color=DarkCyan]= [/color][color=Purple]b0 [/color][color=DarkCyan]& [/color][color=Navy]$F0        
      [/color][color=Blue]pulsout EN[/color][color=Black],[/color][color=Navy]1
      [/color][color=Blue]return[/color]

[color=Black]initB:
      
      [/color][color=Blue]pause [/color][color=Navy]1000
      [/color][color=Purple]dirsB [/color][color=DarkCyan]= [/color][color=Navy]$FF

      [/color][color=Blue]for [/color][color=Purple]index [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Blue]to [/color][color=Navy]9
            [/color][color=Blue]lookup [/color][color=Purple]index[/color][color=Black],[/color][color=Blue]([/color][color=Navy]$30[/color][color=Black],[/color][color=Navy]$30[/color][color=Black],[/color][color=Navy]$30[/color][color=Black],[/color][color=Navy]$20[/color][color=Black],[/color][color=Navy]$20[/color][color=Black],[/color][color=Navy]$80[/color][color=Black],[/color][color=Navy]$0[/color][color=Black],[/color][color=Navy]$C0[/color][color=Black],[/color][color=Navy]$0[/color][color=Black],[/color][color=Navy]$10[/color][color=Blue])[/color][color=Black],[/color][color=Purple]b0   
            pinsb [/color][color=DarkCyan]= [/color][color=Purple]b0
            [/color][color=Blue]pulsout EN[/color][color=Black],[/color][color=Navy]1
      [/color][color=Blue]next [/color][color=Purple]index

      [/color][color=Blue]return[/color]
 
Last edited:

westaust55

Moderator
Are you sure that your posted code is working?

I did not spot any lines with:
LOW RS
To counter the HIGH RS and differentiate between data and instructions.
 

reywas

Member
As the 4-bit LCD data is set using "pinsB=", that will be clearing the RS line.
Yes, that is the case. Unfortunately it also meant the need to add "High RS" before each pulsout in the character routine. The routine also works if the "High RS" is replaced by adding an "OR $04" to the "pinsB =" statements but it compiles to the same number of bytes so I just left the "High RS" statements in place for clarity. I welcome any suggestions for optimizing this code.
 

hippy

Ex-Staff (retired)
The OR $04 probably doesn't save any extra bytes but it likely is a bit faster. Also no need to mask the byte after multiplying by 16 as the assignment will only use the lowest 8 bits -

Code:
LCDchar:
  pinsB = b0 & $F0 | $04 : pulsout EN,1            
  pinsB = b0 * 16  | $04 : pulsout EN,1
  return

LCDinst:
  pinsB = b0 & $F0       : pulsout EN,1            
  pinsB = b0 * 16        : pulsout EN,1
  return
 

Flenser

Senior Member
reywas,

I'm not sure what your sequence of initialization commands is intended to do:
Code:
 +----------------------------------- Function Set. 8-bit mode (repeated thrice)
 |           +----------------------- Function Set. 4-bit mode (repeated twice)
 |           |       +--------------- Set DRAM address (to $00)
 |           |       |   +----------- No such command $0
 |           |       |   |  +-------- Set DRAM Address (to $40)
 |           |       |   |  |   +---- No such command $0
 |           |       |   |  |   |  +- Cursor move. Shift to the left.
$30,$30,$30,$20,$20,$80,$0,$C0,$0,$10
- It does not appear to be turning the LCD on, so I can't see how you would be able to see anything on the LCD after running this sequence of commands.


It's different from the sequence of commands that I've used to initialize a 2 line x 16 character LCD in 4-bit mode is:
Code:
 +------------------------------------ Function Set. 8-bit mode (repeated thrice)
 |           +------------------------ Function Set. 4-bit mode
 |           |   +-------------------- Function Set. 4-bit mode, 2-lines, 5x8 Font
 |           |   |   +---------------- Display Clear
 |           |   |   |   +------------ Cursor Home
 |           |   |   |   |   +-------- Entry Mode Set. I/D=Increment, S=Accompanies display shift
 |           |   |   |   |   |   +---- Display On
 |           |   |   |   |   |   |
$30,$30,$30,$20,$28,$01,$02,$06,$0C
- I got this from one of the tutorials that are around on the web
- The sequence at the start of $30,$30,$30,$20 comes straight from the datasheet for the Hitachi HD44780 LCD
- I've seen the sequence of commands $01,$02 and $06 in a different order. Their effect won't become visible until you execute command $0C to turn the display on anyway.
 

hippy

Ex-Staff (retired)
I'm not sure what your sequence of initialization commands is intended to do:
Code:
 +----------------------------------- Function Set. 8-bit mode (repeated thrice)
 |           +----------------------- Function Set. 4-bit mode (repeated twice)
 |           |       +--------------- Set DRAM address (to $00)
 |           |       |   +----------- No such command $0
 |           |       |   |  +-------- Set DRAM Address (to $40)
 |           |       |   |  |   +---- No such command $0
 |           |       |   |  |   |  +- Cursor move. Shift to the left.
$30,$30,$30,$20,$20,$80,$0,$C0,$0,$10
That part of the code is outputting nibbles from the top 4 bits of each byte, two consecutive nibbles define what a byte being output would have been ...

$20,$80 => $28
$0,$C0 => $0C
$0,$10 => $01
 
Top