目录
一、原理部分
1、什么是DS18B20
DS18B20是一款数字温度传感器。它采用的1-Wire通信即仅采用一个数据线(以及地)与微控制器进行通信。该传感器的温度检测范围为-55℃至+125℃,并且在温度范围超过-10℃至85℃之外时还具有+-0.5℃的精度。此外,DS18B20可以直接由数据线供电而不需要外部电源供电。
2、DS18B20驱动
在蓝桥杯的比赛中,官方会提供DS18B20的底层驱动,我们只需要引用文件,并学习如何使用驱动即可。
不过,需要注意的是,提供的头文件以及C文件需要进行检查,例如函数声明,以及底层驱动的测试环境是即时钟是否与当下芯片的时钟对的上,譬如19年提供的底层驱动,测试环境是12T 8051 12MHZ,相比于实际使用的芯片1T,慢了12倍,则驱动代码中的延时应该扩大12倍。
头文件
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
#endif
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include "reg52.h"
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(50);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(50);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(50);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(120);
DQ = 0;
Delay_OneWire(800);
DQ = 1;
Delay_OneWire(100);
initflag = DQ;
Delay_OneWire(50);
return initflag;
}
3、DS18B20的温度转换与读取流程
官方底层驱动提供了三个函数,分别是DS18B20复位,读一个字节,写一个字节,温度读取依靠这三个函数,流程如下:
(1)DS18B20复位
(2)写入字节0xcc,跳过ROM指令
(3)写入字节0x44,开始温度转换
(4)延时700ms~900ms
(5)DS18B20复位
(6)写入字节0xcc,跳过ROM指令
(7)写入字节0xbe,读取高速暂存器
(8)读取暂存器的第0字节,即温度传感器的LSB
(9)读取暂存器的第1字节,即温度传感器的MSB
(10)DS18B20复位,表示读取数据结束
(11)将LSB与MSB整合为16位数据
(12)判断读取结果的符号,进行正负温度的处理
4、DS18B20的数据处理
通过上述过程获取到温度数据后,需要对数据进行处理,使其以十进制在数码管呈现
DS18B20以16位带符号位扩展的二进制补码(正数和反码相同,负数需要取反加1)形式读出,低4位为小数部分,中间7位为整数部分,高5位为符号位。
DS18B20的分辨率为0.0625。读出数据为正温度时,将LSB和MSB整合成16位整数,直接乘以0.0625即可,读出数据为负温度时,则需将LSB和MSB整合成的16位整数,取反后加1再乘上0.0625即可。
譬如取出数据LSB=0xc7,MSB=0x01,则temp=0x01c7
(1)如果要求温度结果保留1位小数可以进行如下数据处理:
①首先将温度结果的整数部分取出: temp>>= 4;即 temp= 0x001c = 28。
②然后将整数部分放大10倍: temp= temp*10 ;即 temp= 280。
③然后将小数部分取出: LSB&0x0f,其结果为0x07。
④再将小数部分乘以0.0625的10倍,即0x07 × 0.625 = 4.375。
⑤最后将整数部分和小数部分相加: temp= temp+ 4.375= 284(小数点后被省略,temp为整型)
⑥对于温度数据284,则在数码管显示:28.4。(2)如果要求温度结果只显示整数部分:
①temp>>= 4;即 temp= 0x001c = 28摄氏度。
二、程序案例
例子:在数码管上显示带一位小数的温度值
#include "reg52.h"
#include "onewire.h"
//头文件以及C文件前面已提供
unsigned char SMGNoDot_CA[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//没有带小数点的断码表
unsigned char SMGDot_CA[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};//带小数点的断码表
unsigned int temp=0;//定义温度
void SelectHC573(unsigned char channel);//声明函数
/*****************************************************************
*@Function: InitSystem //
*@Description: 初始化,关闭蜂鸣器、继电器以及LED灯 //
*@Input:无 //
*@Output: 无//
*@Return:无 //
*@Others:无 //
/*****************************************************************/
void InitSystem()
{
SelectHC573(5);
P0=0x00;
SelectHC573(4);
P0=0xff;
SelectHC573(0);
}
/*****************************************************************
*@Function: InitSystem //
*@Description: 锁存器选择 //
*@Input:channel:通道选择 //
*@Output:无 //
*@Return:无 //
*@Others:无 //
/*****************************************************************/
void SelectHC573(unsigned char channel)
{
switch(channel)
{
case 4:
P2=P2&0x1f|0x80;
break;
case 5:
P2=P2&0x1f|0xa0;
break;
case 6:
P2=P2&0x1f|0xc0;
break;
case 7:
P2=P2&0x1f|0xe0;
break;
case 8:
P2=P2&0x1f|0x00;
break;
}
}
/*****************************************************************
*@Function:DelaySMG //
*@Description: 数码管延时函数 //
*@Input:无 //
*@Output:无 //
*@Return:无 //
*@Others: 无 //
/*****************************************************************/
void DelaySMG(unsigned char t)
{
while(t--);
}
/*****************************************************************
*@Function: DisplaySMG_Bit //
*@Description:单个数码管显示//
*@Input:value:数码管显示内容
* pos :数码管显示位置 //
*@Output:无 //
*@Return:无 //
*@Others: 无 //
/*****************************************************************/
void DisplaySMG_Bit(unsigned char value,unsigned char pos)
{
SelectHC573(6);
P0=0x01<<pos;
SelectHC573(7);
P0=value;
}
/*****************************************************************
*@Function: Dispaly_All //
*@Description:全部数码管显示 //
*@Input:value:显示内容 //
*@Output:无 //
*@Return:无 //
*@Others: 无 //
/*****************************************************************/
void Dispaly_All(unsigned char value)
{
SelectHC573(6);
P0=0xff;
SelectHC573(7);
P0=value;
}
unsigned char i;//计数
/*****************************************************************
*@Function:Display_Temp //
*@Description:温度显示 //
*@Input: 无//
*@Output:无//
*@Return:无//
*@Others:无//
/*****************************************************************/
void Display_Temp()
{
DisplaySMG_Bit(SMGNoDot_CA[temp%10],7);//显示温度十位
DelaySMG(500);
DisplaySMG_Bit(SMGDot_CA[(temp%100)/10],6);//显示温度个位
DelaySMG(500);
DisplaySMG_Bit(SMGNoDot_CA[temp/100],5);//显示温度小数
DelaySMG(500);
DisplaySMG_Bit(0xff,4);//其余数码管关闭显示
DelaySMG(500);
DisplaySMG_Bit(0xff,3);
DelaySMG(500);
DisplaySMG_Bit(0xff,2);
DelaySMG(500);
DisplaySMG_Bit(0xff,1);
DelaySMG(500);
DisplaySMG_Bit(0xff,0);
DelaySMG(500);
Dispaly_All(0xff);//关闭所有数码管显示,防止残影
DelaySMG(500);
}
/*****************************************************************
*@Function: Delay //
*@Description:延时函数 //
*@Input:无 //
*@Output: 无//
*@Return: 无 //
*@Others: 无 //
/*****************************************************************/
void Delay(unsigned char t)
{
while(t--)
{
Display_Temp();
}
}
/*****************************************************************
*@Function: Read_DS18B20_temp //
*@Description:温度读取与转换 //
*@Input: 无//
*@Output: 无//
*@Return: 无 //
*@Others:无 //
/*****************************************************************/
void Read_DS18B20_temp()
{
unsigned char LSB,MSB;//定义低4位,高4位存储变量
init_ds18b20();//DS18B20复位
Write_DS18B20(0xcc);//写入0xcc,跳过ROM指令
Write_DS18B20(0x44);//写入0x44,开始温度转换
Delay(1000);//延时1000ms
init_ds18b20();//DS18B20复位
Write_DS18B20(0xcc);//写入0xcc,跳过ROM指令
Write_DS18B20(0xbe);//写入0xbe,读取高速暂存器
LSB=Read_DS18B20();//读取暂存器的第0字节
MSB=Read_DS18B20();//读取暂存器的第1字节
temp=MSB;//将LSB、MSB合并为16位数据
temp=temp<<8|LSB;
if((temp&0xf800)==0x0000)//如果temp为正数
{
temp>>=4;//先右移4位,除去小数位
temp=temp*10;//整数位乘10
temp=temp+(LSB&0x0f)*0.625;//将LSB与上0x0f,LSB高4位清零,获得小数位,
//再乘以扩大10倍的分辨率0.625,加上整数部分,获得带一位小数的温度值的10倍
}
}
void main()
{
InitSystem();//初始化
//DisplaySMG_Bit(SMGDot_CA[5],3);
temp=573;//设置温度初始值573
while(1)
{
Read_DS18B20_temp();//温度读取
Display_Temp();//温度显示
}
}
蓝桥杯的学习笔记持续更新中~
要是文章有帮助的话,就点赞收藏关注一下啦!
感谢大家的观看
欢迎大家提出问题并指正~