无法从温度计字节读取

时间:2016-01-13 16:00:30

标签: c byte pic lcd embedded-control

下午所有,

如果这个问题的格式错误或位置错误,请告知道歉,如果是这种情况,请标记,我会更改或将其转移到别处。

我正在使用开发板将温度读数发送到LCD面板,我真的很难理解为什么程序运行时的温度没有打印到我的LCD上。很多代码来自给我的框架,据我所知是正确的。

我的问题源于这些功能:

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

我已修改此函数,以便它应返回温度整数(unsigned char TZ)

此处调用此函数:

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

}

在“MAIN MENU”之后没有打印任何内容,这显然意味着我做错了什么。我可以根据要求提供进一步的澄清/代码。

我应该提一下,我不仅仅是在寻找一个“粘贴它并且它会起作用”的答案。我理解我的错误以及如何解决它的任何反馈都非常感谢。

提前致谢!

编辑:

有些人询问我的写作功能,所以为了进一步说明,我会将它们粘贴在这里:

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++);
    }
}

3 个答案:

答案 0 :(得分:2)

Tempunsigned char

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

因此,使用writeString()将产生未定义的结果。

您应该使用write()代替(取决于您使用的库)。

但您可能希望首先将get_temp()的返回值转换为ASCII字符串,并使用writeString()显示该字符串。

<强>更新

void writeString(char *stringToLcd)

此功能需要char*,因此您无法提供单个uch

您需要先使用Tempitoa()转换为字符串。

答案 1 :(得分:1)

我可以建议你实现一个新功能

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

答案 2 :(得分:0)

这一行:

TZ/100;

将导致TZ无法更改

你真正想要的是:

TZ = TZ%100;

get_temp()返回的值是整数,而不是ascii字符串。我希望LCD需要ascii字符,而不是int变量字节的二进制值。

相关问题