PDA

View Full Version : Simple 4 line LCD display with 18F4550



pic-ker
- 7th April 2007, 18:58
I have written a few lines of code to implement common functions such as displaying a single line and clearing a single line on the 18F4550...spent a long time trying to get this working...this is because some interrupt keeps stopping the LCD routine on the 4550.

This is mainly for beginners and I have tried to include the line display and clear functions similar to the Ohonsoft PIC simulator. This is where i found PBP really lacking so here are the functions, please use them !



DEFINE LCD_LINES 4 ' it is a 4x16 standard LCD
DEFINE LCD_BITS 8 ' LCD is in 8-bit mode connected to PORT0-7
DEFINE LCD_DREG PORTB ' Define LCD connections
DEFINE LCD_RSREG PORTC
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTC
DEFINE LCD_EBIT 1

DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 500 'You can try experimenting with this value

'the following statements make all ports Digital I/O and disable interrupts/comparators etc
TRISD=%00000000
TRISA=%00000000
TRISE=%00000000
ADCON1 = %00001111
CMCON = %00000111
CVRCON = 0
INTCON=%10100001
INTCON2=%01110101
INTCON3=%11000011
T0CON=%00000000


TRISA = %11111111
TRISB = %00000000
TRISC = %00000000
TRISD = %11111111

B1 var byte 'To clear the display
B1=32 '32 is ASCII for space

time1 var Word
time2 var Word
time3 var Word
time4 var Word
msg1 var Byte[64]
msg2 var Byte[64]
tmp var Byte
tmp1 var Byte
j var Byte
mlen var Byte[3]
i var Byte
k var byte
RND var word

mlen(1) = 25


msg1(1) = 72
msg1(2) = 101
msg1(3) = 108
msg1(4) = 108
msg1(5) = 111
msg1(6) = 32
msg1(7) = 87
msg1(8) = 104
msg1(9) = 97
msg1(10) = 116
msg1(11) = 32
msg1(12) = 105
msg1(13) = 115
msg1(14) = 32
msg1(15) = 89
msg1(16) = 111
msg1(17) = 117
msg1(18) = 82
msg1(19) = 32
msg1(20) = 78
msg1(21) = 97
msg1(22) = 77
msg1(23) = 101
msg1(24) = 63
msg1(25) = 63

WaitMs 500
LCDOUT $FE, $0D 'Cursor=Large Blinking thing

k=0
start:

tmp = mlen(1) - 15
For i = 1 To tmp
if K<4 then
k=k+1
else
k=1
endif
if k=1 then gosub lcdline1home
if k=2 then gosub lcdline2home
if k=3 then gosub lcdline3home
if k=4 then gosub lcdline4home
tmp1 = i + 14
For j = i To tmp1
Lcdout msg1(j)
Next j
Gosub time500
if k=1 then GOSUB LCDLINE1CLEAR
if k=2 then gosub LCDline2clear
if k=3 then gosub lcdline3clear
if k=4 then gosub lcdline4clear
Next i
Goto start
End

time500:
For time1 = 1 To 32767
'This is a delay so u can check your interrupts here
Next time1
Return






LCDClear:
LCDOUT $FE,1
return

LCDLine1Home:
LCDOUT $FE,2
return

LCDLine2Home:
LCDOUT $FE,$C0
RETURN

LCDLine3Home:
LCDOUT $FE,$90
RETURN

LCDLine4Home:
LCDOUT $FE,$D0
RETURN

LCDLine1Clear:
'Your cursor will goto line three for a brief period after clearing the line
'If you have problems with the cursor remove ONE b1 from the following LCDOUT statements
LCDOUT $FE,2,B1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1 ,b1
LCDOUT $FE,2
return

LCDLine2Clear:
LCDOUT $FE,$C0,B1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1, b1,b1
LCDOUT $FE,$C0
RETURN

LCDLine3Clear:
LCDOUT $FE,$90,B1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1, b1,b1
LCDOUT $FE,$90
RETURN

LCDLine4Clear:
LCDOUT $FE,$D0,B1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1,b1, b1,b1
LCDOUT $FE,$D0
RETURN

skimask
- 7th April 2007, 19:39
This program will never work.
And if it's interrupts you're worried about, disable them before using the LCD command.

pic-ker
- 8th April 2007, 06:28
I have no idea why you think it wont work...because I have already tested it

My defines are declared in the .INC file and not in the program.
The interrupts are disabled here:


ADCON1 = %00001111
CMCON = %00000111
CVRCON = 0
INTCON=%10100001
INTCON2=%01110101
INTCON3=%11000011
T0CON=%00000000

ok...i may have defined a few TRIS commands twice...but it still works

Darrel Taylor
- 9th April 2007, 00:35
Since you've already tested it.

How did you get WaitMs 500 to work?

Perhaps, with Proton+
<br>

skimask
- 9th April 2007, 03:58
Since you've already tested it.

How did you get WaitMs 500 to work?

Perhaps, with Proton+
<br>

My point exactly...
Also notice the uselessness of the lcdout commands at the end...
Why not just use...
LCDOUT STR 32\20
to clear out a line on an LCD...which works very well in PICBASICPRO.
Go figure...