3

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++);
    }
}
artless noise
  • 21,212
  • 6
  • 68
  • 105
James
  • 356
  • 2
  • 13
  • 1
    Can't read your code. – Iharob Al Asimi Jan 13 '16 at 16:00
  • In what way? Have I excluded some important aspects? Incorrect formatting? – James Jan 13 '16 at 16:02
  • Yes, 0 indentation and one-line `if` statements are also hard to read. – Iharob Al Asimi Jan 13 '16 at 16:02
  • Well, not really if you want to read it. Indent is a nice thing nevertheless and helps us to help you @James. – 4pie0 Jan 13 '16 at 16:04
  • I can only assume "uch" is supposed to be typedef for unsigned char, even though I don't see TZ declared anywhere. The thing is, if you're using WriteString to print the temperature, shouldn't it be converted to a string first? – doppelheathen Jan 13 '16 at 16:06
  • 1
    `writeString` want as null terminated string, is quite clear..... You are passing a `uch` var, that I guess is an `unsigned char`. I don't know your framework (Microchip uP I guess), but take a look at [THIS](http://www.tutorialspoint.com/c_standard_library/c_function_sprintf.htm) – LPs Jan 13 '16 at 16:06
  • @James what is the device you are using? – 4pie0 Jan 13 '16 at 16:10
  • I am using a Q200 PIC board with a PIC16F877A, with a HiTech C compiler @tinky_winky. The writeString function is of my own making, currently looking into using itoa() to change the unsigned char into ASCII – James Jan 13 '16 at 16:19
  • this should do the thing – 4pie0 Jan 13 '16 at 16:22
  • Aside: The line `TZ/100;` will compile but is non-productive code (has no effect). Should be `TZ/=100;` ? – Weather Vane Jan 13 '16 at 16:26
  • It does indeed, much appreciated folks! Odd that I would forget that this is actually similar to an error i tackled a month ago and forgot about. Thanks for your time. – James Jan 13 '16 at 16:29
  • 1
    PS don't ignore compiler warnings! – Weather Vane Jan 13 '16 at 16:30
  • *PS don't ignore compiler warnings!* Indeed. When the authors of the compiler you're using to turn your source code into a runnable binary think you did something sketchy, **you need to listen to them**. – Andrew Henle Jan 13 '16 at 16:37
  • this loop: `while(*stringToLcd > 0) { writeChar(*stringToLcd++); }` would be better written as: `for(int i =0; stringToLcd[i]; i++) { writeChar(stringToLcd[i]); }` – user3629249 Jan 15 '16 at 17:48
  • the calculations involving the `TX` variable are expected to result in a BCD value. However, that is not the case. For instance, if only the 0x40 bit were set, then `wd` is set to 2500 which is 0x94C which is not a valid BCD value. This is because valid BCD nibbles can only contain 0b0000 through 0b1001 (0...9). and 0x9C4 is 0b0000, 0b1001, bad value, 0b0100. Also addition of BCD values cannot be perform via `wd = wd+2500;` but take specific code algorithms. – user3629249 Jan 15 '16 at 18:15

3 Answers3

2

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.

Danny_ds
  • 11,201
  • 1
  • 24
  • 46
  • I've added details of my write functions as I stupidly left them out, as shown my writeString function actually takes a pointer to characters, so i'm not sure if thats the fix I'm looking for – James Jan 13 '16 at 16:23
  • @James - Well, you certainly can't provide a `uch` as a pointer. You'll need to convert that value to a string first, perhaps by using `itoa()`. – Danny_ds Jan 13 '16 at 16:25
  • I take it back, upon re-reading, this is EXACTLY the fix I was looking for. All summed up into a neat post. Much appreciated for the prompt answer. – James Jan 13 '16 at 16:29
1

I could suggest you to implement a new function

void writeUCH(uch value)
{
    unsigned char test = (value >= 100) ? 100 : (value >= 10) ? 10 : 1;

    while(test > 0)
    {
        writeChar((value/test)+'0');

        value = value%test;
        test /= 10;
    }
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
LPs
  • 16,045
  • 8
  • 30
  • 61
0

this line:

TZ/100;

will result in no change to TZ

what you really want is this:

TZ = TZ%100;

the value returned from get_temp() is an integer, not a ascii string. I would expect the LCD needs ascii characters, not the binary value of the bytes of an int variable.

user3629249
  • 16,402
  • 1
  • 16
  • 17