aj9999
Junior Member level 3
Simple 4-bit LCD help
Hi, Im new to the PIC world and i am trying to get the LCD working on a PICDEM 2+ board with a pic1854520 using the C18 compiler. The code below doesnt work and I believe it has to do with breaking the 8bit character into two nibbles. I have searched numerous examples online but i cant seem to get any of them working or they are too complex for me to modify. If anyone can point out my mistake i would appreciate it.
#include <p18f4520.h>
#include <delay.h>
int result;
void LCD_init(void);
void LCD_cntrl(char);
void LCD_data(char);
#define LCD_DATA PORTD
#define LCD_RW PORTDbits.RD5
#define LCD_RS PORTDbits.RD4
#define LCD_E PORTDbits.RD6
#define LCD_PWR PORTDbits.RD7
unsigned char temp_l=0, temp_h=0;
void main()
{
TRISD = 0x00;
TRISB = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTB = 0x00;
PORTD = 0x00;
LCD_init();
while(1)
{
LCD_data("a");
PORTBbits.RB1 = 1; //test to see if code is running
}
}
void LCD_init(void)
{
LCD_PWR = 1;
msdelay(1);
LCD_cntrl(0x20); // sets 4 bit mode
msdelay(5);
LCD_cntrl(0x28); //display shift
LCD_cntrl(0x0f); //display on with blink
LCD_cntrl(0x02); //set cursor position to zero
}
void LCD_cntrl(char x)
{
LCD_RW=0;
LCD_RS=0;
temp_l = x & 0x0f;
temp_h = x>>4;
LCD_E=1;
LCD_DATA= PORTD | temp_l;
msdelay(1);
LCD_E=0;
msdelay(1);
LCD_E=1;
LCD_DATA= PORTD | temp_h;
msdelay(1);
LCD_E=0;
msdelay(1);
}
void LCD_data(char x)
{
LCD_RW=0;
LCD_RS=1;
temp_l = x & 0x0f;
temp_h = x>>4;
LCD_E=1;
LCD_DATA = PORTD | temp_l;
msdelay(1);
LCD_E=0;
msdelay(1);
LCD_E=1;
LCD_DATA = PORTD | temp_h;
msdelay(1);
LCD_E=0;
msdelay(1);
}
Hi, Im new to the PIC world and i am trying to get the LCD working on a PICDEM 2+ board with a pic1854520 using the C18 compiler. The code below doesnt work and I believe it has to do with breaking the 8bit character into two nibbles. I have searched numerous examples online but i cant seem to get any of them working or they are too complex for me to modify. If anyone can point out my mistake i would appreciate it.
#include <p18f4520.h>
#include <delay.h>
int result;
void LCD_init(void);
void LCD_cntrl(char);
void LCD_data(char);
#define LCD_DATA PORTD
#define LCD_RW PORTDbits.RD5
#define LCD_RS PORTDbits.RD4
#define LCD_E PORTDbits.RD6
#define LCD_PWR PORTDbits.RD7
unsigned char temp_l=0, temp_h=0;
void main()
{
TRISD = 0x00;
TRISB = 0x00;
TRISD = 0x00;
TRISE = 0x00;
PORTB = 0x00;
PORTD = 0x00;
LCD_init();
while(1)
{
LCD_data("a");
PORTBbits.RB1 = 1; //test to see if code is running
}
}
void LCD_init(void)
{
LCD_PWR = 1;
msdelay(1);
LCD_cntrl(0x20); // sets 4 bit mode
msdelay(5);
LCD_cntrl(0x28); //display shift
LCD_cntrl(0x0f); //display on with blink
LCD_cntrl(0x02); //set cursor position to zero
}
void LCD_cntrl(char x)
{
LCD_RW=0;
LCD_RS=0;
temp_l = x & 0x0f;
temp_h = x>>4;
LCD_E=1;
LCD_DATA= PORTD | temp_l;
msdelay(1);
LCD_E=0;
msdelay(1);
LCD_E=1;
LCD_DATA= PORTD | temp_h;
msdelay(1);
LCD_E=0;
msdelay(1);
}
void LCD_data(char x)
{
LCD_RW=0;
LCD_RS=1;
temp_l = x & 0x0f;
temp_h = x>>4;
LCD_E=1;
LCD_DATA = PORTD | temp_l;
msdelay(1);
LCD_E=0;
msdelay(1);
LCD_E=1;
LCD_DATA = PORTD | temp_h;
msdelay(1);
LCD_E=0;
msdelay(1);
}