Initialising HD44780 LCD with p18f252 and MPLAB

Thread Starter

richie244

Joined Apr 30, 2009
1
I am currently do a project where I want to display a volatge on an the 20x4 LCD display.

I currently have an 8 bit binary comming out of the Pic and have checked this using a logic probe.

The first problem I ma having is when I attach the outputs from the Pic to the LCD they all go logic High with nothing showing on the display do I need to add a resistor to a circuit before it goes to the LCD display?

I am aware I need to initialise the LCD and have written a program to do so but I am not sure if it is correct. I have had trouble posting a copy of my code but will try. All I want to do is to be able to control the display even being able to display an "A" would give me a warmer feeling.


This is the Code I have been trying. I havent included the A/d code because, as you can see it is Very long as it is. :)


/*****************************************
* Display
/*****************************************/
#define lcd_rs PORTCbits.RC2
#define lcd_rw PORTCbits.RC3
#define lcd_e PORTCbits.RC4
#define lcd_ret 0x10
#define lcd_cmd_wri 0x00
#define lcd_data_wri 0x01
#define lcd_busy_rd 0x02
#define lcd_set_function 0x3C //00111100 8 bit interface, 4 line mode, 5x11 dot format
#define lcd_set_visible 0x0F //Display on, cursor underline on, cursor blink on
#define lcd_set_shift 0x16 //Cursor move, Right shift
#define lcd_set_mode 0x06 //Increment, display shift off
#define lcd_set_cgaddr 0x40
#define lcd_set_ddaddr 0x80
#define lcd_clr 0x01
#define lcd_init 0x30
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i,j;
const int Twentyms = 640000;
const int Tenms = 320000;
const int Onetwofivems = 4000000;
const int Twohundredus = 6400;
const int Onehundredus = 3200;
const int Sixtyfourms = 2048000;

void delay(int);
void WrCmd2Lcd(char cmd);
void WrDat2Lcd(char data);
void InitLcd();
void Speech(char words,int position);
void LcdWait();
void LcdPutPar(int par);

void LcdEnable()
{
lcd_e=1;
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
lcd_e=0;
}
void WrCmd2Lcd(char cmd)
{
TRISB = 0x00;
lcd_rw=0;
lcd_rs=1;
PORTB=cmd;
LcdEnable();
}
void WrDat2Lcd(char data)
{
TRISB =0x00;
lcd_rw=1;
lcd_rs=0;
PORTB = data;
LcdEnable();

}

void InitLcd()
{
lcd_e=0;
j=Onetwofivems;
for (i = 0; i < j; i++); //delay for 125 ms
WrCmd2Lcd(lcd_init);
j=Twentyms;
for (i = 0; i < j; i++); //delay for 20ms
LcdEnable();
j=Twohundredus;
for (i = 0; i < j; i++); //delay for 200us
LcdEnable();
j=Onehundredus;
for (i = 0; i < j; i++); //delay for 100us
WrCmd2Lcd(lcd_set_function);
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
WrCmd2Lcd(lcd_set_visible);
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
WrCmd2Lcd(lcd_clr);
j=Onehundredus;
for (i = 0; i < j; i++); //delay for 100us
WrCmd2Lcd(lcd_set_mode);
j=Tenms;
for (i = 0; i < j; i++); //delay for 10ms
WrCmd2Lcd(lcd_set_ddaddr);
j=Onehundredus;
for (i = 0; i < j; i++); //delay for 100us

PORTB = lcd_clr;
delay(1000);
PORTB = lcd_ret;
delay(4960000);
PORTB = lcd_set_mode;
delay(1280);
PORTB = lcd_set_visible;
delay(1280);
PORTB = lcd_set_shift;
delay(1280);
PORTB = lcd_set_function;
delay(1280);
PORTB = lcd_set_cgaddr;
delay(1280);
PORTB = lcd_set_ddaddr;
delay(1280);
PORTB = lcd_busy_rd;
delay(1280);
PORTB = lcd_data_wri;
delay(1280);
PORTB = lcd_cmd_wri;


}

void LcdWait()
{
int status;
TRISB=0xFF;
TRISB = lcd_busy_rd;
do
{
lcd_e=1;
status = ADCON1;
lcd_e=0;
}
while(status & 0x80); // test busy flag.
}

void Speech(char words,int position)
{
char *textptr;
//textptr = words;
WrCmd2Lcd(position);
LcdWait();
j=Sixtyfourms;
for (i = 0; i < j; i++); //delay for 64ms
do
{
WrDat2Lcd(*textptr);
*textptr++;
}
while(*textptr != '\n');
}

void LcdPutPar(int par)
{
TRISBbits.TRISB0 = PORTBbits.RB0,par & 0X01;
TRISBbits.TRISB1 = PORTBbits.RB1,par & 0X02;
TRISBbits.TRISB2 = PORTBbits.RB2,par & 0X04;
TRISBbits.TRISB3 = PORTBbits.RB3,par & 0X08;
TRISBbits.TRISB0 = PORTBbits.RB4,par & 0X10;
TRISBbits.TRISB1 = PORTBbits.RB5,par & 0X20;
TRISBbits.TRISB2 = PORTBbits.RB6,par & 0X40;
TRISBbits.TRISB3 = PORTBbits.RB7,par & 0X80;
}

void main(void) {
// unsigned char voltage1, voltage2;
while(1){
// voltage1 = readADC(0);
// voltage2 = readADC(1);
//InitLcd();
// WrCmd2Lcd(0X06);
// LcdEnable();

//clear display
lcd_e = 1;
lcd_rs = 1;
lcd_rw = 0;

WrDat2Lcd(00100001);
// LcdWait();
while(1);
}
}



void delay(int x){
int i=0;
for(i=0; i<= x; i++);


}
 
Top