基于DS18B20的温度检测系统

  最近在看单片机的东西,前两天做了一个温度测量系统。用的是DS18B20做传感。和大家分享下。希望高人指教。

一、电路原理图



二、原程序
/*-----------------------------------------------
  名称:18B20温度传感器
------------------------------------------------*/

#include<reg52.h>     //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include<math.h>
#include<INTRINS.H>

#define uchar unsigned char
#define uint   unsigned int;
/******************************************************************/
/*                    定义端口                                    */
/******************************************************************/
sbit seg1=P2^0;
sbit seg2=P2^1;
sbit seg3=P2^2;
sbit seg4=P2^3;
sbit seg5=P2^4;
sbit seg6=P2^5;

sbit DQ=P3^5;//ds18b20 端口
sfr dataled=0x90;//显示数据端口 P1
/******************************************************************/
/*                    全局变量                                    */
/******************************************************************/
uint temp;
uchar flag_get,count,num,minute,second;
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,
    0x99,0x92,0x82,0xf8,
           0x80,0x90};
                                               //7段数码管段码表共阳
uchar  str[6];

/******************************************************************/
/*                   函数声明                                     */
/******************************************************************/
void delay1(uchar MS);
unsigned int ReadTemperature(void);
void Init_DS18B20(void);
unsigned char ReadOneChar(void);
void WriteOneChar(unsigned char dat);
void delay(unsigned int i);

/******************************************************************/
/*                    主函数                                      */
/******************************************************************/
main()
{
unsigned char TempH,TempL;
TMOD|=0x01;//定时器设置 16位计数器
TH0=0xef;
TL0=0xf0;
IE=0x82;//相当月 EA=A  ET0=1-允许定时器0中断
TR0=1;//定时器开始工作
P2=0x0;  //why
count=0;
while(1)
{
   str[5]=0x9c;         //显示°符号
   str[1]=tab[TempH/100]; //百位温度
   str[2]=tab[(TempH%100)/10]; //十位温度
   str[3]=tab[(TempH%100)%10]&0x7f; //个位温度,带小数点
   str[4]=tab[TempL];
   if(str[1])
  if(flag_get==1)       //定时读取当前温度
    {
  temp=ReadTemperature();
  if(temp&0x800)//检测第一位符号位是否为1
     {
     str[0]=0xbf;//负号标志
     temp=~temp;  // 取反加1
temp +=1;
}
  else
     str[0]=0xff;
  TempH=temp>>4;
  TempL=temp&0x0f;
  TempL=TempL*6/10;//小数近似处理
  flag_get=0;
    }
  }
}


/******************************************************************/
/*                  定时器中断                                    */
/******************************************************************/
void tim(void) interrupt 1 using 1//中断,用于数码管扫描和温度检测间隔
{
TH0=0xef;//定时器重装值
TL0=0xf0;
num++;
if (num==50)
    {num=0;
     flag_get=1;//标志位有效
      }
count++;
if(count==1)
   {P2=0x01;
    dataled=str[0];}//数码管扫描
if(count==2)
   {P2=0x02;
    dataled=str[1];}
if(count==3)
   { P2=0x04;
     dataled=str[2];
     }
if(count==4)
   { P2=0x08;
     dataled=str[3];
     }
if(count==5)
   { P2=0x10;
     dataled=str[4];
     }
if(count==6)
   { P2=0x20;
     dataled=str[5];
     count=0;}
}
/******************************************************************/
/*                    延时函数                                    */
/******************************************************************/
void delay(unsigned int i)//延时函数
{
while(i--);
}
/******************************************************************/
/*                    初始化                                      */
/******************************************************************/
void Init_DS18B20(void)
{
unsigned char x=0;
DQ = 1;    //DQ复位
delay(8);  //稍做延时
DQ = 0;    //单片机将DQ拉低
delay(80); //精确延时 大于 480us
DQ = 1;    //拉高总线
delay(10);//wait DQ RETURN 0
x=DQ;      //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
delay(5);
}

/******************************************************************/
/*                    读一个字节                                  */
/******************************************************************/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
  DQ = 0; // 给脉冲信号
  dat=dat>>1;
  DQ = 1; // 给脉冲信号
  if(DQ)
   dat|=0x80;
  delay(5);
}
return(dat);
}

/******************************************************************/
/*                 写一个字节                                     */
/******************************************************************/
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
  DQ = 0;
  DQ = dat&0x01;
  delay(5);
  DQ = 1;
  dat=dat>>1;
}
delay(5);
}

/******************************************************************/
/*                   读取温度                                     */
/******************************************************************/
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
delay(200);
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度
a=ReadOneChar();   //低位
b=ReadOneChar();   //高位

b=b<<8;
t=a+b;

return(t);
}

三、仿真结果


四、简单说明

通过P2端口控制位选,通过ABCDEFG DP控制八段数码管。中断程序程序控制DS18B20的采样转换时隙,定时产生 flag_get=1 标志,而调用 ReadTemperature() 函数,从而读取温度值放在temp中,经过转换,送入P2口的各个位上。

猜你喜欢

转载自anlx27.iteye.com/blog/861007