PDA

View Full Version : Simple LCD code not working!...WHY?



jellis00
- 29th November 2009, 00:15
The below rather simple code will not operate a 2x16 LCD display that is installed in my EasyPic6 with a 18F4550 MCU. I have studied the examples that came with the EasyPic6 (which work with a 16F887 indicating the EasyPic6 is not the problem), the schematic for the EasyPic6 to make sure I had the port settings correct as used in the EasyPic6/LCD, and the LCDOUT section of the PBP manual. From this I am convinced I have the hardware setup and initalized correctly. I placed the TOGGLE statements in the code to trigger LED's that are on the EasyPic6 as a troubleshooting measure, and they continuously light up in the correct sequence, telling me that the code is actually working through the Main loop. So this made me believe I have some kind of problem with analog settings so used DT's ALLDIGITAL.pbp INCLUDE which I am not sure works with the 18F4550.
I checked the contrast and it is OK so the display's blue backlight turns on and it clears properly during the code, but no words are displayed.
From all of this, I am still not succeeding and am getting frustrated. Can anyone look at this rather simple code and tell me what might be wrong?

Define OSC 8

' LCD DEFINES
'============
DEFINE LCD_DREG PORTB ' Use PortB for LCD Data
DEFINE LCD_DBIT 4 ' Use lower(4) 4 bits of PortB
' PORTB.0 thru PORTB.3 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTB ' PORTB for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 4 ' PORTB.4 pin for LCD's RS line
DEFINE LCD_EREG PORTB ' PORTB for Enable (E) bit
DEFINE LCD_EBIT 5 ' PORTB.5 pin for LCD's E line
DEFINE LCD_BITS 4 ' Using 4-bit bus
DEFINE LCD_LINES 2 ' Using 2 line Display
DEFINE LCD_COMMANDUS 2000 ' Command Delay (uS)
DEFINE LCD_DATAUS 50 ' Data Delay (uS)

'** DEFINE LCD Control Constants **
'====================================
I CON 254 ' Control Byte
Clr CON 1 ' Clear the display
Line1 CON 128 ' Point to beginning of line 1
Line2 CON 192 ' Point to beginning of line 2

;--- if you un-comment these, you must comment the ones in the .inc file--
ASM ; 18F2550/4550, 8mhz crystal
__CONFIG _CONFIG1L, _PLLDIV_2_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
__CONFIG _CONFIG1H, _FOSC_HSPLL_HS_1H
__CONFIG _CONFIG2L, _PWRT_ON_2L & _BOR_OFF_2L & _VREGEN_ON_2L
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _PBADEN_OFF_3H
__CONFIG _CONFIG4L, _LVP_OFF_4L & _XINST_OFF_4L
ENDASM

' Initialize Hardware
' -------------------
INCLUDE "ALLDIGITAL.pbp" ' Sets all registers for digital ops.
' User must make sure the AllDigital.pbp file
' is in same directory location as this source
' code before compiling.
DEFINE SHOWDITIAL 1 ' When uncommented will show analog settings
' in Assembler Results window.
' A/D & Comparators disabled for digital ops
' All of these statements should be commented out when using the
' INCLUDE "AllDigital.pbp" statement to set digital ops.
'ADRESH = 0
'ADRESL = 0
'ADCON0 = 0
'ADCON1 = %11111111
'ADCON2 = 0
TRISA =%00000000
TRISB =%11111111
TRISC =%00000000
TRISD =%00000000
TRISE =%00000000
PORTB = 0

'Main Loop
'=========
Main:
Pause 2000 ' Delay to let LCD start up
TOGGLE PortA.2:Pause 1000 ' For test only...ops comment out
LCDOut I,Clr:Pause 1000 ' Clear Display
TOGGLEe PortA.3:Pause 1000 ' For test only...ops comment out
LCDOut I,Line1+2," TEST " ' Display "TEST" on 1st line
LCDOut I,Line2+2,"..Power On.. !!" ' Display "Power on" on 2nd line
TOGGLE PortA.4:Pause 1000 ' For test only...ops comment out
Goto Main ' Continue loop

Dennis
- 29th November 2009, 00:52
Hi jellis00

Do you see blocks on the LCD ?
What's this line in your code?
'DEFINE SHOWDITIAL 1'
shouldn't it be showdigital ?
Next .. which port is your LCD connected to ?
If it's on PORTB like your code shows then your TRIS statement is set in the wrong direction.
You have
TRISB =%11111111 which should actually be
TRISB=%00000000 since you are sending to the LCD so the port should be set for output.
A nice way to remember it is that the number one looks like an i (I) which stands for Input and a zero (0 ) stands for output :-)

I feel you frustration by the way.
I spent many a night trying to get mine working :-)
If you like you are welcome to follow my example and then head back to yours once it's working and trouble-shoot the problem !
So probably the easiest is to just get it working and then trouble shoot. I used PORTD by the way :-)
By the way I have an 18F4520 (very similar)

Your problems could be a few things .. like a pause 1000 ( 1 second delay) before your code , giving time for the LCD to 'warm up' or initialize and so forth.

So I'll attach a schematic just wire it the same way and then just cut and paste my code and compile it and program your pic with it.
Here's my code


'*************************************
'Keypress display on LCD and TX to wherever
'*************************************

'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
cmcon = 7
INTCON2.7 = 0 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 'All pins are inputs
' Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %11111111 'for 4x4 keypad all input
TRISC = %10010000
TRISD = %00000000
TRISE.0 = 0
TRISE.1 = 0
TRISE.2 = 0
'End of Port IO directions and presets for port pins begin here


'variables begin here
LED var PORTd.0 ' Alias PORTD.0 to LED
'end of variables

'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

