PDA

View Full Version : Crystalfontz LCD



jman12
- 11th January 2007, 18:32
Hi
Silly question
Has anyone done an emulator in PBP for these http://www.crystalfontz.com/products/632/index.html serial displays.
I am thinking of writing one but i dont want to revent the wheel.
That way we can use our cheap LCD's with the magnitude of software
allready out there for these displays.


John

jman12
- 31st January 2007, 04:58
Hi
So nobody done this i got it mostly working
Works with LCD Smartie and my router .
At 9600 my router displays rubbish characters but at 9600 when
connected to my pc it seems ok.
ideas anybody
is this good code / bad code

Thanks



'* Name : Crystal.pbp adapted from SERCLCD1b.BAS *
'* Author : John Gerrard *
'* Notice : Orginal Author Ranjit Diol *
'* : *
'* Date : 29/01/07 *
'* Version : 1..00 *
'* Compiler: PICBasicPro Ver 2.45 *
'* Notes : Designed for HD44780 type character *
'* MCU : PIC16F628 *
'* *

DEFINE OSC 20 'Set the Xtal frequency
include "Modedefs.bas" 'Include general definitions

'ADCON1 = 7 'Make all pins digital
cmcon = 7 ' Make port Digital

' ** Declare LCDOUT Defines **
DEFINE LCD_LINES 2
Define LCD_DREG PORTB
Define LCD_DBIT 4
Define LCD_RSREG PORTB
Define LCD_RSBIT 0
Define LCD_EREG PORTB
Define LCD_EBIT 3




' ** Define LCD Control Constants **

CTL Con 254 ' Control Byte
Clr Con 1 ' Clear the display
HOME Con 2 'Beginning of the 1st line
LINE1 Con 128 ' Point to beginning of line 1
LINE2 Con 192 ' Point to beginning of line 2
LINE3 Con 148 ' Point to beginning of line 3
LINE4 Con 212 ' Point to beginning of line 4
CGram Con 64 ' Point to Cgram within LCD
DDram Con 128 ' Pont to DDRAM
ShiftL Con 24 ' Shift display left
ShiftR Con 28 ' Shift display right
Blank Con 8 ' Blank display (don't clear memory)
ULon Con 14 ' Underline ON
BlinkCur Con 15 ' Blinking Cursor
CurLeft Con 16 ' Move cursor left 1 position
CurRight Con 20 ' Move cursor right 1 position
BlockCur Con 13 ' Block cursor
Space Con 32 'ASCII code for a space
Null CON 0


' ** Declare Variables **


SI Var PortB.1 ' Serial In Inverted
VDD Var PortA.3 ' LCD VDD pin
InByte1 Var byte ' The byte received
InByte2 var byte ' Second byte recvd
InBytes VAR BYTE[4] 'For multi commands
baud var word 'Baud rate
bd var word 'Displayed baud number
BP0 var PortA.0 'A0,A1,A3 Pins used to determine
BP1 var PortA.1 'baud rate and polarity
BP2 var PortA.2
ch1 var byte 'General variables
ch2 var byte
ch3 var byte
bits var byte 'Used for baud calculations
tmp var byte 'Temp var
CGChar var byte[8] 'CGRam custom character
CGAddr var byte 'CGRam address
cnt var byte 'General use counter

' Init LCD
HIGH VDD ' Switch On LCD VDD
Lcdout $fe, 1 ' Init LCD
pause 100 ' Wait for LCD to Initialize

'Initialize variables
bits = 0
'Set to high
high BP0
high BP1
high BP2
low SI
'Now make them input (they should read high)
input BP0
input BP1
input BP2
input SI


'Check pins to determine baud rate and polarity
'BITS var will end up having a value to
'determine baud and polarity
if BP0 = 0 then
bits.0=0
else
bits.0=1
endif

if BP1 = 0 then
bits.1=0
else
bits.1=1
endif

if BP2 = 0 then
bits.2=0
else
bits.2=1
endif

if bits > 3 then
bits = bits - 4
ch1 = "N" 'Inverted TTL RS232
else
ch1 = "T" 'True RS232
endif
'19200 4800 2400 9600
lookup2 bits,[32,188,396,84],baud
lookup2 bits,[19200,4800,2400,9600],bd
if ch1 = "N" then baud.14 = 1 'Invert it


'==== MAIN SECTION =======
gosub Init
InByte1 = " "

