×
单片机 > 单片机程序设计 > 详情

单片机中使用DS18B20温度传感器C语言程序(参考5)

发布时间:2020-06-19 发布时间:
|
#include

#define uchar unsigned char
#define uint unsigned int

sbit DQ=P2^7;            //define interface 定义接口

uint temp;              // variable of temperature   定义一个变量

uchar flag1;             // sign of the result positive or negative 定
//义一个标志,标志温度是否还是正

sbit P2_0=P2^0;           //数码管位选
sbit P2_1=P2^1;
sbit P2_2=P2^2;

unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82, 0xf8,0x80,0x90};   //数字编码

unsigned char code table1[]={0x40,0x79,0x24,0x30,
0x19,0x12,0x02, 0x78,0x00,0x10};//带小数点的编码

void delay(uint i)       //delay    延时子程序
{
     while(i--);
}


/*******************************************************************/
/*                           初始化ds18b2子函数*                       */
/*******************************************************************/
Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1;                                          //DQ复位
delay(8);                                        //稍做延时
DQ = 0;                                          //单片机将DQ拉低
delay(80);                                       //精确延时 大于 480us
DQ = 1;                                          //拉高总线
delay(14);
x=DQ;                                            //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(20);
}


/*******************************************************************/
/*                            读字节子函数                            */
/*******************************************************************/
ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
   DQ = 0;                                           // 给脉冲信号
   dat>>=1;
   DQ = 1;                                           // 给脉冲信号
   if(DQ)
    dat|=0x80;
   delay(4);
}
return(dat);
}
/********************************************************************/
/*                             写字节子函数                                  */
/********************************************************************/
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
   DQ = 0;
   DQ = dat&0x01;
   delay(5);
   DQ = 1;
   dat>>=1;
}
}

/*void tmpwritebyte(uchar dat)    //write a byte to ds18b20 给温度传感器写一个字节
{
   uint i;
   uchar j;
   bit testb;
   for(j=1;j<=8;j++)
   {
     testb=dat&0x01;
     dat=dat>>1;
     if(testb)      //write 1
     {
       DQ=0;
       i++;i++;
       DQ=1;
       i=8;while(i>0)i--;
     }
     else
     {
       DQ=0;        //write 0
       i=8;while(i>0)i--;
       DQ=1;
       i++;i++;
     }
   }
}*/

void tmpchange(void)                                         //DS18B20 begin change    发送温度转换命令
{
   Init_DS18B20();                                            //初始化DS18B20
   delay(200);                                                  //延时
   WriteOneChar(0xcc);                                        // 跳过序列号命令
   WriteOneChar(0x44);                                        //发送温度转换命令
}

uint tmp()                                                   //get the temperature          得到温度值
{
   float tt;
   uchar a,b;
   Init_DS18B20();
   delay(1);
   WriteOneChar(0xcc);
   WriteOneChar(0xbe);                                       //发送读取数据命令
   a=ReadOneChar();                                          //连续读两个字节数据
   b=ReadOneChar();
   temp=b;
   temp<<=8;                                               
   temp=temp|a;                                              //两字节合成一个整型变量。
   tt=temp*0.0625;                                           //得到真实十进制温度值,因为DS18B20
                                                            //可以精确到0.0625度,所以读回数据的最低位代表的是
                                                            //0.0625度。
   temp=tt*10+0.5;                                           //放大十倍,这样做的目的将小数点后第一位
                                                            //也转换为可显示数字,同时进行一个四舍五入操作。
   return temp;                                              //返回温度值
}

/*void readrom()           //read the serial 读取温度传感器的序列号
{                       //本程序中没有用到此函数
   uchar sn1,sn2;
   Init_DS18B20();
   delay(1);
   WriteOneChar(0x33);
   sn1=ReadOneChar();
   sn2=ReadOneChar();
}*/

void delay10ms()             //delay            延时10MS子函数
   {
     uchar a,b;
     for(a=50;a>0;a--)
       for(b=60;b>0;b--);
    }
void display(uint tem)            //display        显示子函数
   {
      uchar A1,A2,A2t,A3;
   if(tem>=100)
   {
         A1=table[tem/100];
         A2t=tem%100;
         A2=table1[A2t/10];
         A3=table[A2t%10];
   }
   else
   {
      A2t=tem%100;
         A1=table1[A2t/10];
         A2=table[A2t%10];
   A3=0xFF;
   }
        P0=A1;
        P2_0=0;
        P2_1=1;
        P2_2=1;
        delay10ms();
        P0=A2;
        P2_1=0;
        P2_0=1;
        P2_2=1;
        delay10ms();
     if(A3!=0xFF)
     {
          P0=A3;
          P2_2=0;
          P2_0=1;
          P2_1=1;
     }
    }

void main()                                  //主函数
{
   do
   {
     tmpchange();                            //温度转换,
     display(tmp());                         //显示温度值
   }
      while(1);
}

关键字:单片机  DS18B20  温度传感器 

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

热门文章 更多
浅谈msp430f5529入门(2)----时钟配置.例程分析