Wednesday, December 14, 2011

Serial-to-parallel LCD display driver

Amazingly we managed to get a working serial character LCD in under an hour today!
Just followed the schematics at http://embedded-lab.com/blog/?p=30 and converted the code example they gave into Oshonsoft Basic. Fired it up and there we go - a blinking "Hello World!" message using just three pins on the microcontroller.



The only real issue we had was that the contrast isn't brilliant; instead of a 10K linear pot, we've gone and used the first potentiometer that was to hand (a 1M logarithmic pot!)


If, like us here at Nerd Towers,  you're an Oshonsoft fan, here's the code listing to get everything working:

AllDigital
Config PORTC = Output

configuration:
       Symbol enablepin = PORTC.0
       Symbol clockpin = PORTC.1
       Symbol datapin = PORTC.2
     
declarations:
       Dim i As Byte
       Dim lcdi As Byte
       Dim lcdbyte As Byte
       Dim lcdbytetosend As Byte
       Dim rs As Bit
       Dim flag As Byte
       Dim mask As Byte
     
init:
       Gosub initialiselcd
     
loop:
       lcdbytetosend = 32
       Gosub writelcddata
       lcdbytetosend = "H"
       Gosub writelcddata
       lcdbytetosend = "e"
       Gosub writelcddata
       lcdbytetosend = "l"
       Gosub writelcddata
       lcdbytetosend = "l"
       Gosub writelcddata
       lcdbytetosend = "o"
       Gosub writelcddata
       lcdbytetosend = 32
       Gosub writelcddata
       lcdbytetosend = "W"
       Gosub writelcddata
       lcdbytetosend = "o"
       Gosub writelcddata
       lcdbytetosend = "r"
       Gosub writelcddata
       lcdbytetosend = "l"
       Gosub writelcddata
       lcdbytetosend = "d"
       Gosub writelcddata
       lcdbytetosend = "!"
       Gosub writelcddata
     
       WaitMs 4000
       lcdbytetosend = 0x01 'clear screen
       Gosub writelcdcommand
     
       WaitMs 2000

Goto loop
End


writelcdnibble:
       Low enablepin
       Low clockpin
       If rs = 1 Then High datapin Else Low datapin

       Gosub toggleclockpin
     
       'shift in 4 bits
       mask = 8
       For lcdi = 1 To 4
              flag = lcdbyte And mask
              If flag = 0 Then Low datapin Else High datapin
              Gosub toggleclockpin
              mask = ShiftRight(mask, 1)
       Next lcdi
            
       'now strobe the clock one more time because ST+SC are tied
       Gosub toggleclockpin

       'toggle the enable pin to "flush" the data into the lcd
       Low datapin
       High enablepin
       Low enablepin
     
Return



toggleclockpin:
       'toggle the clock pin
       High clockpin
       Low clockpin
Return



writelcddata:
       rs = 1
       Gosub senddata
Return



writelcdcommand:
       rs = 0
       Gosub senddata
Return



senddata:
       lcdbyte = ShiftRight(lcdbytetosend, 4)
       Gosub writelcdnibble
       lcdbyte = lcdbytetosend And 15
       Gosub writelcdnibble
Return



initialiselcd:
       For i = 1 To 3
              WaitMs 50
              lcdbytetosend = 0x20
              Gosub writelcdcommand
       Next i
     
       WaitMs 50
       lcdbytetosend = 0x28 '4 bits, 2 lines, 5x7 font
       Gosub writelcdcommand
     
       WaitMs 50
       lcdbytetosend = 0x0c 'display on, no cursors
       Gosub writelcdcommand
     
       WaitMs 50
       lcdbytetosend = 0x06 'entry mode auto-increment
       Gosub writelcdcommand
     
       WaitMs 50
       lcdbytetosend = 0x01 'clear screen
       Gosub writelcdcommand
     
Return

No comments:

Post a Comment