MainLoop:
Serin2 SI,baud,[InByte1] ' Receive the serial byte
if InByte1 <= 31 then goto LCDfonz
if InByte1 >= 128 then goto Custom


Lcdout InByte1 'Dispaly the Char

Goto MainLoop

LCDfonz:

'if InByte1 = 0 then MainLoop
select case InByte1
case 0
' Null do nothing
case 1
lcdout CTL, Line1
'LOW VDD 'lcdout ctl, 1
'pause 30
case 2
Lcdout CTL, blank

case 3

Lcdout CTL, 12

case 4
Lcdout CTL, 12

Case 5
lcdout ctl, ulon
case 6
lcdout ctl, blockcur
case 7
Lcdout CTL, blockcur
case 8
Lcdout CTL, curleft
case 9
Serin2 SI,baud,[InBytes(0)] 'command Value
case 10
' Null do nothing
Case 11
' Null do nothing
case 12
lcdout CTL, $C0
lcdout " "
lcdout CTL, $80
lcdout " "
case 13
' Null do nothing
case 14

Serin2 SI,baud, [InBytes(0)] 'Brightness
'LOW VDD 'high vdd
case 15
Serin2 SI,baud, [InBytes(0)] 'Contrast

case 16
' Null do nothing
Case 17
Serin2 SI,baud, [InBytes(0), InBytes(1)]
if InBytes(1) = 0 Then
Lcdout CTL, ($80 + InBytes(0))
else
lcdout CTL, ($C0 + Inbytes(0))
endif

