0
\$\begingroup\$

I am trying to display a variable in an LCD. I am using a PIC18F4550 and I am compiling with XC8. My code is the next:

#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
    }
}

and I am using the next lcd.h header:

////// 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
}

But I am not capable to display the character "p" enter image description here

I am new on pics. I would appreciate any suggestions, thanks

\$\endgroup\$
  • \$\begingroup\$ Are the other parts of your LCD code working? Is it just the variable that isn't being displayed? If so, is it displaying anything for the variable? A char is just a single character, so you should assign it like this char k = 'p';, using single quotes. Double quotes are for strings. \$\endgroup\$ – DigitalNinja Feb 7 '17 at 1:26
  • \$\begingroup\$ Yes, the other parts are working, I can even create and display special characters. I tried to use single quotes too. But I obtain the same result as the image I uploaded \$\endgroup\$ – DieDauphin Feb 7 '17 at 1:37
  • \$\begingroup\$ It looks like lcd_msgwants a char*. If you want to display a single character try using lcd_dwr instead. So keep char k = 'p'; (single quotes), and call lcd_dwr(k); instead of lcd_msg(k);. \$\endgroup\$ – DigitalNinja Feb 7 '17 at 2:00
1
\$\begingroup\$

The problem is that the lcd_msg() routine wants a char *, but you're passing it a single char instead. You could either turn your variable into a NUL terminated string like this:

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

Or you could display a single character like so:

char k = 'p';  // note use of single quotes for single char
// other code
lcd_dwr(k);
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.