• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Interfacing LCD display to PORTC(PIC18f2431)

Not open for further replies.



I'm trying to use connect an LCD to PORTC of PIC18f2431. So far I have been only connecting LCD's to PORTB when using PICs which is assigned by default.

So I modified the contents of the "xlcd.h" file as this:

#ifndef __XLCD_H
#define __XLCD_H
#include "p18cxxx.h"
/* PIC18 XLCD peripheral routines.
*   Notes:
*      - These libraries routines are written to support the
*        Hitachi HD44780 LCD controller.
*      - The user must define the following items:
*          - The LCD interface type (4- or 8-bits)
*          - If 4-bit mode
*              - whether using the upper or lower nibble
*          - The data port
*              - The tris register for data port
*              - The control signal ports and pins
*              - The control signal port tris and pins
*          - The user must provide three delay routines:
*              - DelayFor18TCY() provides a 18 Tcy delay
*              - DelayPORXLCD() provides at least 15ms delay
*              - DelayXLCD() provides at least 5ms delay

/* Interface type 8-bit or 4-bit
* For 8-bit operation uncomment the #define BIT8
/* #define BIT8 */

/* When in 4-bit interface define if the data is in the upper
* or lower nibble.  For lower nibble, comment the #define UPPER
/* #define UPPER */

/* DATA_PORT defines the port to which the LCD data lines are connected */
#define DATA_PORT             PORTC
#define TRIS_DATA_PORT         TRISC

/* CTRL_PORT defines the port where the control lines are connected.
* These are just samples, change to match your application.
#define RW_PIN   LATCbits.LATC6           /* PORT for RW */
#define TRIS_RW  TRISCbits.TRISC6       /* TRIS for RW */

#define RS_PIN   LATCbits.LATC5           /* PORT for RS */
#define TRIS_RS  TRISCbits.TRISC5       /* TRIS for RS */

#define E_PIN    LATCbits.LATC4         /* PORT for D  */
#define TRIS_E   TRISCbits.TRISC4       /* TRIS for E  */

/* Display ON/OFF Control defines */
#define DON         0b00001111  /* Display on      */
#define DOFF        0b00001011  /* Display off     */
#define CURSOR_ON   0b00001111  /* Cursor on       */
#define CURSOR_OFF  0b00001101  /* Cursor off      */
#define BLINK_ON    0b00001111  /* Cursor Blink    */
#define BLINK_OFF   0b00001110  /* Cursor No Blink */

/* Cursor or Display Shift defines */
#define SHIFT_CUR_LEFT    0b00000100  /* Cursor shifts to the left   */
#define SHIFT_CUR_RIGHT   0b00000101  /* Cursor shifts to the right  */
#define SHIFT_DISP_LEFT   0b00000110  /* Display shifts to the left  */
#define SHIFT_DISP_RIGHT  0b00000111  /* Display shifts to the right */

/* Function Set defines */
#define FOUR_BIT   0b00101100  /* 4-bit Interface               */
#define EIGHT_BIT  0b00111100  /* 8-bit Interface               */
#define LINE_5X7   0b00110000  /* 5x7 characters, single line   */
#define LINE_5X10  0b00110100  /* 5x10 characters               */
#define LINES_5X7  0b00111000  /* 5x7 characters, multiple line */

#ifdef _OMNI_CODE_
#define PARAM_SCLASS auto

#ifndef MEM_MODEL
#ifdef _OMNI_CODE_
#define MEM_MODEL
#define MEM_MODEL far  /* Change this to near for small memory model */

