當(dāng)前位置:首頁(yè) > 單片機(jī) > 單片機(jī)
[導(dǎo)讀]AT89C52控制溫度傳感器DS18B20并在LM016L_LCD上顯示

 //--------------------------------------------------------

//文 件 名:Temperture_Control
//
//文件簡(jiǎn)介:AT89C52作為主控,實(shí)時(shí)獲取、處理、發(fā)送數(shù)據(jù),全局控制;
//          DS18B20_Sensor溫度(-50 ~ 255℃)數(shù)據(jù)實(shí)時(shí)輸入;
//          LM016L_LCD溫度數(shù)據(jù)實(shí)時(shí)顯示
//
//修改時(shí)間:2017.10.24
//
//修改人員:Touch
//--------------------------------------------------------

 


#include"reg52.h"

//宏定義、全局變量定義區(qū)   
#define uchar unsigned char  
#define uint unsigned int
  
uchar code disp[]="0123456789";//數(shù)據(jù)顯示數(shù)組  
uchar code disp2[]="Tempreture";//標(biāo)題欄顯示  
uchar code disp3[]="^C"; //單位顯示


//LCD管腳位定義 
sbit lcden=P2^0;  
sbit lcdrw=P2^1;  
sbit lcdrs=P2^2;  
sbit tempt=P2^3;
  
uint u_temp;   //定義無(wú)符號(hào)整型形參 
float f_temp;  //定義浮點(diǎn)型形參  

