Search code examples
cembeddedhardwarepiclcd

Can't extract an integer from a thermometer byte reading


Afternoon all,

Apologies if this question is in the wrong format or in the wrong place, if this is the case, please flag and I'll change it or take it elsewhere.

I am using a development board to send a temperature reading to an LCD panel and I am really struggling to comprehend as to why the temperature at the moment that the program is run isn't being printed onto my LCD. A lot of the code is from framework given to me and is correct as far as I can tell.

My question stems from these functions:

uch get_temp()
{
    int i;
    DQ_HIGH();
    reset();                              //reset,wait for  18b20 responsion
    write_byte(0XCC);                     //ignore ROM matching
    write_byte(0X44);                     //send  temperature convert command
    for(i=20;i>0;i--)
        {

            //display();                    //call some display function,insure the time of convert temperature
        }
    reset();                              //reset again,wait for 18b20 responsion
    write_byte(0XCC);                     //ignore ROM matching
    write_byte(0XBE);                     //send read temperature command
    TLV=read_byte();                      //read temperature low byte
    THV=read_byte();                      //read temperature high byte
    DQ_HIGH();                            //release general line
    TZ=(TLV>>4)|(THV<<4)&0X3f;            //temperature integer
    TX=TLV<<4;  //temperature decimal

    if(TZ>100) 
    {
        TZ/100; 
    }                   //not display hundred bit

    ge=TZ%10;                     //integer Entries bit
    shi=TZ/10;                    //integer ten bit
    wd=0;

if (TX & 0x80) 
    wd=wd+5000;

if (TX & 0x40) 
    wd=wd+2500;

if (TX & 0x20) 
    wd=wd+1250;

if (TX & 0x10)
    wd=wd+625;                //hereinbefore four instructions are turn  decimal into BCD code

    shifen=wd/1000;                          //ten cent bit
    baifen=(wd%1000)/100;                    //hundred cent bit
    qianfen=(wd%100)/10;                     //thousand cent bit
    wanfen=wd%10;                            //myriad cent bit
    NOP();
    return TZ;
}

I have modified this function so that it should return the temperature integer (unsigned char TZ)

This function is then called here:

void Init_lcd(void)
{
    ADCON1 = 0x07; //required setting of analog to digital

    uch Temp;

    TRISD = 0x00;
    TRISA1 = 0;
    TRISA2 = 0;
    TRISA3 = 0;

    writeCommand(0x0f);
    writeCommand(0x38); //set to two line mode
    clearDisplay();

    writeString("MAIN MENU");
    Temp = get_temp();
    writeString(Temp);
    writeCommand(0xC0); //change cursor line

}

It isn't printing anything after "MAIN MENU", which obviously means I'm doing something wrong. I can provide further clarification/code on request.

I should probably mention that I am NOT only simply looking for an answer of "paste this in and it'll work". Any feedback in which I understand my mistake and how to fix it is greatly appreciated.

Thanks in advance!

EDIT:

A few people are asking about my writing functions so for further clarification I'll paste them here:

void writeChar(unsigned char ch)
{
    lcd = ch;
    RS = 1;
    RW =0;
    E = 1;
    lcdDelay();
    E=0;
}

void writeString(char *stringToLcd)
{
    while(*stringToLcd > 0)
    {
        writeChar(*stringToLcd++);
    }
}

Solution

  • Temp is an unsigned char

    uch Temp;
    //...
    Temp = get_temp();
    writeString(Temp);
    

    So, using writeString() will produce undefined results.

    You should use write() instead (depending on the library you're using).

    But you probably want to convert the return value of get_temp() to an ASCII string first, and display that using writeString().

    Update:

    void writeString(char *stringToLcd)
    

    This function needs a char*, so you can't provide a single uch.

    You need to convert Temp to a string first, using itoa() for example.