PDA

View Full Version : newbie LCD to 18F4520



Dennis
- 17th November 2009, 19:41
Hi all
I am trying to get my LCD working on a pic 18F4520. (have tested the LCD on a 16f628 and it definitely works but only have the .hex file from years ago)

SO right now I attaching the code I have so far ...
I'm just not sure I have the code all correct.
Do I need to add things like DEFINES to change the default ports.
I would also like to use the built in clock.This is in my code, please could someone just double check it is correct.

Here is my code so far, I know it's probably very wrong but it's what I have so far.



OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter

TRISA = %00000000 'All pins are outputs
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0

DEFINE OSC 32 '4x 8MHz
Pause 500 ' Wait for LCD to startup

loop1:
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Hello" ' Display Hello
Pause 500 ' Wait .5 second

Lcdout $fe, 1 ' Clear LCD screen
Lcdout "World"
Pause 500 ' Wait .5 second

Goto loop1 ' Do it forever


My pinout are as follows and please see the attached diagram too
1 VDD VSS
2 VSS VDD
3 VEE VEE (10k)
4 RS 21(PortD.2)
5 RW VSS
6 E 22(PortD.3)
7
8
9
10
11 D4 27(PortD.4)
12 D5 28(PortD.5)
13 D6 29(PortD.6)
14 D7 30(PortD.7)
15
16

I really would appreciate any help and suggestions.

Kind regards
Dennis

mackrackit
- 17th November 2009, 19:52
Yes, you need to set the DEFINEs if you are not using the default port/pins.

Dennis
- 17th November 2009, 20:38
Thanks Mackrackit

Ok I have altered my code to.. would appreciate if someone could check it's right



'*************************************
'LCD code for 16 X 2 HD4x lcd
'*************************************

'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
'END of oscillator selections


'Port IO directions and presets for port pins begin here
TRISA = %00000000 'All pins are outputs
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here

'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines

'LCD defines begin here
DEFINE LCD_BITS 4 'defines the number of data interface lines (4 or 8)
DEFINE LCD_DREG PORTD 'defines the port where data lines are connected to
DEFINE LCD_DBIT 4 'defines the position of data lines for 4-bit interface (0 or 4)
DEFINE LCD_RSREG PORTD 'defines the port where RS line is connected to
DEFINE LCD_RSBIT 2 'defines the pin where RS line is connected to
DEFINE LCD_EREG PORTD 'defines the port where E line is connected to
DEFINE LCD_EBIT 3 'defines the pin where E line is connected
DEFINE LCD_RWREG 0 'defines the port where R/W line is connected to (set to 0 if not used)
DEFINE LCD_RWBIT 0 'defines the pin where R/W line is connected to (set to 0 if not used)
DEFINE LCD_COMMANDUS 2000 'defines the delay after LCDOUT statement
DEFINE LCD_DATAUS 200 'delay in micro seconds
'END of LCD DEFINES

LCDINIT LcdCurBlink
Pause 500 ' Wait for LCD to startup

loop1:
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Hello" ' Display Hello
Pause 500 ' Wait .5 second

Lcdout $fe, 1 ' Clear LCD screen
Lcdout "World"
Pause 500 ' Wait .5 second

Goto loop1 ' Do it forever

Dennis
- 17th November 2009, 21:29
OK all fixed now and working ....well almost :-)
This line was causing my problem
LCDINIT LcdCurBlink

so just took it out ! See the working code below ;-)
Now there just one problem to sort out ... the "hello world' message writes itself on one long string and then repeats without anyspaces on the first and second lines ....
something like this
Hello||World||Hello||World||Hello||World||Hello||W orld||Hello||World||Hello||World||
How do I format it to something like
Hello
World
?





'*************************************
'LCD code for 16 X 2 HD4x lcd
'*************************************

'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
'END of oscillator selections


'Port IO directions and presets for port pins begin here
TRISA = %00000000 'All pins are outputs
TRISB = %00000000
TRISC = %00000000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here

'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines

'LCD defines begin here
DEFINE LCD_BITS 4 'defines the number of data interface lines (4 or 8)
DEFINE LCD_DREG PORTD 'defines the port where data lines are connected to
DEFINE LCD_DBIT 4 'defines the position of data lines for 4-bit interface (0 or 4)
DEFINE LCD_RSREG PORTD 'defines the port where RS line is connected to
DEFINE LCD_RSBIT 2 'defines the pin where RS line is connected to
DEFINE LCD_EREG PORTD 'defines the port where E line is connected to
DEFINE LCD_EBIT 3 'defines the pin where E line is connected
DEFINE LCD_RWREG 0 'defines the port where R/W line is connected to (set to 0 if not used)
DEFINE LCD_RWBIT 0 'defines the pin where R/W line is connected to (set to 0 if not used)
DEFINE LCD_COMMANDUS 2000 'defines the delay after LCDOUT statement
DEFINE LCD_DATAUS 200 'delay in micro seconds
'END of LCD DEFINES


Pause 500 ' Wait for LCD to startup

loop1:
Lcdout $fe, 1 ' Clear LCD screen
Lcdout "Hello" ' Display Hello
Pause 500 ' Wait .5 second

Lcdout $fe, 1 ' Clear LCD screen
Lcdout "World"
Pause 500 ' Wait .5 second

Goto loop1 ' Do it forever