'includes begin here
INCLUDE "modedefs.bas"
'end of includes

'Main code begins here

Pause 2000 ' Wait for LCD to startup

test:
lcdout $fe,1 'clear lcd screen
lcdout "hello world !"


high LED ' LED on
Pause 1000 ' Delay for .5 seconds
low led 'LED off
Pause 1000 ' Delay for .5 seconds
goto test
end


I used an LED on port D.0 as you can see, change the port to where your LED is connected. Get 1 blinking first then start playing with more :-)

Schematic here
[/HTML]http://www.picbasic.co.uk/forum/showthread.php?t=12179[/HTML]
All the best ..good luck

Kind regards

Dennis

Dennis
- 29th November 2009, 01:58
Hi jellis

I have altered the code to suit your LCD.


'*************************************
'Keypress display on LCD and TX to wherever
'*************************************

'Ocsillator selections here
OSCCON = $70 'Int CLK 8MHz
OSCTUNE.6 = 1 'PLL 4x
ADCON1= %00001111 '$0F = disable A/D converter
cmcon = 7
INTCON2.7 = 0 'switch pull-ups ON
'END of oscillator selections
'timer/oscillator defines
DEFINE OSC 32 '4x 8MHz
'END of timer/oscillator defines

'Port IO directions and presets for port pins begin here
'TRISX = %76543210 << tris bit order numbering
'TRISA = %11111111 'All pins are inputs
' Define port pins as inputs and outputs ...
TRISA = %00000000 'example only - TRISB = %00001111 ;Make B4-B7 outputs, B0-B3 inputs,
TRISB = %00000000 'all output
TRISC = %10010000 'C4 and C7 input
TRISD = %00000000 'all output
TRISE.0 = 0 'output
TRISE.1 = 0 'output
TRISE.2 = 0 'output
'End of Port IO directions and presets for port pins begin here


'variables begin here
LED var PORTd.0 ' Alias PORTD.0 to LED
'end of variables

'LCD defines begin here
DEFINE LCD_BITS 4 'defines the number of data interface lines (4 or 8)
DEFINE LCD_DREG PORTB '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 PORTB '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 PORTB '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

'includes begin here
INCLUDE "modedefs.bas"
'end of includes

'Main code begins here

Pause 2000 ' Wait for LCD to startup

test:
lcdout $fe,1 'clear lcd screen
lcdout "hello world !"


high LED ' LED on
Pause 1000 ' Delay for .5 seconds
low led 'LED off
Pause 1000 ' Delay for .5 seconds
goto test
end


Please make sure you have an LED wired to PORTD.0 or change the variable in your code to suit your setup.

Apparently most LCD issues are because of bad wiring (I can vouch for it, I messed up many a time too)
So please check and double check your wiring.
Probably easiest is to follow the schematic i referred you to and just substitute (or even better just redraw it) to suit your PIC

Also check out the diagram here which I made and has proved very useful as a reference for my project and experiments.

Good luck

Kind regards
Dennis

jellis00
- 29th November 2009, 02:03
Do you see blocks on the LCD ?
Yes, I do see the blocks when I turn the contrast up. Does that give any hints as to what is going wrong?


What's this line in your code?
'DEFINE SHOWDITIAL 1'
shouldn't it be showdigital ?
Yes...my typo...which I have fixed.

Next .. which port is your LCD connected to ?
If it's on PORTB like your code shows then your TRIS statement is set in the wrong direction.
You have
TRISB =%11111111 which should actually be
TRISB=%00000000 since you are sending to the LCD so the port should be set for output.
A nice way to remember it is that the number one looks like an i (I) which stands for Input and a zero (0 ) stands for output :-)
I knew better and must have had a senior moment....but fixed this.


Your problems could be a few things .. like a pause 1000 ( 1 second delay) before your code , giving time for the LCD to 'warm up' or initialize and so forth.
Changed it to 1000.

After all of above changes it still doesn't display and info.


Here's my code
I will take a close look at your code versus mine and see if I can figure out where I went wrong and get back to you. Thanks for your inputs!

Darrel Taylor
- 29th November 2009, 02:54
The below rather simple code will not operate a 2x16 LCD display that is installed in my EasyPic6 with a 18F4550 MCU ...


DEFINE LCD_DREG PORTB ' Use PortB for LCD Data
DEFINE LCD_DBIT 4 ' Use lower(4) 4 bits of PortB
' PORTB.0 thru PORTB.3 connects to
' LCD DB4 thru LCD DB-7 respectively
DEFINE LCD_RSREG PORTB ' PORTB for RegisterSelect (RS) bit
DEFINE LCD_RSBIT 4 ' PORTB.4 pin for LCD's RS line
DEFINE LCD_EREG PORTB ' PORTB for Enable (E) bit
DEFINE LCD_EBIT 5 ' PORTB.5 pin for LCD's E line


Double check your LCD connections.
It looks like your data bus and the RS/E are on the same pins.
<br>

jellis00
- 29th November 2009, 03:18
Double check your LCD connections.
It looks like your data bus and the RS/E are on the same pins.
<br>

That was the problem, Darrel! I must have checked the LCD DEFINES against page 108 of the PBP User Manual a half dozen times and still missed this.

BTW...ignore a posting I did on your ALLDIGITAL.pbp thread...another stupid mistake that I found...I mispelled DIGITAL. Must be really tired after all the frustration today
Thanks so much...I will be able to sleep tonight after all..

Dennis
- 29th November 2009, 10:02
DEFINE senior moment ?


Glad it's working :-)

jellis00
- 29th November 2009, 19:48
DEFINE senior moment ?


Senior moment = when someone over 60 yrs age does something stupid due to temporary lack of judgement or insight ;>)