LCD PIC18f4550 help

Thread Starter

Alanayala

Joined Apr 11, 2013
2
Hi, i have been triying to initiate the lCD with the pic18f4550 but i can´t. I dont have and idea.
I paste mi code, if anyone can help. Thanks
Code:
#include <p18f4550.h>
#define E  PORTAbits.RA5
#define RS PORTAbits.RA4
#define D7 PORTAbits.RA0
#define D6 PORTAbits.RA1
#define D5 PORTAbits.RA2
#define D4 PORTAbits.RA3

#define Clean_Display    0x01
#define Go_Home        0x02
#define Autoshift_right    0x07
#define Autoshift_left    0x05
#define All_On        0x0F
#define Display_On    0x0C
#define Cursor_On    0x0E
#define _8Bit_2Lines    0x38
#define _4Bit_2Lines    0x28
#define _4bit_10dots    0x2C
#define DDRAM_Home    0x80 //move cursor display
#define DDRAM_Line2    0xA8 //move cursor display low bit

unsigned char line = 0;



void wait_ms(int tiempo_ms) //Función de esperar un segundo.
{
    int temp;
    int x;
    temp=tiempo_ms;
    x=0; //variable utilizada para el tiempo
//    INTCON2=0b00000100; //Option - configuracion del preescaler
//    T0CON=0b00000001;
    do
        {
        TMR0=6;         do{x=x+1;} while (x<550);
        x=0;
        temp=--temp;
        }
    while (temp!=0);
}


void LCD_Write_Nibble(unsigned char data) {
    if (data & 8) {
        D7 = 1;
    } else {
        D7 = 0;
    }
    if (data & 4) {
        D6 = 1;
    } else {
        D6 = 0;
    }
    if (data & 2) {
        D5 = 1;
    } else {
        D5 = 0;
    }
    if (data & 1) {
        D4 = 1;
    } else {
        D4 = 0;
    }
    wait_ms(10);
    E = 1;
    wait_ms(20);
    E = 0;
}


void LCD_Write_Byte (char data)
{
    LCD_Write_Nibble(data >> 4);
    LCD_Write_Nibble(data);
}

/*************************************************************************************************/
/*********************                  exported Functions                    **********************/
/*************************************************************************************************/

void LCD_Init(void) { 
    D4=0;
    D5=0;
    D6=0;
    D7=0;
    wait_ms(20);
    E = 0;
    RS = 0;
    LCD_Write_Nibble(0x30);
    wait_ms(30);
    LCD_Write_Nibble(0x30);
    wait_ms(30);
    LCD_Write_Nibble(0x30);
    wait_ms(30);
    LCD_Write_Nibble(0x20);
    LCD_Write_Byte(_4Bit_2Lines);     // Configure LDC for 4 bits
    LCD_Write_Byte(Clean_Display);    // Clean display
    LCD_Write_Byte(Cursor_On);        // Turn on display, cursor
    LCD_Write_Byte(DDRAM_Home);        // Set the DDRAM position to
}

void LCD_Skip_Line(void) {
    RS = 0;
    if(line) {
        LCD_Write_Byte(DDRAM_Home);
        line = 0;
    } else {
        LCD_Write_Byte(DDRAM_Line2);
        line = 1;
    }
}

char LCD_Get_Line() {
    return line;
}

void LCD_Write_String(char data) {
    RS = 1;
    LCD_Write_Byte(data);
}


unsigned char mensaje[]={"Hola mundo"};

void main (void){
ADCON1=0x0F;
wait_ms(200);
unsigned char i;
    TRISD=0;
    TRISB=0;
    D4=0;
    D5=0;
    D6=0;
    D7=0; 
    E=0;
    RS=0;
    wait_ms(5);
    LCD_Init();
    wait_ms(5);

        i=0;
       do
    {
        LCD_Write_String(mensaje[i++]);
    } while (mensaje[I]!=0);
do{}
while(1);
}
 
Last edited by a moderator:

ErnieM

Joined Apr 24, 2011
8,053
Assuming the "lCD" means an alphanumeric display...

These devices must be initialized before you use them, and you can't do that before they complete their internal init stuff.

A very common error is to have the required delays during set up too short.

This task is very tricky and probably best not to be attempted until you are more proficient at C. There is lots of sample code on the net to copy, (some good, some bad).

The one section of code I noted will fail is
Code:
do
{
   LCD_Write_String(mensaje[i++]);
} while (mensaje!=0);
as "while (mensaje!=0)" compares the address of the array to zero

This is what you meant:
Code:
do
{
   LCD_Write_String(mensaje[i++]);
} while (mensaje[i]!=0);
And this is how I would write the same thing with a "while" loop as opposed to a "do...while" loop:
Code:
while (mensaje[i]) LCD_Write_String(mensaje[i++]);
 

MrChips

Joined Oct 2, 2009
21,679
The bracketed 'I' got interpreted as italics tag. Next time use code tags.

Why not create a better LCD_Write_String( ) function that does the job:

Code:
void LCD_Write_String(char *s)
  {
    RS = 1;
    while(*s) LCD_Write_Byte(*s++);
  }
 

MrChips

Joined Oct 2, 2009
21,679
Here is my LCD initialization sequence from my blog:

Code:
void LCD_init(void)
{
  RS = 0;
  LCD_Write_Byte(0x28); // dual line, 4 bits
  LCD_Write_Byte(0x06); // increment mode
  LCD_Write_Byte(0x0C); // cursor turned off
  LCD_Write_Byte(0x10); // cursor right
  LCD_Write_Byte(0x01); // clear display
}
 
Top