/* OpenXLCD
* Configures I/O pins for external LCD
void OpenXLCD(PARAM_SCLASS unsigned char);

/* SetCGRamAddr
* Sets the character generator address
void SetCGRamAddr(PARAM_SCLASS unsigned char);

/* SetDDRamAddr
* Sets the display data address
void SetDDRamAddr(PARAM_SCLASS unsigned char);

/* BusyXLCD
* Returns the busy status of the LCD
unsigned char BusyXLCD(void);

/* ReadAddrXLCD
* Reads the current address
unsigned char ReadAddrXLCD(void);

/* ReadDataXLCD
* Reads a byte of data
char ReadDataXLCD(void);

/* WriteCmdXLCD
* Writes a command to the LCD
void WriteCmdXLCD(PARAM_SCLASS unsigned char);

/* WriteDataXLCD
* Writes a data byte to the LCD
void WriteDataXLCD(PARAM_SCLASS char);

/* putcXLCD
* A putc is a write
#define putcXLCD WriteDataXLCD

/* putsXLCD
* Writes a string of characters to the LCD
void putsXLCD(PARAM_SCLASS char *);

/* putrsXLCD
* Writes a string of characters in ROM to the LCD
void putrsXLCD(const char *);

/* User defines these routines according to the oscillator frequency */
extern void DelayFor18TCY(void);
extern void DelayPORXLCD(void);
extern void DelayXLCD(void);

I also included the files from " C:\Program Files (x86)\Microchip\xc8\v1.30\include\plib" to my project.

Then wrote this code that's supposed to display some simple text on the lcd:

#include <stdio.h>
#include <stdlib.h>
#include "header.h"
#include <plib/delays.h>
#include <plib/xlcd.h>

void init_XLCD(void);              //Initialize LCD display
void DelayFor18TCY( void );        //18 cycles delay
void DelayPORXLCD (void);          // Delay of 15ms
void DelayXLCD (void);             // Delay of 5ms

void main(void)
SCS1 = 0;
SCS0 = 0; // select primary oscillator

    init_XLCD();                    //Call the Initialize LCD display function

    putrsXLCD("Some");          //Display some text
    SetDDRamAddr(0x40);            //shift cursor to beginning of second line
    putrsXLCD("text");      // display some text



void init_XLCD(void)                //Initialize LCD display
OpenXLCD(FOUR_BIT&LINES_5X7);       //configure LCD in 4-bit Data Interface mode
                                    //and 5x7 characters, multiple line display
while(BusyXLCD());                  //Check if the LCD controller is not busy
                                    //before writing some commands
WriteCmdXLCD(0x06);                 //Move cursor right, don't shift display
WriteCmdXLCD(0x0C);                 //Turn display on without cursor

void DelayFor18TCY( void )         //18 cycles delay
void DelayPORXLCD (void)           //Delay of 15ms
void DelayXLCD (void)              //Delay of 5ms
The header.h is:
// PIC18F2431 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 = XT         // Oscillator Selection bits (XT oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = ON        // Internal External Oscillator Switchover bit (Internal External Switchover mode enabled)

#pragma config PWRTEN = OFF     // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled)
// BORV = No Setting

#pragma config WDTEN = OFF      // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDPS = 32768     // Watchdog Timer Postscale Select bits (1:32768)
#pragma config WINEN = OFF      // Watchdog Timer Window Enable bit (WDT window disabled)

#pragma config PWMPIN = OFF     // PWM output pins Reset state control (PWM outputs disabled upon Reset (default))
#pragma config LPOL = HIGH      // Low-Side Transistors Polarity (PWM0, 2, 4 and 6 are active-high)
#pragma config HPOL = HIGH      // High-Side Transistors Polarity (PWM1, 3, 5 and 7 are active-high)
#pragma config T1OSCMX = ON     // Timer1 Oscillator MUX (Low-power Timer1 operation when microcontroller is in Sleep mode)

#pragma config MCLRE = OFF      // MCLR Pin Enable bit (Disabled)

#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Low-Voltage ICSP Enable bit (Low-voltage ICSP enabled)

#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000200-000FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (001000-001FFF) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (002000-002FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (003000-003FFFh) not code-protected)

#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot Block (000000-0001FFh) 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 (000200-000FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (001000-001FFF) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (002000-002FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (003000-003FFFh) 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-0001FFh) 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 (000200-000FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (001000-001FFF) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (002000-002FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (003000-003FFFh) not protected from table reads executed in other blocks)

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

#define _XTAL_FREQ 25000000
This is how I connected the LCD:

When I run this code I don't get any text on the LCD. What am I doing wrong?
Not open for further replies.

Latest threads

EE World Online Articles