PDA

View Full Version : LCD via 74HC595



helloo
- 20th October 2010, 12:22
Hello coders,
I'm stuck in a problem: I've connected a LC display with a 16F876A via a 74HC595 stack IC. But the display does'nt do what I want it to.
The cirquit is O.K., the communication to the stack works fine. And the communication to the display works also fine.
If I try to send the regular initialisation sequence, like there are code examples all over the net, it won't work. So what I did was sending a incrementing byte starting at zero. Code is something like this:


RS_state = 0

main: ' 74HC595 : 5=6(E), 4=4(RS), 3=14(DB7), 2=13(DB6), 1=12(DB5), 0=11(DB4)

for char = 0 to 255
gosub tolcd

'pause 500
toggle led

next

goto main

' shiftout datapin,clockpin,msbfirst,[var1, var2] ' MSBFIRST = 1

sendHalfnibble:
Tosend = char >> 4 ' High Nibble only, needed for the initialisation routine, move Char 4 bits to the right
gosub sendtolcd
return

ToLCD:
Tosend = char >> 4 ' High Nibble, move Char 4 digits to the right
gosub sendtolcd
tosend = char ' Low nibble = char, the other bit's won't matter
gosub sendtolcd
return

SendToLCD:
tosend.4 = RS_state ' RS state setzen "0" = command für LCD, "1" = Zeichen an LCD
tosend.5 = 0 ' Enable Bit OFF
shiftout datapin,clockpin,msbfirst,[tosend] ' send data to HC595
high latchpin ' write into paralell buffer
low latchpin
'pause 250

tosend.5 = 1 ' enable bit ON
shiftout datapin,clockpin,msbfirst,[tosend] ' data to HC595
high latchpin ' sent to paralell buffer
low latchpin
'pause 250

tosend.5 = 0 ' Enable OFF
shiftout datapin,clockpin,msbfirst,[tosend] ' send data to HC595
high latchpin ' write to paralell
low latchpin
pause 500

return

As you see, the RS pin is always low, wich means there are only commands to the LCD.

