I am having some trouble when trying to use LCD
.
I'm using LCDHD44780
2x16 and PIC18F
with MPLAB
.
I think my code is good and I can compile the project but still nothing happens in the LCD when I try to use it to display a simple char.
What is wrong with following code:
#include "LCD_HD44780.h"
void LCD_HD44780_reset(void)
{
TRIS_D7 = 0;
TRIS_D6 = 0;
TRIS_D5 = 0;
TRIS_D4 = 0;
DATA_D7 = 0;
DATA_D6 = 0;
DATA_D5 = 0;
DATA_D4 = 0;
RW_PIN = 0; // R/W pin made low
RS_PIN = 0; // Register select pin made low
E_PIN = 0; // Clock pin made low
TRIS_RW = 0; // All control signals made outputs
TRIS_RS = 0;
TRIS_E = 0;
__delay_ms(15);
LCD_HD44780_sendCmd8bit(0x30);
__delay_ms(10);
LCD_HD44780_sendCmd8bit(0x30);
__delay_ms(10);
LCD_HD44780_sendCmd8bit(0x30);
__delay_ms(10);
}
void LCD_HD44780_init(void)
{
LCD_HD44780_reset();
// Enable 4 bit mode
LCD_HD44780_sendCmd8bit(0x20);
__delay_ms(10);
LCD_HD44780_sendCmd(0x28); //Function set: 4 bit interface
LCD_HD44780_sendCmd(0x0f); //Display on/off: display on, cursor off, blink off
LCD_HD44780_ClearScreen();
LCD_HD44780_sendCmd(0x06); //Entry mode: increment, no shift
}
void LCD_HD44780_sendCmd8bit(BYTE c)
{
RW_PIN = 0; // Set the control signals
RS_PIN = 0; // for sending a command
LCD_HD44780_writeNibble((c & 0xF0) >> 4); //send only 4 msb
}
void LCD_HD44780_sendCmd(BYTE c)
{
RW_PIN = 0; // Set the control signals
RS_PIN = 0; // for sending a command
LCD_HD44780_writeNibble((c & 0xF0) >> 4);
LCD_HD44780_writeNibble(c & 0x0F);
}
void LCD_HD44780_sendData(BYTE d)
{
RW_PIN = 0; // Set the control signals
RS_PIN = 1; // for sending data
LCD_HD44780_writeNibble((d & 0xF0) >> 4);
LCD_HD44780_writeNibble(d & 0x0F);
}
void LCD_HD44780_sendStr(BYTE *str)
{
while(*str != 0)
{
LCD_HD44780_sendData(*str++);
}
}
void LCD_HD44780_setCursor(BYTE row, BYTE col)
{
BYTE command;
BYTE add = row*0x40 + col;
command = 0x80 | add;
LCD_HD44780_sendCmd(command);
}
void LCD_HD44780_writeNibble(BYTE nb)
{
BYTE a;
if(nb & 0x08)
{
DATA_D7 = 1;
}
else
{
DATA_D7 = 0;
}
if(nb & 0x04)
{
DATA_D6 = 1;
}
else
{
DATA_D6 = 0;
}
if(nb & 0x02)
{
DATA_D5 = 1;
}
else
{
DATA_D5 = 0;
}
if(nb & 0x01)
{
DATA_D4 = 1;
}
else
{
DATA_D4 = 0;
}
E_PIN = 1;
for(a = 0; a < 4; a++);
E_PIN = 0;
for(a = 0; a < 4; a++);
}
In my .h file i have:
#define _XTAL_FREQ 20000000
#define RW_PIN LATCbits.LATC0 /* PORT for RW */
#define TRIS_RW TRISCbits.TRISC0 /* TRIS for */
#define RS_PIN LATCbits.LATC2 /* PORT for RS */
#define TRIS_RS TRISCbits.TRISC2 /* TRIS for RS */
#define E_PIN LATCbits.LATC1 /* PORT for E */
#define TRIS_E TRISCbits.TRISC1 /* TRIS for E */
#define DATA_D7 LATEbits.LATE7 // LAT for DISP 7
#define DATA_D7_RD PORTEbits.RE7 // PORT for DISP 7
#define TRIS_D7 TRISEbits.TRISE7 // TRIS for DISP 7
#define DATA_D6 LATEbits.LATE6 // LAT for DISP 6
#define DATA_D6_RD PORTEbits.RE6 // PORT for DISP 6
#define TRIS_D6 TRISEbits.TRISE6 // TRIS for DISP 6
#define DATA_D5 LATEbits.LATE5 // LAT for DISP 5
#define DATA_D5_RD PORTEbits.RE5 // PORT for DISP 5
#define TRIS_D5 TRISEbits.TRISE5 // TRIS for DISP 5
#define DATA_D4 LATEbits.LATE4 // LAT for DISP 4
#define DATA_D4_RD PORTEbits.RE4 // PORT for DISP 4
#define TRIS_D4 TRISEbits.TRISE4 // TRIS for DISP 4
#define LCD_HD44780_CursorHome() LCD_HD44780_sendCmd(0x02);
#define LCD_HD44780_ScrollLeft() LCD_HD44780_sendCmd(0x18);
#define LCD_HD44780_ScrollRight() LCD_HD44780_sendCmd(0x1E);
#define LCD_HD44780_ClearScreen() LCD_HD44780_sendCmd(0x01);
#define LCD_HD44780_CursorOff() LCD_HD44780_sendCmd(0x0C);
#define LCD_HD44780_ClearOn() LCD_HD44780_sendCmd(0x0E);
Bruno: Here is working tutorial for the same LCD with PIC18 series MCU Click Here
I also suggest to compare your hardware with this reference design.