DS18B20-Onewire Bus-单总线 单片机读取温度

DS18B20,单片机,11.0592MHZ

</pre><pre name="code" class="cpp">#ifndef __DELAY_H__
#define __DELAY_H__
/*------------------------------------------------
 uS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
 长度如下 T=tx2+5 uS 
------------------------------------------------*/
void delay(unsigned int i);

#endif

 

DS18B20_H

 

#ifndef __DS18B20_H__
#define __DS18B20_H__

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

#define uchar unsigned char
#define uint   unsigned int;
/*------------------------------------------------
                    端口定义
------------------------------------------------*/

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

#endif


delay.c

 

 

 

 

#include "delay.h"
/*------------------------------------------------
 uS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
 长度如下 T=tx2+5 uS 
------------------------------------------------*/
/******************************************************************/
/*                    延时函数                                    */
/******************************************************************/
void delay(unsigned int i)//延时函数
{
 while(i--);
}

 

/*---------<span style="font-family: Arial, Helvetica, sans-serif;">--</span><span style="font-family: Arial, Helvetica, sans-serif;">blog.csdn.net/liuzongming1988</span><span style="font-family: Arial, Helvetica, sans-serif;">-</span>-------------------------------------
  名称:18B20温度传感器
  网站:<span style="font-family: Arial, Helvetica, sans-serif;">blog.csdn.net/liuzongming1988</span>
  内容:18B20单线温度检测的应用样例程序
------------------------------------------------*/
#include"delay.h"
#include"18b20.h"


sbit DQ=P1^3;//ds18b20 端口

/******************************************************************/
/*                    初始化                                      */
/******************************************************************/
void Init_DS18B20(void)
{
 unsigned char x=0;
 DQ = 1;    //DQ复位
 delay(8);  //稍做延时
 DQ = 0;    //单片机将DQ拉低
 delay(300); //精确延时 大于 480us
 DQ = 1;    //拉高总线
 delay(10);
 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>>=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>>=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<<=8;
t=a+b;

return(t);
}

 

 

 

 

/*-----------<span style="font-family: Arial, Helvetica, sans-serif;">blog.csdn.net/liuzongming1988</span>---------------------
  名称:DS18b20数码管显示
  编写:blog.csdn.net/liuzongming1988
  内容:显示格式 符号 xxx.x C 
        可以显示负温度
------------------------------------------------*/
#include<reg52.h> //包含头文件,一般情况不需要改动,头文件包含特殊功能寄存器的定义
#include<math.h>
#include<INTRINS.H>		

#include "18b20.h"
#include "delay.h"
#define uchar unsigned char
#define uint   unsigned int;
/******************************************************************/
/*                    定义端口                                    */
/******************************************************************/
sbit seg1=P2^0;
sbit seg2=P2^1;
sbit seg3=P2^2;

sfr dataled=0x80;//显示数据端口
/******************************************************************/
/*                    全局变量                                    */
/******************************************************************/
uint temp;
uchar flag_get,count,num,minute,second;
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
                                               //7段数码管段码表  共阴极 显示段码值0~ 9
//{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,	0x7f,0x6f};//
uchar  str[6];

void Init_Timer0(void);

/*------------------------------------------------
                    主函数
------------------------------------------------*/
void main (void)
{                  
unsigned char TempH,TempL,j;
Init_Timer0();
P2=0x00;  
count=0;
temp=ReadTemperature();
temp=ReadTemperature();


while(1)
{
   str[5]=0x39;         //显示C符号
   str[1]=tab[TempH/100]; //百位温度
   str[2]=tab[(TempH%100)/10]; //十位温度
   str[3]=tab[(TempH%100)%10]|0x80; //个位温度,带小数点
   str[4]=tab[TempL];
  if(flag_get==1)       //定时读取当前温度
    {
  temp=ReadTemperature();
  if(temp&0x8000)
     {
     str[0]=0x40;//负号标志
     temp=~temp;  // 取反加1
	 temp +=1;
	 }
  else
     str[0]=0;

  TempH=temp>>4;
  TempL=temp&0x0F;
  TempL=TempL*6/10;//小数近似处理
  flag_get=0;
    }
  }
}

/*------------------------------------------------
                    定时器初始化子程序
------------------------------------------------*/
void Init_Timer0(void)
{

TMOD|=0x01;//定时器设置
TH0=0xef;
TL0=0xf0;

//IE=0x82;
EA=1;            //总中断打开
ET0=1;           //定时器中断打开

TR0=1;           //定时器开关打开
}
/*------------------------------------------------
                 定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1 	using 1
{
TH0=0xef;//定时器重装值
TL0=0xf0;
num++;
if (num==50)
    {num=0;
	  flag_get=1;//标志位有效
      }
count++;
if(count==1)
   {P2=0;
    dataled=str[0];}//数码管扫描
if(count==2)
   {P2=1;
    dataled=str[1];}
if(count==3)
   { P2=2;
     dataled=str[2];
     }
if(count==4)
   { P2=3;
     dataled=str[3];
     }
if(count==5)
   { P2=4;
     dataled=str[4];
     }
if(count==6)
   { P2=5;
     dataled=str[5];
     count=0;}
	
}

 

 

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爆板流

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值