What happens with this code is, that the LCD cursor is switching on and off, and moving around at some bytes, wich makes me sure that the cirquit works, but I don't know how to send the high and low nibbles correctly. Everything works, but I don't know why or how, wich gives me a headache. :(

The stack is connected like: Porta.0 -> clck, Porta.1 -> data, Porta.2 -> latch
porta.3 at the PIC is a LED for debug blinking stuff (to check if the code is still running)

the LCD is connected to the stack like described in the code example (first line)

I've connected a LED to every PIN of the LCD, so with some pauses I can see clearly the data sent to the LCD. This data seems to be O.K. too.

Well so much for that. Now my questions:

A byte within the PIC is bitwise like that: 76543210 wich means bit 7 is the first one, bit zero the last one. What about the nibbles? Are the bits 7654 the high nibble, or bits 3210?

Is it neccesary to send the data first with enable OFF, then with enable ON, then again with enable OFF?



THX for help, and pls excuse my bad english,
helloo

Darrel Taylor
- 20th October 2010, 21:08
You were getting close, but it did need a bit more.
The pins I used are probably different than yours.


INCLUDE "ModeDefs.bas"
INCLUDE "AllDigital.pbp"

datapin VAR PORTB.1
clockpin VAR PORTB.0
latchpin VAR PORTB.2
led VAR PORTA.5

Tosend VAR BYTE
char VAR BYTE
RS_state VAR BIT



GOSUB InitLCD

main: ' 74HC595 : 5=6(E), 4=4(RS), 3=14(DB7), 2=13(DB6), 1=12(DB5), 0=11(DB4)

for char = 32 to 90
gosub SendData
pause 500
toggle led
next char

goto main

;----[Initialize LCD]----------------------------------------------------------
InitLCD:
RS_state = 0
char = $30
GOSUB sendHalfnibble
PAUSE 6
GOSUB sendHalfnibble
PAUSE 1
GOSUB sendHalfnibble
PAUSE 1
char = $20 ; Start 4-bit mode
GOSUB sendHalfnibble
char = $28 ; Function Set, 4-bit, 2-line, 5x7
GOSUB SendCommand
char = $0C ; Display ON
GOSUB SendCommand
char = $01 ; Clear Screen
GOSUB SendCommand
char = $06 ; Entry Mode
GOSUB SendCommand
RETURN

;----[Send the Upper nibble to the LCD]----------------------------------------
sendHalfnibble:
Tosend = char >> 4 ' High Nibble only, needed for the initialisation routine, move Char 4 bits to the right
GOTO sendtolcd
return

SendCommand:
RS_state = 0
GOSUB ToLCD
PAUSE 2
RETURN

SendData:
RS_state = 1

ToLCD:
Tosend = char >> 4 ' High Nibble, move Char 4 digits to the right
gosub sendtolcd
tosend = char & $0F ' Low nibble = char, the other bit's won't matter
gosub sendtolcd
return

SendToLCD:
tosend.4 = RS_state ' RS state setzen "0" = command für LCD, "1" = Zeichen an LCD
tosend.5 = 0 ' Enable Bit OFF
shiftout datapin,clockpin,msbfirst,[tosend] ' send data to HC595
high latchpin ' write into paralell buffer
low latchpin

tosend.5 = 1 ' enable bit ON
shiftout datapin,clockpin,msbfirst,[tosend] ' data to HC595
high latchpin ' sent to paralell buffer
low latchpin

tosend.5 = 0 ' Enable OFF
shiftout datapin,clockpin,msbfirst,[tosend] ' send data to HC595
high latchpin ' write to paralell
low latchpin
pauseus 50
return
4867

helloo
- 24th October 2010, 14:13
Hi Darryl,
thank you for trying to help, but after another three days of trying, changing bytes, switching bits, I am pretty sure that my LCD doesnt meet HD44780 specs for 100%.

Your code does exact the same as mine did before.

So I managed somehow to print out some chars, wich makes me sure:
1. the LCD switches to 4 bit mode
2. the wiring is right

but I could'nt use the 2nd line, nor some other commands.

the next thing I tried was to connect the LC display directly to the pic without the stack - just the same.

and then I tried to write stuff with the LCDOUT command - nothing.

Now I ordered some other LCD's wich are used by other people before (LCD1602). Next week I'll see what happenes.

Again, thanks.

sayzer
- 26th October 2010, 15:34
You were getting close, but it did need a bit more.
The pins I used are probably different than yours.


INCLUDE "ModeDefs.bas"
INCLUDE "AllDigital.pbp"

datapin VAR PORTB.1
clockpin VAR PORTB.0
latchpin VAR PORTB.2
led VAR PORTA.5

Tosend VAR BYTE
char VAR BYTE
RS_state VAR BIT



GOSUB InitLCD

main: ' 74HC595 : 5=6(E), 4=4(RS), 3=14(DB7), 2=13(DB6), 1=12(DB5), 0=11(DB4)

for char = 32 to 90
gosub SendData
pause 500
toggle led
next char

goto main

;----[Initialize LCD]----------------------------------------------------------
InitLCD:
RS_state = 0
char = $30
GOSUB sendHalfnibble
PAUSE 6
GOSUB sendHalfnibble
PAUSE 1
GOSUB sendHalfnibble
PAUSE 1
char = $20 ; Start 4-bit mode
GOSUB sendHalfnibble
char = $28 ; Function Set, 4-bit, 2-line, 5x7
GOSUB SendCommand
char = $0C ; Display ON
GOSUB SendCommand
char = $01 ; Clear Screen
GOSUB SendCommand
char = $06 ; Entry Mode
GOSUB SendCommand
RETURN

;----[Send the Upper nibble to the LCD]----------------------------------------
sendHalfnibble:
Tosend = char >> 4 ' High Nibble only, needed for the initialisation routine, move Char 4 bits to the right
GOTO sendtolcd
return

SendCommand:
RS_state = 0
GOSUB ToLCD
PAUSE 2
RETURN

SendData:
RS_state = 1

ToLCD:
Tosend = char >> 4 ' High Nibble, move Char 4 digits to the right
gosub sendtolcd
tosend = char & $0F ' Low nibble = char, the other bit's won't matter
gosub sendtolcd
return

SendToLCD:
tosend.4 = RS_state ' RS state setzen "0" = command für LCD, "1" = Zeichen an LCD
tosend.5 = 0 ' Enable Bit OFF
shiftout datapin,clockpin,msbfirst,[tosend] ' send data to HC595
high latchpin ' write into paralell buffer
low latchpin

tosend.5 = 1 ' enable bit ON
shiftout datapin,clockpin,msbfirst,[tosend] ' data to HC595
high latchpin ' sent to paralell buffer
low latchpin

tosend.5 = 0 ' Enable OFF
shiftout datapin,clockpin,msbfirst,[tosend] ' send data to HC595
high latchpin ' write to paralell
low latchpin
pauseus 50
return
4867


Darrel,

Would that be possible to use 595_LCD routine (above) with a Hijacked LCDOUT command?

Something like your LCDAnypin hijacking.

________

Darrel Taylor
- 26th October 2010, 16:20
Would that be possible to use 595_LCD routine (above) with a Hijacked LCDOUT command?
Something like your LCDAnypin hijacking.

Yes it would ... with some modification.

But you may want to check out this post from Guido which uses a 74xx164 shift register and only uses 2 pins on the PIC with the HighJack routines.

http://www.picbasic.co.uk/forum/showthread.php?t=7139&p=95344#post95344

helloo
- 28th October 2010, 19:48
Hi coders,
yepp, it was the darn LC display. I tried the 1602, and the code works. *grnnpgf* I spent 5 days of trying for nothing...

Anyway, the next goal is to store strings in PICs memory, and shift them out to the LCD. Darryls "STRINGS.PBP" (http://www.pbpgroup.com/modules/wfsection/article.php?articleid=10) looks very interesting. I think I use his routines again...

I'll write again.

sayzer
- 29th October 2010, 08:17
Yes it would ... with some modification.

But you may want to check out this post from Guido which uses a 74xx164 shift register and only uses 2 pins on the PIC with the HighJack routines.

http://www.picbasic.co.uk/forum/showthread.php?t=7139&p=95344#post95344

Thank you Darrel.
Guido's way did not work in my simulator. I will make the real circuit assuming that the simulator could not simulate it.

helloo
- 31st October 2010, 20:49
The circuit and the program work now. It's easily possible to drive up to three LCDs with only three I/O pins of the µC. Darryls "strings.pbp" work with only minor changes for my program. Thanks again, Darryl.


http://www.youtube.com/watch?v=MHrvifP0jTw