×
嵌入式 > 技术百科 > 详情

温度传感器18B20程序案例

发布时间:2020-06-15 发布时间:
|

该程序为通过18B20,测量温度,并将温度的整数值在两个数码管中显示出来。

 

该程序只适用于系统只有一个18B20,没有检测多个18B20的功能。

 

另外,该程序未进行温度的正式判断,默认环境温度高于0度。

      

 #include

 #include

 #define uint unsigned int

 #define uchar unsigned char

 

 sbit DQ=P3^3;    // 18B20的数据位

 uint code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

 uint time=0;

 

//沿时1ms的函数

  void Delay1ms()

 {

    uint i,j;

    for(i=0;j<4;i++)

    {

       for(j=0;j<33;j++)

       {

          

       }

    }

 }

 

 

//沿时nms的函数

 void Delaynms(uint x)

 {

    uint i;

    for(i=0;i

    {

       Delay1ms();

    }

 }

 

 

//18B20初始化函数

 bit Init_18B20()

 {

    bit flag;

    DQ=1;

    for(time=0;time<2;time++)

    {

   

    }

    DQ=0;

    for(time=0;time<200;time++)

    {

   

    }

    DQ=1;

    for(time=0;time<15;time++)

    {

   

    }

    flag=DQ;

    for(time=0;time<200;time++)

    {

   

    }

    return flag;                   

 }

 

 

//从18B20里读出一个字节的函数

 uchar Read_18B20()

 {

    uchar i,result;

    for(i=0;i<8;i++)

    {

       DQ=1;

       _nop_();

       DQ=0;

       _nop_();

       DQ=1;

       for(time=0;time<2;time++)

       {

      

       }

       result>>=1;;

       if(DQ==1)

       {

           result|=0x80;

       }

       for(time=0;time<20;time++)

       {

      

       }

   

    }

    return result;

 

 }

 

 

//往18B20里写一个字节的函数

 void Write_18B20(uint cmd)

 {

    uint i;

    for(i=0;i<8;i++)

    {

       DQ=1;

       _nop_();

       DQ=0;

       _nop_();

       DQ=(cmd&0x01);

       for(time=0;time<15;time++)

       {

      

       }

       DQ=1;

       _nop_();

       _nop_();

       _nop_();

       _nop_();

       cmd>>=1;

       for(time=0;time<10;time++)

       {

      

       }

    }

 

 }

 

 

//18B20芯片进行温度转换的函数

 void Temp_Tran()

 {

    Init_18B20();

    Write_18B20(0xcc);//跳过读序列号的操作

    Write_18B20(0X44); //启动温度转化

    Delaynms(250);

 }

 

 

 //从18B20里读出温度值准备

 void TempGet_Ready()

 {

     Init_18B20();

     Write_18B20(0XCC);  //跳过读序列号的操作

     for(time=0;time<20;time++)

     {

     

     }

     Write_18B20(0XBE);    //读取温度寄存器,前两个分别为温度的低位与高位

     for(time=0;time<20;time++)

     {

     

     }

 }

 

 

 

//数码管显示函数

 void Dispaly(num1,num2)

 {

       P0=table[num2];     //P0为数码管的段选择

       P2=0xfe;      //P2为数码管的位选择

       Delaynms(30);

 

       P0=table[num1];

       P2=0XFD;

       Delaynms(5);

 

 }

 

 

 

 //主函数

 void main()

 {

    uint temp_tl;

    uint temp_th;

    uint th;

    //uint tl;

    uint gewei;

    uint shiwei;

 

    if(Init_18B20()==0)

    {

       P1=0X00;

    }

 

    while(1)

    {

       Temp_Tran();//调用温度转化函数

       _nop_();

       TempGet_Ready();//为读取温度值做准备

       temp_tl=Read_18B20(); // 温度值的低字节读取

       temp_th=Read_18B20(); //  温度值的高字节读取

 

       th=16*temp_th+temp_tl/16; //将温度值转化为十进制的值

       gewei=th%10;   // 十进制温度值的个位

 

       shiwei=th/10; //  十进制温度值的十位

      

       Dispaly(gewei,shiwei); //数码管显示温度值

    }

 }

 

将以上程序烧写入单片机中,数码管即可正常显示测量到的温度。

 

上面程序主要是说明18B20芯片的初始化,读取一个字节,写一个字节的过程。只要掌握了,其他的功能就很容易实现。

 

如系统有多个18B20,只需增加检测其序号号即可。

 

如设定18B20的温度报警值。




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
科胜讯推出针对全球有线数字机顶盒和宽带数据应用的DOCSIS2.0+芯片解决方案