//----------------------------------------
//函 數(shù) 名:delay()
//函數(shù)類(lèi)型:void
//入口參數(shù):unint x
//出口參數(shù):None
//說(shuō)    明:?jiǎn)纹瑱C(jī)指令周期為---計(jì)算延時(shí)、//延時(shí)500ms程序
//----------------------------------------
void delay(uint x)      
{  
  uint i,j;
  for(i=0;i
   for(j=0;j<130;j++); 
}


//----------------------------------------
//函 數(shù) 名:write_com
//函數(shù)類(lèi)型:void
//入口參數(shù):uchar com
//出口參數(shù):None
//說(shuō)    明:液晶寫(xiě)命令函數(shù)
//----------------------------------------
void write_com(uchar com)
{  
 lcdrs=0;  
 P0=com;  
 delay(5);  
 lcden=1;  
 delay(5);  
 lcden=0;  
 delay(5);
}


//----------------------------------------
//函 數(shù) 名:write_data
//函數(shù)類(lèi)型:void
//入口參數(shù):uchar date
//出口參數(shù):None
//說(shuō)    明:液晶寫(xiě)數(shù)據(jù)函數(shù)
//----------------------------------------  
void write_data(uchar date)  
{  
 lcdrs=1;  
 P0=date;  
 delay(5);  
 lcden=1;   
 delay(5);  [!--empirenews.page--]
 lcden=0;  
 delay(5);       
 }
 
 
//----------------------------------------
//函 數(shù) 名:lcd_init
//函數(shù)類(lèi)型:void
//入口參數(shù):None
//出口參數(shù):None
//說(shuō)    明:液晶初始化
//---------------------------------------- 
void lcd_init()      
{  
 lcden=0;
 lcdrw=0;  
 write_com(0x38); //顯示模式 
 write_com(0x0c); //開(kāi)顯示,關(guān)光標(biāo) 
 write_com(0x06); //寫(xiě)字符后地址加1,光標(biāo)加1 
 write_com(0x01); //清屏 
}
 
 
//----------------------------------------
//函 數(shù) 名:display_tempt
//函數(shù)類(lèi)型:void
//入口參數(shù):None
//出口參數(shù):None
//說(shuō)    明:溫度數(shù)據(jù)顯示
//----------------------------------------   
void display_tempt(uint date) 
{  
 uchar i,j,k;  
 i=date/100;   //十位
 j=date0/10;  //個(gè)位
 k=date0;  //小數(shù)位  
 write_com(0x80+0x40+5); 
 write_data(disp[i]);
 write_data(disp[j]); 
 write_data('.');
 write_data(disp[k]);
 }


//----------------------------------------
//函 數(shù) 名:reset_temp
//函數(shù)類(lèi)型:void
//入口參數(shù):None
//出口參數(shù):None
//說(shuō)    明:DS18B20復(fù)位,初始化
//----------------------------------------  
void reset_tempt()    
{  
 uint i;  
 tempt=0;  
 i=90;   //延時(shí) 
 while(i>0)i--;  
 tempt=1;  
 i=4;    //延時(shí)  
 while(i>0)i--;
 }


//----------------------------------------
//函 數(shù) 名:read_bit()
//函數(shù)類(lèi)型:bit
//入口參數(shù):None
//出口參數(shù):dat
//說(shuō)    明:讀一位DS18B20數(shù)據(jù)
//----------------------------------------   
bit read_bit()  
{  
 uint i;  
 bit dat;  
 tempt=0;  
 i++;    //延時(shí) 
 tempt=1;  
 i++;    
 i++;  
 dat=tempt; 
 i=8;    //延時(shí)
 while(i>0)i--;  
 return dat;  
}


//----------------------------------------
//函 數(shù) 名:read_tempt()
//函數(shù)類(lèi)型:uchar
//入口參數(shù):None
//出口參數(shù):dat
//說(shuō)    明://讀一個(gè)字節(jié)DS18B20數(shù)據(jù)
//---------------------------------------- 
uchar read_tempt()    
{  
 uchar i,j,dat;  
 dat=0;  
 for(i=1;i<=8;i++)  
 {   
    j=read_bit();  
   dat=(j<<7)|(dat>>1); //將讀取的數(shù)據(jù)按讀取的先后順序,從低位到高位排列保存到dat 
 }  
  return dat;  
}
 
 
//----------------------------------------
//函 數(shù) 名:write_byte()
//函數(shù)類(lèi)型:void
//入口參數(shù):uchar dat
//出口參數(shù):None
//說(shuō)    明:寫(xiě)一個(gè)字節(jié)數(shù)據(jù)到DS18B20
//---------------------------------------- 
void write_byte(uchar dat) 
{  
  uint i;  
  uchar j; 
  bit testbit;
  for(j=1;j<=8;j++)   
    {   
      testbit=dat&0x01;  //每次只寫(xiě)一位數(shù)據(jù)
      dat=dat>>1;  
      if(testbit) [!--empirenews.page--]
       {  
        tempt=0;
        i++;    //延時(shí)
        tempt=1;
        i=8;    //延時(shí)
        while(i>0)i--;  
        }
     else
      {
       tempt=0;
       i=8;    //延時(shí)
       while(i>0)i--;
       tempt=1;
       i++;   //延時(shí)
       i++;
    }
   }
}


//----------------------------------------
//函 數(shù) 名:convert_tempt()
//函數(shù)類(lèi)型:void
//入口參數(shù):None
//出口參數(shù):None
//說(shuō)    明:DS18B20開(kāi)始獲取溫度,并進(jìn)行轉(zhuǎn)換
//----------------------------------------      
void convert_tempt() 
{
  reset_tempt();//初始化
  delay(1);
  write_byte(0xcc);//跳過(guò)序列號(hào)讀取
  write_byte(0x44);//溫度轉(zhuǎn)換
}

 

//----------------------------------------
//函 數(shù) 名:get_tempt
//函數(shù)類(lèi)型:uint
//入口參數(shù):None
//出口參數(shù):u_temp
//說(shuō)    明:獲取DS18B20寄存器中的溫度數(shù)據(jù)
//----------------------------------------
uint get_tempt()
   { 
     uchar a,b;   //低位和高位
     reset_tempt();
     delay(1);
     write_byte(0xcc);
     write_byte(0xbe);//寫(xiě)入
//讀數(shù)據(jù)命令
     
     a=read_tempt();
     b=read_tempt();   //讀取值暫存
     u_temp=b;    
     u_temp<<=8;
  u_temp|=a;        //數(shù)據(jù)處理問(wèn)題
   //  u_temp=u_temp/a;          
     f_temp=u_temp*0.0625;   //精度為12位,所以分辨率為0.0625
     u_temp=f_temp*10;       //乘以10,將實(shí)際溫度擴(kuò)大10倍

     return u_temp;          //返回的u_temp是整型數(shù)據(jù)
 }


//----------------------------------------
//函 數(shù) 名:display()
//函數(shù)類(lèi)型:void
//入口參數(shù):None
//出口參數(shù):None
//說(shuō)    明:顯示控制函數(shù)
//---------------------------------------- 
void display()     
 { 
  uchar i,j;
  write_com(0x80+3);
  for(i=0;i<10;i++) 
   { 
   write_data(disp2[i]);
    }
     write_com(0x80+0x40+10);
    for(j=0;j<2;j++)
    {
      write_data(disp3[j]);  //單位顯示控制
    } 
}

[!--empirenews.page--]
//----------------------------------------
//函 數(shù) 名:main()
//函數(shù)類(lèi)型:void
//入口參數(shù):None
//出口參數(shù):None
//說(shuō)    明:main函數(shù)
//----------------------------------------
void main()      

   uchar i;
   lcd_init();
   display();
   while(1) 
    {   
     convert_tempt();
       for(i=0;i<10;i++)
          {   display_tempt(get_tempt());  }
    }

本站聲明: 本文章由作者或相關(guān)機(jī)構(gòu)授權(quán)發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點(diǎn),本站亦不保證或承諾內(nèi)容真實(shí)性等。需要轉(zhuǎn)載請(qǐng)聯(lián)系該專(zhuān)欄作者,如若文章內(nèi)容侵犯您的權(quán)益,請(qǐng)及時(shí)聯(lián)系本站刪除。
換一批
延伸閱讀

9月2日消息,不造車(chē)的華為或?qū)⒋呱龈蟮莫?dú)角獸公司,隨著阿維塔和賽力斯的入局,華為引望愈發(fā)顯得引人矚目。

