專注電子技術(shù)學(xué)習(xí)與研究
當(dāng)前位置:單片機(jī)教程網(wǎng) >> MCU設(shè)計(jì)實(shí)例 >> 瀏覽文章

51單片機(jī)18B20溫度與串口實(shí)驗(yàn)

作者:佚名   來源:本站原創(chuàng)   點(diǎn)擊數(shù):  更新時(shí)間:2013年10月25日   【字體:

有圖有真相





溫度顯示在串口調(diào)速器上,此軟件可到 http://www.torrancerestoration.com/mcudown/ 單片機(jī)開發(fā)實(shí)用工具軟件這一欄下載
下面單片機(jī)部分的源代碼:
 

//11.0592晶振
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
code uchar seg[]={0x28,0x7e,0xa2,0x62,0x74,0x61,0x21,0x7a,0x20,0x60};   //數(shù)碼管碼表
code uchar tab[]={0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe};   //數(shù)碼管位選

//sbit k8=P1^7;   //按鍵發(fā)送溫度值
sbit dec=P0^5;   //小數(shù)點(diǎn)顯示位
static uint count;     //延時(shí)發(fā)送溫度值變量
void delay(uint k)
{
 while(k--);
}
void show_temp(uint k)    //顯示溫度值函數(shù)
{
 P0=seg[k/100];
 P2=tab[2];
 delay(30);
 P0=0xff;
 P2=0xff;
 P0=seg[k%100/10];
 dec=0;       //顯示小數(shù)點(diǎn)
 P2=tab[1];
 delay(30);
 P0=0xff;
 P2=0xff;
 P0=seg[k%10];
 P2=tab[0];
 delay(30);
 P0=0xff;
 P2=0xff;
}
#include "18B20.h"        //18B20庫函數(shù)
 
uchar suf;      //存放串口收到的數(shù)據(jù)
uint temp;    //輸出溫度值
 
void serial_init()   //串口初始化
{
   TMOD|=0x20;   //定時(shí)器設(shè)定自動(dòng)重撞在模式
   TH1=TL1=0xfd;  //9600波特率
   SM2=0;       //無多機(jī)處理位
   SM1=1;     // 8位可變波特率模式
   SM0=0;
   TR1=1;         //  定時(shí)器1打開
   REN=1;         //串口接收使能
}
void send_byte(uchar u)   //發(fā)送一個(gè)字節(jié)函數(shù)
{
   bit es;    //保存先前ES狀態(tài)變量,這樣可以不干擾其他用戶用它是現(xiàn)在的狀態(tài)
   es=ES;
   ES=0;     //暫時(shí)關(guān)閉串口中斷
   SBUF=u;    //數(shù)據(jù)放入SBUF緩沖器等待發(fā)送完成
   while(TI==0);   //等待發(fā)送完成
   TI=0;
   ES=es;     //恢復(fù)先前狀態(tài)
}
void send_str(uchar *p) //發(fā)送一串字符函數(shù)
{
   while(*p)
   {
 send_byte(*p);
 p++;
   }
}
void str_init()   //串口中斷初始化
{
 EA=1;
 ES=1;
}
void serial_event() interrupt 4   //串口中斷函數(shù)
{
   if(RI==1)   //在中斷里盡量只做需要的事情
   {
    suf=SBUF;
 RI=0;
   }
   else
   {
 ;
 }
}
void send_number(uchar *p,uint k)    //通過串口發(fā)送溫度值函數(shù)
{
 while(*p)
 {
  send_byte(*p);
  p++;
 }
 send_byte(' ');
 send_byte(' ');
 send_byte(k/100+48);
 send_byte(k%100/10+48);
 send_byte('.');
 send_byte(k%10+48);
 send_byte('\r');
 send_byte('\n');
}
/*
void key8()             //按鍵發(fā)送函數(shù)
{
  static uchar i,j;
  if(k8==0)
  {
 if(i==0)
 {
  j++;
  if(j>3)
  {
   i=1;j=0;
   send_number("the temperature is  ",temp);
  }
 }
  }
  else
  {
   i=0;
   j=0;
  }
}
*/
void main()
{
   serial_init();       //串口初始化
   send_byte(12);  //超級(jí)終端清屏
   send_str("serial_init is ok \r\n");    //初始化好壞標(biāo)示字符串
   str_init();           //串口中斷初始化
   send_str("str_init is ok \r\n");
 while(1)               
 {
   count++;  
   temp=read_18b20();
   show_temp(temp);
   if(count>500)     //約沒3秒發(fā)送一次溫度值給串口
   {
   send_number("The temperature is ",temp);
   count=0;
   }
   else
   {
   }
 }
}
 
//-----------------------18B20----庫函數(shù)----------------
sbit dq=P3^4;   //18B20數(shù)據(jù)口
void send_1()  //發(fā)送1位1函數(shù)
{
  dq=0;
  dq=0;
  dq=0;
  dq=1;
  delay(2);
}
void send_0() //發(fā)送1位0函數(shù)
{
  dq=0; 
  delay(2);
  dq=1;
}
void Tsend_byte(uchar dat)
{
 uchar i;
 for(i=0;i<8;i++)
 {
  if(dat&0x01)send_1();
  else send_0();
  dat>>=1;
 }
}
uchar read_byte()   //18B20讀取數(shù)據(jù)函數(shù)
{
  uchar i,byte;
  for(i=0;i<8;i++)
  {
    dq=0;
 dq=0;
 dq=1;
 dq=1;
 byte>>=1;
 if(dq==1)
 {
  byte=byte+0x80;  //如果采樣為1則最高位為1
 }
 delay(3);
  }
  return byte;
}
uchar start_18b20() //啟動(dòng)18b20實(shí)質(zhì)上判斷有誤應(yīng)答信號(hào)
{
  static uint cnt;
  dq=0;
  delay(62);
  dq=1;
  while(dq==1)  //等待拉低
  {
 cnt++;
 if(cnt>50000)
 {
 cnt=0;
 return 1;  //等待超時(shí)判斷說明器件壞了
 }
  }
   while(dq==0)             
   {
 cnt++;
 if(cnt>50000)
 {
  cnt=0;
  return 1;
 }
   }
   return 0;       //正常啟動(dòng)返回0值
}
uint read_18b20()  //讀取溫度值函數(shù)
{
  uchar TH,TL;
  uint temp;
  start_18b20();
  Tsend_byte(0xcc);
  Tsend_byte(0x44);
  ;;
  start_18b20();
  Tsend_byte(0xcc);
  Tsend_byte(0xbe);
   TL=read_byte();
   TH=read_byte();
   temp=TH;
   temp<<=8;
   temp=temp+TL;
   /*if(temp&0xf800)   //如果最高位為1則是溫度為負(fù)值   .可以設(shè)置溫度負(fù)值符號(hào)
   {
 temp=~temp+1;
   }
   */
   temp=temp*0.0625*10;   //顯示小數(shù)部分
   return temp;
}
關(guān)閉窗口

相關(guān)文章