Continue to Site

Welcome to

Welcome to our site! is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] PIC18F4680 LCD not working.

Not open for further replies.


Junior Member level 1
Feb 21, 2007
Reaction score
Trophy points
Activity points
Hi all,

I am having a PIC18f4680 with which I am trying to write on LCD. I am using the xLCD library and has just modified the portB to PORTD. Created a new header xLCDMod.h and copied all the XLCD.h contents to that and modified the port to PORTD. Also added copy of all the *.c files of the XLCD in the project and made the reference to xLCDMod.h and compiled.

Now that I am not able to make the LCD display the actual items that I am writing to. I am sure that the PORT change is not causing the issue as some characters are shown and is kind of scrambled and displayed in some random orders. Sometimes only 1 character is shown. I tried out many config setting but nothing worked. I am using a PIC dev board, with 20 MHz crystal.

I am not able to fully understand whether these issues are because of the crystal config or not. Can someone please help me . I am posting the code .

I have removed the config bits for now and just kept only 3 . What all config bits to be set just for the LCD ?

#include "XLCDMod.h"// XLCD header customized for PORTD

// PIC18F4680 Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#pragma config OSC = HS         // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = BOHW     // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (VBOR set to 2.1V)

#pragma config WDT = ON         // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config BBSIZ = 1024     // Boot Block Size Select bits (1K words (2K bytes) Boot Block)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)

#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)

#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)

#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

#define _XTAL_FREQ=20000000

void init_XLCD();
void DelayFor18TCY();
void DelayPORXLCD();
void DelayXLCD();

void main() {

    while (BusyXLCD());

    while (1) {

void init_XLCD() {
    while (BusyXLCD());
    while (BusyXLCD());

void DelayFor18TCY() {


void DelayPORXLCD() {
    Delay1KTCYx(75); // Delay of 15ms
    // Cycles = (TimeDelay * Fosc) / 4
    // Cycles = (15ms * 20MHz) / 4
    // Cycles = 75,000

void DelayXLCD() {
    Delay1KTCYx(25); // Delay of 5ms
    // Cycles = (TimeDelay * Fosc) / 4
    // Cycles = (5ms * 20MHz) / 4
    // Cycles = 25,000

Could you post your customized version of the xLCDMod.h header file?

When reviewing your configuration register settings, I noticed you have the watchdog timer enable, however I do not see any indication you've attempted to "kick the dog" directly in main(). Are you servicing the watchdog timer periodic routine call? Typically, during the majority of code development the watchdog timer is disable.

Also, have you run simulations using the stopwatch feature to test your implementations of the required delay routines produce reasonable delay durations.

You may also want to output a series of test pattern strings to LCD, consisting of consecutive alphanumeric characters to test both the LCD and connections.


Thanks for the reply
I am posting the project as a zip file. Also I have tested the same in proteus and it is working fine.
I am having some issues doubts here, I have the 20MHz crystal in my development board and this code is written considering that in mind.
But when I set the PIC Processor Frequency in the pic properties to 20Mhz , it is not working. but when I set the frequency to 4 MHz in the properties in the proteus it is working.

Can you please help me to figure out the problem. I have spent enough time in this :(

In the LCD in the development board, I am seeing only 3 characters and it starts from second position in line 1

Code is attached below.


  • ProjectUSARTLCD18F.X.7z
    185.4 KB · Views: 34
Last edited by a moderator:

The first task is to disable the watchdog timer in the configuration register settings:

#pragma config WDT = [COLOR="#FF0000"]OFF[/COLOR]         // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

If you wish to fully implement a watchdog timer in the final application, then make you typically make it one of the last items on the "TODO" list.

In the future, please use the "Manage Attachment" feature to upload files, rather than external file sharing services.


Thank you. I was not having the permission to upload as attachment . that is why i uploaded to file sharing.

yes, I have disabled the WDT and even then it has not difference.
Only in proteus it works fine where as in actual development board it is not.

Might not be the problem but:
- drop the includes for stdio.h and stdlib.h - they might have a place in programming a PC etc but are almost never needed for an embedded system
- put the include for xc.h first - this is used to set things up for the rest of the program
What is the development board you mention? Is it a purchased one such as an Explorer 16 or is it a breadboard with your own crystal and capacitors etc?
If the latter then have you connected power and ground to both sets of pins, are there bypass capacitors in place, what size capacitors do you have on the crystal? These are some of the "common" problems people have that cause instability. One other big thing is the capacitance between tracks on a breadboard - you may be better off using a 5MHz crystal and the 4x PLL if you really need a clock speed of 20MHz as experience has shown that 20MHZ crystals in a breadboard can be hard to get going properly.

Not open for further replies.

Part and Inventory Search

Welcome to