關(guān)鍵字: 阿維塔 塞力斯 華為

倫敦2024年8月29日 /美通社/ -- 英國(guó)汽車(chē)技術(shù)公司SODA.Auto推出其旗艦產(chǎn)品SODA V,這是全球首款涵蓋汽車(chē)工程師從創(chuàng)意到認(rèn)證的所有需求的工具,可用于創(chuàng)建軟件定義汽車(chē)。 SODA V工具的開(kāi)發(fā)耗時(shí)1.5...

關(guān)鍵字: 汽車(chē) 人工智能 智能驅(qū)動(dòng) BSP

北京2024年8月28日 /美通社/ -- 越來(lái)越多用戶(hù)希望企業(yè)業(yè)務(wù)能7×24不間斷運(yùn)行,同時(shí)企業(yè)卻面臨越來(lái)越多業(yè)務(wù)中斷的風(fēng)險(xiǎn),如企業(yè)系統(tǒng)復(fù)雜性的增加,頻繁的功能更新和發(fā)布等。如何確保業(yè)務(wù)連續(xù)性,提升韌性,成...

關(guān)鍵字: 亞馬遜 解密 控制平面 BSP

8月30日消息,據(jù)媒體報(bào)道,騰訊和網(wǎng)易近期正在縮減他們對(duì)日本游戲市場(chǎng)的投資。

關(guān)鍵字: 騰訊 編碼器 CPU

8月28日消息,今天上午,2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)開(kāi)幕式在貴陽(yáng)舉行,華為董事、質(zhì)量流程IT總裁陶景文發(fā)表了演講。

關(guān)鍵字: 華為 12nm EDA 半導(dǎo)體

8月28日消息,在2024中國(guó)國(guó)際大數(shù)據(jù)產(chǎn)業(yè)博覽會(huì)上,華為常務(wù)董事、華為云CEO張平安發(fā)表演講稱(chēng),數(shù)字世界的話(huà)語(yǔ)權(quán)最終是由生態(tài)的繁榮決定的。

關(guān)鍵字: 華為 12nm 手機(jī) 衛(wèi)星通信

要點(diǎn): 有效應(yīng)對(duì)環(huán)境變化,經(jīng)營(yíng)業(yè)績(jī)穩(wěn)中有升 落實(shí)提質(zhì)增效舉措,毛利潤(rùn)率延續(xù)升勢(shì) 戰(zhàn)略布局成效顯著,戰(zhàn)新業(yè)務(wù)引領(lǐng)增長(zhǎng) 以科技創(chuàng)新為引領(lǐng),提升企業(yè)核心競(jìng)爭(zhēng)力 堅(jiān)持高質(zhì)量發(fā)展策略,塑強(qiáng)核心競(jìng)爭(zhēng)優(yōu)勢(shì)...

關(guān)鍵字: 通信 BSP 電信運(yùn)營(yíng)商 數(shù)字經(jīng)濟(jì)

北京2024年8月27日 /美通社/ -- 8月21日,由中央廣播電視總臺(tái)與中國(guó)電影電視技術(shù)學(xué)會(huì)聯(lián)合牽頭組建的NVI技術(shù)創(chuàng)新聯(lián)盟在BIRTV2024超高清全產(chǎn)業(yè)鏈發(fā)展研討會(huì)上宣布正式成立。 活動(dòng)現(xiàn)場(chǎng) NVI技術(shù)創(chuàng)新聯(lián)...

關(guān)鍵字: VI 傳輸協(xié)議 音頻 BSP

北京2024年8月27日 /美通社/ -- 在8月23日舉辦的2024年長(zhǎng)三角生態(tài)綠色一體化發(fā)展示范區(qū)聯(lián)合招商會(huì)上,軟通動(dòng)力信息技術(shù)(集團(tuán))股份有限公司(以下簡(jiǎn)稱(chēng)"軟通動(dòng)力")與長(zhǎng)三角投資(上海)有限...

關(guān)鍵字: BSP 信息技術(shù)
關(guān)閉
關(guān)閉