case 18
' Null do nothing
case 19
' Null do nothing
Case 20
' Null do nothing
case 21
' Null do nothing
case 22
' Null do nothing
Case 23
' Null do nothing
case 24
' Null do nothing
case 25
Serin2 SI,baud, [CGAddr, CGChar(0), CGChar(1),_
CGChar(2), CGChar(3), CGChar(4), CGChar(5),_
CGChar(6), CGChar(7)]
Select case CGAddr
Case 0
LCDOUT CTL,64,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 1
LCDOUT CTL,72,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 2
LCDOUT CTL,80,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 3
LCDOUT CTL,88,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 4
LCDOUT CTL,96,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 5
LCDOUT CTL,104,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CG Char(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 6
LCDOUT CTL,112,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CG Char(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 7
LCDOUT CTL,120,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CG Char(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char
End select
Case 26
' Null do nothing
case 27
' Null do nothing
case else
'Lcdout CTL,InByte1
end select
goto Mainloop

Custom:
if InByte1 > 135 Then ' if not a custom Char then return
Lcdout InByte1
Goto Mainloop
endif

select case InByte1
case 128
lcdout 0 ' Dispaly Custom Char 0
case 129
lcdout 1 ' Dispaly Custom Char 1
case 130
lcdout 2 ' Dispaly Custom Char 2
case 131
lcdout 3 ' Dispaly Custom Char 3
case 132
lcdout 4 ' Dispaly Custom Char 4
Case 133
lcdout 5 ' Dispaly Custom Char 5
case 134
lcdout 6 ' Dispaly Custom Char 6
case 135
Lcdout 7 ' Dispaly Custom Char 7

end Select
goto MainLoop


'******End of Main Program *********
END
'***********************************

'Subroutines
'ClrLCD:
' Lcdout CTL,Clr
' Pause 50 ' Clear the LCD
'Return

Init:

'Load Custom CGRam characters
LCDOut CTL,64,$00,$04,$02,$1F,$02,$04,$00,$00,CTL,128
LCDOut CTL,72,$00,$04,$08,$1F,$08,$04,$00,$00,CTL,128
LCDOut CTL,80,$00,$04,$0E,$15,$04,$04,$00,$00,CTL,128
LCDOut CTL,88,$00,$00,$04,$04,$15,$0E,$04,$00,CTL,128
LCDOut CTL,96,$1F,$11,$11,$11,$11,$11,$11,$1F,CTL,128
LCDOut CTL,104,$04,$0E,$1F,$00,$00,$1F,$0E,$04,CTL,128
LCDOut CTL,112,$02,$06,$0E,$1E,$0E,$06,$02,$00,CTL,128
LCDOut CTL,120,$08,$0C,$0E,$0F,$0E,$0C,$08,$00,CTL,128

logo:
'Lcdout CTL,Clr:Pause 100 ' Clear the LCD
Lcdout CTL,LINE1,"Crystal EMU .90"
Lcdout CTL,LINE2,"Baud: ", ch1,dec bd
Lcdout CTL,HOME


Return

BobK
- 31st January 2007, 14:10
Hi jman12,

Yes you are re-inventing the wheel so to speak. There was a recent thread regarding serial interfaces for LCDs. I have purchased a number of 9600 and 2400 interfaces from www.wulfden.org. Very inexpensive. They come in kit form (I love building things!) and also have 1x14 and 2x7 pinouts and they handle up to 20x4 screens.

HTH
BobK

jman12
- 1st February 2007, 17:58
Yip i can buy one of those very nice looking units but still i want it to
emulate a Crystalfonz unit.
I like the idea of the 64 char buffer any idea's on how to do that

Jman

jman12
- 3rd February 2007, 21:09
Hi
The codes now looks like this
i use hserin and now 9600 is perfect
i also added contrast control (thanks to the forum serach for that).
This code now works quite well LcdSmartie and my Miktotik routers.

Regards Jman

' LCD should be connected as follows:
' LCD PIC
' DB4 PortA.0
' DB5 PortA.1
' DB6 PortA.2
' DB7 PortA.3
' RS PortB.7
' E PortB.6
' RW Ground
' Vdd 5 volts
' Vss Ground
' Vo PMW via20K potentiometer (or ground)
' DB0-3 No connect
'
'

DEFINE OSC 20 'Set the Xtal frequency
include "Modedefs.bas" 'Include general definitions

'ADCON1 = 7 'Make all pins digital
cmcon = 7 ' Make port Digital

' ** Declare LCDOUT Defines **
DEFINE LCD_LINES 2
Define LCD_DREG PORTA
Define LCD_DBIT 0
Define LCD_RSREG PORTB
Define LCD_RSBIT 7
Define LCD_EREG PORTB
Define LCD_EBIT 6




' HSERIN defines

DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_SPBRG 129 ' 9600 Baud @ 20MHz, 0.16%
DEFINE HSER_CLROERR 1 ' Clear overflow automatically


' ** Define LCD Control Constants **

CTL Con 254 ' Control Byte
Clr Con 1 ' Clear the display
HOME Con 2 'Beginning of the 1st line
LINE1 Con 128 ' Point to beginning of line 1
LINE2 Con 192 ' Point to beginning of line 2
LINE3 Con 148 ' Point to beginning of line 3
LINE4 Con 212 ' Point to beginning of line 4
CGram Con 64 ' Point to Cgram within LCD
DDram Con 128 ' Pont to DDRAM
ShiftL Con 24 ' Shift display left
ShiftR Con 28 ' Shift display right
Blank Con 8 ' Blank display (don't clear memory)
ULon Con 14 ' Underline ON
BlinkCur Con 15 ' Blinking Cursor
CurLeft Con 16 ' Move cursor left 1 position
CurRight Con 20 ' Move cursor right 1 position
BlockCur Con 13 ' Block cursor
Space Con 32 'ASCII code for a space
Null CON 0
Restore CON 12

' ** Declare Variables **



InByte1 Var byte ' The byte received
InByte2 var byte ' Second byte recvd
InBytes VAR BYTE[4] 'For multi commands
contrast var byte 'General variables
ch2 var byte
ch3 var byte
tmp var byte 'Temp var
CGChar var byte[8] 'CGRam custom character
CGAddr var byte 'CGRam address
cnt var byte 'General use counter

' Init LCD
Lcdout $fe, 1 ' Init LCD
pause 100 ' Wait for LCD to Initialize

'contrast set default
'HPWM 1,20,1300


'==== MAIN SECTION =======
gosub Init
InByte1 = " "

MainLoop:
HSerin [InByte1] ' Receive the serial byte
if InByte1 <= 31 then goto LCDfonz
if InByte1 >= 128 then goto Custom


Lcdout InByte1 'Dispaly the Char

Goto MainLoop

LCDfonz:

if InByte1 = 0 then MainLoop

select case InByte1
case 1
lcdout CTL, Line1
'LOW VDD 'lcdout ctl, 1
'pause 30
case 2
Lcdout CTL, blank

case 3

Lcdout CTL, Restore

case 4
Lcdout CTL, 12

Case 5
lcdout ctl, ulon
case 6
lcdout ctl, blockcur
case 7
Lcdout CTL, blockcur
case 8
Lcdout CTL, curleft
case 9
hSerin [InBytes(0)] 'command Value
case 10
' Null do nothing
Case 11
' Null do nothing
case 12
lcdout CTL, $80
lcdout " "
lcdout CTL, $C0
lcdout " "
case 13
' Null do nothing
case 14

hSerin [InBytes(0)] 'Brightness
'LOW VDD 'high vdd
case 15
hSerin [InBytes(0)] 'Contrast
contrast = inbytes(0) * 2
HPWM 1,contrast,1300
case 16
' Null do nothing
Case 17
HSerin [InBytes(0), InBytes(1)]
if InBytes(1) = 0 Then
Lcdout CTL, ($80 + InBytes(0))
else
lcdout CTL, ($C0 + Inbytes(0))
endif

case 18
' Null do nothing
case 19
' Null do nothing
Case 20
' Null do nothing
case 21
' Null do nothing
case 22
' Null do nothing
Case 23
' Null do nothing
case 24
' Null do nothing
case 25
hserin [CGAddr, CGChar(0), CGChar(1),_
CGChar(2), CGChar(3), CGChar(4), CGChar(5),_
CGChar(6), CGChar(7)]
Select case CGAddr
Case 0
LCDOUT CTL,64,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 1
LCDOUT CTL,72,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 2
LCDOUT CTL,80,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 3
LCDOUT CTL,88,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 4
LCDOUT CTL,96,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 5
LCDOUT CTL,104,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CG Char(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 6
LCDOUT CTL,112,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CG Char(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

Case 7
LCDOUT CTL,120,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CG Char(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char
End select
Case 26
' Null do nothing
case 27
' Null do nothing
case else
'Lcdout CTL,InByte1
end select
goto Mainloop

Custom:
if InByte1 > 135 Then ' if not a custom Char then return
'Lcdout InByte1
Goto Mainloop
endif

select case InByte1
case 128
lcdout 0 ' Dispaly Custom Char 0
case 129
lcdout 1 ' Dispaly Custom Char 1
case 130
lcdout 2 ' Dispaly Custom Char 2
case 131
lcdout 3 ' Dispaly Custom Char 3
case 132
lcdout 4 ' Dispaly Custom Char 4
Case 133
lcdout 5 ' Dispaly Custom Char 5
case 134
lcdout 6 ' Dispaly Custom Char 6
case 135
Lcdout 7 ' Dispaly Custom Char 7

end Select
goto MainLoop





Init:

'Load Custom CGRam characters
LCDOut CTL,64,$00,$04,$02,$1F,$02,$04,$00,$00,CTL,128
LCDOut CTL,72,$00,$04,$08,$1F,$08,$04,$00,$00,CTL,128
LCDOut CTL,80,$00,$04,$0E,$15,$04,$04,$00,$00,CTL,128
LCDOut CTL,88,$00,$00,$04,$04,$15,$0E,$04,$00,CTL,128
LCDOut CTL,96,$1F,$11,$11,$11,$11,$11,$11,$1F,CTL,128
LCDOut CTL,104,$04,$0E,$1F,$00,$00,$1F,$0E,$04,CTL,128
LCDOut CTL,112,$02,$06,$0E,$1E,$0E,$06,$02,$00,CTL,128
LCDOut CTL,120,$08,$0C,$0E,$0F,$0E,$0C,$08,$00,CTL,128

logo:
Lcdout CTL,LINE1,"Crystal EMU Ver .99"
Lcdout CTL,LINE2,"Jman says 9600 baud"
Lcdout CTL,HOME


Return

Archangel
- 4th February 2007, 05:53
Hi jman,
you got a typo that will cause a no compile


LCDOUT CTL,64,CGChar(0),CGChar(1),CGChar(2),CGChar(3),CGC har(4),CGChar(5),CGChar(6),CGChar(7) ' Write Custom Char

it says CGC har instead if CGChar in 8 places.

jman12
- 4th February 2007, 10:31
HI
It was during the copy paste from microcode mine has no compile errors
and looks good in microcode :)
Also the contrast is now connected to pmw pin with 2 resistors as per Mels diagram
Thanks for looking

Jman

mister_e
- 4th February 2007, 12:17
you could still shrink it a little bit

LCDOUT LCDOUT CTL,64, STR CGChar\8 ' Write Custom Char

jman12
- 9th February 2007, 04:57
Thanks Mister E worked perfect.
Any clue on how to add a buffer 64 chars would be nice

John

mister_e
- 9th February 2007, 15:04
BufferChar VAR BYTE[64]
'
'
'
'
'
'
'
HSERIN [STR BufferChar\64]