找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2192|回復(fù): 9
打印 上一主題 下一主題
收起左側(cè)

基于stc12c5a32s2單片的18b20溫度顯示固定數(shù)值不變,以下是程序,哪位大神能否指導(dǎo)...

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:389725 發(fā)表于 2018-8-24 17:18 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include "ds18b20.h"


void delay(uint z)
{
   uint x,y;
   for(x=z;x>0;x--)
      for(y=110;y>0;y--);
}
void dsreset(void)  //復(fù)位,初始化
{
   uint i;
   ds=0;
   i=103;
   while(i>0) i--;
   ds=1;
   i=4;
   while(i>0) i--;
}
bit tempreadbit(void)
{
   uint i;
   bit dat;
   ds=0;
   i++;
   ds=1;
   i++;
   i++;
   dat=ds;
   i=8;
   while(i>0)i--;
   return dat;
}
uchar tempread(void)   //讀一個(gè)字節(jié)函數(shù)
{
   uchar i,j,dat;
   dat=0;
   for(i=1;i<=8;i++)
   {
      j=tempreadbit();
   dat=(j<<7)|(dat>>1);  //讀出的數(shù)據(jù)最低位在最前面,這樣剛好一個(gè)字節(jié)在dat里
   }
   return dat;
}
void tempwritebyte(uchar dat)   //寫(xiě)一個(gè)字節(jié)函數(shù)
{
   uint i;
   uchar j;
   bit testb;
   for(j=1;j<=8;j++)
   {
       testb=dat&0x01;
    dat=dat>>1;
    if(testb)
    {
       ds=0;
    i++;
    i++;
    ds=1;
    i=8;
    while(i>0)i--;
  }
     else
  {
    ds=0;
    i=8;
    while(i>0)i--;
    ds=1;
    i++;
    i++;
  }
}
}
void tempchange(void)  //開(kāi)始獲取溫度并轉(zhuǎn)換
{
    dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0x44);
}
uint get_temp(void)
{
   uchar a,b;
   uint temp;
   dsreset();
   delay(1);
   tempwritebyte(0xcc);
   tempwritebyte(0xbe);
   a =tempread();                           //存放溫度值的低字節(jié)
   b =tempread();
   temp=b;
   temp<<=8;
   temp=temp|a;
   temp=temp*0.0625*10+0.5;
   return temp;
}
顯示程序在主函數(shù)
tempchange();//調(diào)用溫度轉(zhuǎn)換函數(shù)
LcdDisplay(get_temp());   //讀取溫度并顯示
void LcdDisplay(int temp)   //lcd顯示
{   
   unsigned char datas[] = {0, 0, 0, 0, 0}; //定義數(shù)組
float tp;  
if(temp< 0)    //當(dāng)溫度值為負(fù)數(shù)
   {
  temp=temp-1;
  temp=~temp;
  tp=temp;
  temp=tp*0.0625*100+0.5;
   }
  else
   {   
   
  tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
  temp=tp*0.0625*100+0.5;
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
write_str("溫度:");
write_data('0'+datas[1]|0x30);
write_data('0'+datas[2]|0x30);
write_data('.');
write_data('0'+datas[3]|0x30);
write_data('0'+datas[4]|0x30);
location(4,6);
write_data(0xA1);
write_data(0xE6);
}
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:388013 發(fā)表于 2018-8-24 17:43 | 只看該作者
讀取后好像沒(méi)有返回

評(píng)分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 回帖助人的獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

板凳
ID:137075 發(fā)表于 2018-8-24 18:12 | 只看該作者
我也沒(méi)看到返回
回復(fù)

使用道具 舉報(bào)

地板
ID:80221 發(fā)表于 2018-8-25 17:11 | 只看該作者
沒(méi)刷新顯示
回復(fù)

使用道具 舉報(bào)

5#
ID:389725 發(fā)表于 2018-8-27 08:11 | 只看該作者
歷程龍 發(fā)表于 2018-8-24 18:12
我也沒(méi)看到返回

怎么樣修改才有返回呢
回復(fù)

使用道具 舉報(bào)

6#
ID:308437 發(fā)表于 2018-8-27 10:06 | 只看該作者
1984528951 發(fā)表于 2018-8-24 17:43
讀取后好像沒(méi)有返回

顯示程序在主函數(shù)
tempchange();//調(diào)用溫度轉(zhuǎn)換函數(shù)
LcdDisplay(get_temp());   //讀取溫度并顯示
void LcdDisplay(int temp)   //lcd顯示
{   
   unsigned char datas[] = {0, 0, 0, 0, 0}; //定義數(shù)組
float tp;  
if(temp< 0)    //當(dāng)溫度值為負(fù)數(shù)
   {
  temp=temp-1;
  temp=~temp;
  tp=temp;
  temp=tp*0.0625*100+0.5;
   }
  else
   {   
   
  tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
  temp=tp*0.0625*100+0.5;
}
datas[0] = temp / 10000;
datas[1] = temp % 10000 / 1000;
datas[2] = temp % 1000 / 100;
datas[3] = temp % 100 / 10;
datas[4] = temp % 10;
write_str("溫度:");
write_data('0'+datas[1]|0x30);
write_data('0'+datas[2]|0x30);
write_data('.');
write_data('0'+datas[3]|0x30);
write_data('0'+datas[4]|0x30);
location(4,6);
write_data(0xA1);
write_data(0xE6);
}
他書(shū)主函數(shù)調(diào)用了

評(píng)分

參與人數(shù) 1黑幣 +30 收起 理由
admin + 30 回帖助人的獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

7#
ID:307368 發(fā)表于 2018-8-27 11:29 | 只看該作者
uint get_temp(void)
{
   uchar a,b;
   uint temp;
   dsreset();
   delay(1);
   tempwritebyte(0xcc);
   tempwritebyte(0xbe);
   a =tempread();                           //存放溫度值的低字節(jié)
   b =tempread();
   temp=b;
   temp<<=8;
   temp=temp|a;
   temp=temp*0.0625*10+0.5;
   return temp;
}
這個(gè)函數(shù)已經(jīng)是溫度了,樓上那一段又算了一遍,但也沒(méi)有看到哪里調(diào)用了這段函數(shù)。復(fù)雜。
啟動(dòng)溫度采集,等待足夠時(shí)間(看配置,有的配置好像需要900ms左右,其他忘了),讀取數(shù)據(jù),一般就可以了。就算手不去摸他。基本都會(huì)跳。

評(píng)分

參與人數(shù) 1黑幣 +90 收起 理由
admin + 90 回帖助人的獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

8#
ID:389725 發(fā)表于 2018-8-27 14:13 | 只看該作者
熊熊生風(fēng) 發(fā)表于 2018-8-27 11:29
uint get_temp(void)
{
   uchar a,b;

LcdDisplay(get_temp());   在主函數(shù)的顯示函數(shù)調(diào)用了
回復(fù)

使用道具 舉報(bào)

9#
ID:317472 發(fā)表于 2018-8-28 01:40 來(lái)自觸屏版 | 只看該作者
LcdDisplay(get_temp());   //讀取溫度并顯示 需要加一個(gè)中間變量,分開(kāi)寫(xiě)
回復(fù)

使用道具 舉報(bào)

10#
ID:384109 發(fā)表于 2018-8-28 10:27 | 只看該作者
temp=b;
   temp<<=8;
   temp=temp|a;
   temp=temp*0.0625*10+0.5;
   return temp;
這里的temp*0.0625*10+0.5;去除

temp=tp*0.0625*100+0.5;
   }
  else
   {   
   
  tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
  temp=tp*0.0625*100+0.5;
}
這里改成 temp=(tp*0.0625*100+0.5)*100;
   }
  else
   {   
   
  tp=temp;//因?yàn)閿?shù)據(jù)處理有小數(shù)點(diǎn)所以將溫度賦給一個(gè)浮點(diǎn)型變量
  temp=(tp*0.0625*100+0.5)*100;
}
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表