Cómo mostrar variables en LCD usando el compilador PIC18 y XC8

0

Estoy intentando mostrar una variable en un LCD. Estoy usando un PIC18F4550 y estoy compilando con XC8 . Mi código es el siguiente:

#include<pic18f4550.h>              //----Include Reg file of Pic18f4550

#define lcd PORTD                   //----Assing PORTD as lcd

#define rs PORTCbits.RC0            //----Register Select Pin for Control of LCD
#define rw PORTCbits.RC1            //----Read/Write Pin for Control of LCD
#define en PORTCbits.RC2            //----Enable Pin for Control of LCD

#include "lcd.h";
#include "stdlib.h";


//////////// Main Program //////////////////////////////////////////////////////

void main()
{
    DDRD = 0x00;                    //----Set as output
    DDRC = 0x00;                    //----Set as output

    lcd_init();                     //----Initialize LCd

    char k = "p";

    while(1)                        //-----Creating Super Loop
    {
        lcd_cmd(0x80);               //----- First row in LCD
        lcd_msg("LCD Display"); //-----Data
        lcd_cmd(0xC0);               //-----Last lacation of 1st Line
        lcd_msg("Variable:");
        lcd_msg(k);

        delay(250);                 //-----250 msec delay
    }
}

y estoy usando el siguiente encabezado lcd.h :

////// Proto-Type Decleration //////////////////////////////////////////////////

void lcd_cmd(unsigned char x);      //----Function to Send Command to LCD
void lcd_dwr(unsigned char x);      //----Function to Send Data to LCD
void lcd_msg(unsigned char *c);     //----Function to Send String of Data to     LCD
void lcd_lat();                     //----Function to Latch data into LCD
void lcd_ready();                   //----Function to Check LCD is Busy

void lcd_init();                    //----Initialization of LCD

void delay(unsigned int ms);        //----Delay Function for 1ms

////////////////////////////////////////////////////////////////////////////////

void lcd_cmd(unsigned char x)
{
    lcd_ready();                  //----To Check whether lcd is busy
    lcd = x;                      //----8-Bit Command is Send to PORTD
    rs = 0;                       //----Register Select Pin is Low => Command Register
    rw = 0;                       //----Read/Write Pin is Low => Write.
    lcd_lat();                    //----Latch data into LCD
}

void lcd_dwr(unsigned char x)
{
    lcd_ready();                  //----To Check whether lcd is busy
    lcd = x;                      //----8-Bit Data is Send to PORTD
    rs = 1;                       //----Register Select Pin is High => Data Register
    rw = 0;                       //----Read/Write Pin is Low => Write.
    lcd_lat();                    //----Latch data into LCD
}

void lcd_msg(unsigned char *c)
{
    while(*c != 0)              //---Check till last data is send
        lcd_dwr(*c++);          //---Send data to lcd and increment
}

void lcd_lat()                    //----To Latch data into LCD
{
    en = 1;                       //----Enable Pin goes high
    delay(1);                     //----delay of 1ms
    en = 0;                       //----Enable Pin goes Low
}

void lcd_ready()
{
    lcd = 0xFF;                   //----PORTD is High
    lcd &= 0x80;                  //----D7 is set as high
    rs = 0;                       //----Command Register is Selected
    rw = 1;                       //----Read/Write Pin is High => Read
    en = 0; delay(1); en = 1;     //----Low to High to read data from LCD
    if(lcd == 0x80)
    {
        en = 0; delay(1); en = 1;     //----Low to High to read data from LCD
    }
    else
    {
        //---Do nothing.
    }
}

void lcd_init()
{
    lcd_cmd(0x38);              //----8-bit data and 16x2 line
    lcd_cmd(0x0E);              //----Cusor Blinking
    lcd_cmd(0x01);              //----Clear LCD
    lcd_cmd(0x06);              //----Increment Cusor
    lcd_cmd(0x80);              //----1st low of 1st row
}

void delay(unsigned int ms)
{
    unsigned int i,j;
    for(i=0;i<=120;i++)         //---To generate 1ms delay
        for(j=0;j<=ms;j++);     //---To generate user define delay
}

Pero no soy capaz de mostrar el carácter "p"

Soy nuevo en las fotos. Agradecería cualquier sugerencia, gracias

    
pregunta Delfin

1 respuesta

1

El problema es que la rutina lcd_msg() quiere un char * , pero en su lugar le está pasando un solo char . Puede convertir su variable en una cadena terminada en NUL como esta:

char *k = "p";
//  other code
lcd_msg(k);

O puedes mostrar un solo carácter así:

char k = 'p';  // note use of single quotes for single char
// other code
lcd_dwr(k);
    
respondido por el Edward

Lea otras preguntas en las etiquetas