<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設計應用 > DS18B20+PIC測溫用1602顯示溫度C程序

          DS18B20+PIC測溫用1602顯示溫度C程序

          作者: 時間:2016-11-30 來源:網(wǎng)絡 收藏

          //***********************************************
          //函 數(shù) 名:lcd_init();
          //入口參數(shù):無
          //出口參數(shù):無
          //函數(shù)作用:LCD初始化
          //說 明:
          //***********************************************
          void lcd_init(void)
          {
          PORTD=0X01; //清除顯示
          lcd_enable();
          PORTD=0X38; //8位2行5*7點陣
          lcd_enable();
          PORTD=0X0e; //顯示開,光標開,閃爍
          lcd_enable();
          PORTD=0X06; //文字不動,光標右移
          lcd_enable();
          }

          //***********************************************
          //函 數(shù) 名:ds18b20_reset();
          //入口參數(shù):無
          //出口參數(shù):無
          //函數(shù)作用:DS18B20復位
          //說 明:
          //***********************************************
          voidds18b20_reset(void)
          {
          uchar x=1;
          while(x)
          {
          DQ_IO=0; //設置RE0為輸出口
          DQ=0; //RE0輸出低電平
          DelayUs(201); //延時503us(最短480us低電平信號)
          DQ_IO=1; //釋放總線,進入接收(設置RE0為輸入口)
          DelayUs(29); //延時70us(18b20檢測到上升沿時,等待15-60us)
          if(DQ){x=1;} //有應答信號,跳出
          else {x=0;} //沒有應答信號,繼續(xù)復位(低電平持續(xù)在60-240us)
          DelayUs(172); //延時430us
          }
          }

          //***********************************************
          //函 數(shù) 名:ds18b20_writebyte(uchar data);
          //入口參數(shù):data
          //出口參數(shù):無
          //函數(shù)作用:DS18B20寫一個字節(jié)數(shù)據(jù)
          //說 明:
          //***********************************************
          voidds18b20_writebyte(uchar data)
          {
          uchar i,temp;
          for(i=8;i>0;i--) //寫8位數(shù)據(jù)
          {
          temp=data&0x01; //先寫低位數(shù)據(jù)
          DQ_IO=0; //設置RE0為輸出口
          DQ=0; //RE0輸出低電平
          DelayUs(1); //延時6us(15us之內(nèi)把數(shù)據(jù)送到總線上)
          if(temp){DQ_IO=1;} //設置RE0為輸入口(寫1時序)
          DelayUs(25); //延時61us(總線采樣時間15-60us)
          DQ_IO=1; //設置RE0為輸入口(寫0時序)
          DelayUs(1); //延時6us(寫第二位時間間隙大于1us)
          data=data>>1; //右移一位
          }
          }

          //***********************************************
          //函 數(shù) 名:ds18b20_readbyte();
          //入口參數(shù):無
          //出口參數(shù):無
          //函數(shù)作用:DS18B20讀一個字節(jié)數(shù)據(jù)
          //說 明:
          //***********************************************
          unsigned chards18b20_readbyte(void)
          {
          uchar i,data=0; //讀出溫度
          for(i=8;i>0;i--) //讀8位數(shù)據(jù)
          {
          data=data>>1; //數(shù)據(jù)先右移一位
          DQ_IO=0; //設置RE0為輸出口
          DQ=0; //RE0輸出低電平
          DelayUs(1); //延時6us(低電平時間大于1us)
          DQ_IO=1; //拉高總線,產(chǎn)生讀時間間隙(設置RE0為輸入口)
          DelayUs(1); //延時6us(從拉低電平開始15us之內(nèi)完成讀位)
          if(DQ){data=data|0x80;} //先讀高位數(shù)據(jù),高位為1
          else {data=data|0x00;} //高位為0
          DelayUs(25); //延時61us(從拉低電平開始60-120us之內(nèi)釋放總線)
          }
          return(data);
          }

          //***********************************************
          //函 數(shù) 名:read_ds18b20_data();
          //入口參數(shù):無
          //出口參數(shù):無
          //函數(shù)作用:讀DS18B20測試的溫度數(shù)據(jù)
          //說 明:
          //***********************************************
          void read_ds18b20_data(void)
          {
          DQ_IO=1; //設置RE0為輸入口
          ds18b20_reset(); //調用復位函數(shù)
          ds18b20_writebyte(0XCC); //跳過ROM匹配
          ds18b20_writebyte(0X44); //發(fā)送溫度變換命令
          ds18b20_reset(); //再次復位
          ds18b20_writebyte(0XCC); //跳過ROM匹配
          ds18b20_writebyte(0XBE); //發(fā)送讀溫度命令
          lowtemp=ds18b20_readbyte(); //讀出低8位溫度值
          hightemp=ds18b20_readbyte(); //讀出高8位溫度值
          DQ_IO=1; //釋放總線
          zhengshu=((lowtemp>>4)|(hightemp<<4))&0X3F;
          xiaoshu=lowtemp<<4;
          temp[0]=(zhengshu/100)%10; //整數(shù)百位
          temp[1]=(zhengshu/10)%10; //整數(shù)十位
          temp[2]=zhengshu%10; //整數(shù)個位
          temperature=0;
          if(xiaoshu&0x80) //下面是把小數(shù)部分轉換為BCD碼形式
          {
          temperature+=5000;
          }
          if(xiaoshu&0x40)
          {
          temperature+=2500;
          }
          if(xiaoshu&0x20)
          {
          temperature+=1250;
          }
          if(xiaoshu&0x10)
          {
          temperature+=625;
          }
          temp[3]=(temperature/1000)%10; //十分位
          temp[4]=(temperature/100)%10; //百分位
          temp[5]=(temperature/10)%10; //千分位
          temp[6]=temperature%10; //萬分位
          DelayUs(1); //延時6us
          }

          //***********************************************
          //函 數(shù) 名:lcd_display_temp();
          //入口參數(shù):無
          //出口參數(shù):無
          //函數(shù)作用:LCD顯示測試溫度程序
          //說 明:
          //***********************************************
          void lcd_display_temp(void)
          {
          PORTD=0X80; //設置第1行顯示地址
          lcd_enable();
          lcd_writedata(name); //調用顯示函數(shù)
          PORTD=0XC0; //設置第2行顯示地址
          lcd_enable(); //調用寫使能函數(shù)
          lcd_writebyte(0x20);
          lcd_writebyte(0x20);
          lcd_writebyte(0x20);
          if(temp[0]==0)
          {
          lcd_writebyte(0x20);
          }
          else
          {
          lcd_writebyte(temp[0]+0x30);
          }
          lcd_writebyte(temp[1]+0x30);
          lcd_writebyte(temp[2]+0x30);
          lcd_writebyte(0x2e);
          lcd_writebyte(temp[3]+0x30);
          lcd_writebyte(temp[4]+0x30);
          lcd_writebyte(temp[5]+0x30);
          lcd_writebyte(temp[6]+0x30);
          lcd_writebyte(0x20);
          lcd_writebyte(0x43);
          lcd_writebyte(0x20);
          lcd_writebyte(0x20);
          lcd_writebyte(0x20);
          }

          //***********************************************
          //函 數(shù) 名:main();
          //入口參數(shù):無
          //出口參數(shù):無
          //函數(shù)作用:MAIN函數(shù)
          //說 明:
          //***********************************************
          void main(void)
          {
          port_init(); //調用端口初始化函數(shù)
          lcd_init(); //調用LCD初始化函數(shù)
          while(1)
          {
          read_ds18b20_data(); //調用溫度轉換函數(shù)
          CLRWDT(); //清看門狗
          lcd_display_temp(); //調用溫度顯示函數(shù)
          }
          }


          上一頁 1 2 下一頁

          評論


          技術專區(qū)

          關閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();