Simple LCD code not working!...WHY?
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?
Code:
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
1 Attachment(s)
code for your LCD setup ;-)
Hi jellis
I have altered the code to suit your LCD.
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 = %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