栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

基于单片机的温度计

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基于单片机的温度计

 

#include
#include
#define uchar unsigned char
#define uint unsigned int 
#define out P0
sbit smg1=out^4;
sbit smg2=out^5;
sbit DQ=P3^7;
void delay5(uchar);
void init_ds18b20(void); 
uchar readbyte(void);
void writebyte (uchar); 
uchar retemp(void);

         void main( void)
          {
            uchar i, temp;
            delay5(1000);
        while(1)
            {
              temp=retemp( );
              for(i=0;i<10;i++)
              {
                out= (temp/10)&0x0f;
                    smg1 =0;
                  smg2=1;
                  delay5( 1000) ;
                out= (temp%10) &0x0f;
                  smg1=1;
                  smg2=0;
                  delay5(1000);
               }
             }
           }
          
        void delay5(uchar n)
         {
           do
             {
               _nop_();
               _nop_();
               _nop_();
               n--;
              }
           while(n);
         }

        void init_ds18b20(void)
         {
            uchar x=0;
            DQ=0;
            delay5(120);
            DQ=1;
            delay5(16);
            delay5(80);
          }

        uchar readbyte(void)
          {
            uchar i=0;
            uchar date=0;
            for (i=8;>;--)
             {
              DQ=0;
              delay5(1);
              DQ=1;
              date>>=1;
              if(DQ)
              date | =0x80;
              delay5(11);
              }
            return(date);
          }

      void writebyte uchar dat)
          {
            uchar i=0;
            for(i=8;i>0;i--)
            {
              DQ=0;
              DQ=dat&0x01;
              delay5(12);
              DQ=1;
              dat>>=1;
              delay5(5);
             }
           }

           uchar retemp( void)
           {
              uchar a,b,tt;
              uint t;
              init_ds18b20( );
              writebyte(0xCC); 
              writebyte(0x44);
              init_ds18b20( );
              writebyte(0xCC);
              writebyte(0xBE);
              a=readbyte( );
              b=readbyte( );
              t=b;
              t<<=8;
              t=t | a;
              tt=t*0.0625;
              return(tt);
            }

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/965898.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号