日本a√视频在线,久久青青亚洲国产,亚洲一区欧美二区,免费g片在线观看网站

        <style id="k3y6c"><u id="k3y6c"></u></style>
        <s id="k3y6c"></s>
        <mark id="k3y6c"></mark>
          
          

          <mark id="k3y6c"></mark>

          新聞中心

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

          DS18B20+PIC測(cè)溫用1602顯示溫度C程序

          作者: 時(shí)間:2016-11-30 來(lái)源:網(wǎng)絡(luò) 收藏

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

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

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

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

          //***********************************************
          //函 數(shù) 名:read_ds18b20_data();
          //入口參數(shù):無(wú)
          //出口參數(shù):無(wú)
          //函數(shù)作用:讀DS18B20測(cè)試的溫度數(shù)據(jù)
          //說(shuō) 明:
          //***********************************************
          void read_ds18b20_data(void)
          {
          DQ_IO=1; //設(shè)置RE0為輸入口
          ds18b20_reset(); //調(diào)用復(fù)位函數(shù)
          ds18b20_writebyte(0XCC); //跳過(guò)ROM匹配
          ds18b20_writebyte(0X44); //發(fā)送溫度變換命令
          ds18b20_reset(); //再次復(fù)位
          ds18b20_writebyte(0XCC); //跳過(guò)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ù)個(gè)位
          temperature=0;
          if(xiaoshu&0x80) //下面是把小數(shù)部分轉(zhuǎn)換為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; //萬(wàn)分位
          DelayUs(1); //延時(shí)6us
          }

          //***********************************************
          //函 數(shù) 名:lcd_display_temp();
          //入口參數(shù):無(wú)
          //出口參數(shù):無(wú)
          //函數(shù)作用:LCD顯示測(cè)試溫度程序
          //說(shuō) 明:
          //***********************************************
          void lcd_display_temp(void)
          {
          PORTD=0X80; //設(shè)置第1行顯示地址
          lcd_enable();
          lcd_writedata(name); //調(diào)用顯示函數(shù)
          PORTD=0XC0; //設(shè)置第2行顯示地址
          lcd_enable(); //調(diào)用寫(xiě)使能函數(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ù):無(wú)
          //出口參數(shù):無(wú)
          //函數(shù)作用:MAIN函數(shù)
          //說(shuō) 明:
          //***********************************************
          void main(void)
          {
          port_init(); //調(diào)用端口初始化函數(shù)
          lcd_init(); //調(diào)用LCD初始化函數(shù)
          while(1)
          {
          read_ds18b20_data(); //調(diào)用溫度轉(zhuǎn)換函數(shù)
          CLRWDT(); //清看門(mén)狗
          lcd_display_temp(); //調(diào)用溫度顯示函數(shù)
          }
          }


          上一頁(yè) 1 2 下一頁(yè)

          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