蓝桥杯基础学习记录——ds18b20温度传感器的基本应用

温度传感器要用到官方给的底层驱动代码onewire.c

/*	# 	单总线代码片段说明
	1. 	本文件夹中提供的驱动代码供参赛选手完成程序设计参考。
	2. 	参赛选手可以自行编写相关代码或以该代码为基础,根据所选单片机类型、运行速度和试题
		中对单片机时钟频率的要求,进行代码调试和修改。
*/

//
#include <REGX52.H>
sbit DQ=P1^4;
void Delay_OneWire(unsigned int t)  
{
	unsigned char i;
	while(t--){
		for(i=0;i<12;i++);
	}
}


//
void Write_DS18B20(unsigned char dat)
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		DQ = 0;
		DQ = dat&0x01;
		Delay_OneWire(5);
		DQ = 1;
		dat >>= 1;
	}
	Delay_OneWire(5);
}

//
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(5);
	}
	return dat;
}

//
bit init_ds18b20(void)
{
  	bit initflag = 0;
  	
  	DQ = 1;
  	Delay_OneWire(12);
  	DQ = 0;
  	Delay_OneWire(80);
  	DQ = 1;
  	Delay_OneWire(10); 
    initflag = DQ;     
  	Delay_OneWire(5);
  
  	return initflag;
}

以及这里自己写一下onewire.h函数

#ifndef_文件名_H_
#define_文件名_H_
要调用的函数声明
变量声明
#endif

 这里只用到读写和初始化温度函数

 

#ifndef _ONEWIRE_H_
#define _ONEWIRE_H_

bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
#endif

首先我们要知道ds18b20的温度转换和读取流程。以上步骤转换成代码:

void Read_ds18b20_temp()
{
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);
	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	
	temp=MSB;
	temp=(temp<<8)|LSB;//合并为一个十六位的值
	
	if((temp & 0xf800)==0x0000)//判断温度是否为正数
	{
		temp>>=4;//温度值右移四位,temp是16位的有符号整数,最高四位位小数部分,剩下12位是整数部分
		temp=temp*10;//将12位整数部分转换为实际温度值
		temp=temp+(LSB & 0x0f)*0.625;//将LSB的低四位乘以0.625是为了得到小数部分的实际值
	
	}
}

读出来的温度值temp显示到数码管上,需要单个数码管显示函数Display和多个数码管显示函数DisplaySmg_temp。为了消除显示重叠或显示不完整情况,最好再写一个DisplayAll函数,每次读取完一次温度后,将所有的数码管关掉。


void Display(unsigned char value,unsigned char pos)
{
	SELECT_HC138(7);
	P0=0xff;	
	SELECT_HC138(6);
	P0=0x01<<pos;
	SELECT_HC138(7);
	P0=value;
}


void DisplayAll(unsigned char dat)
{
	SELECT_HC138(6);
	P0=0xff;
	SELECT_HC138(7);
	P0=dat;
}

void DisplaySmg_temp()
{
	
	Display(SMGNoDot_CA[(temp%10)%10],7);
	Delay(100);
	Display(SMGDot_CA[((temp/10)%10)%10],6);
	Delay(100);
	Display(SMGNoDot_CA[((temp/100)%10)%10],5);
	Delay(100);
	Display(0xff,4);
	Delay(100);
	Display(0xff,3);
	Delay(100);
	Display(0xff,2);
	Delay(100);
	Display(0xff,1);
	Delay(100);
	Display(0xff,0);
	Delay(100);
	DisplayAll(0xff);//消除显示重叠或显示不完整情况
}

void Delay(unsigned int t)
{
	while(t--);
}

另外再加上一个系统初始化函数,再将各个部分放到main函数中就可以了

ds12b20.c的完整代码

#include <REGX52.H>
#include "onewire.h"

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 SELECT_HC138(unsigned char n)
	{

  switch(n)
{
		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;

}
	}


void Init_Temp()			//初次温度读取,避免读取默认值85
{
	unsigned char LSB,MSB;
	init_ds18b20();			//复位
	Write_DS18B20(0xcc);	//跳过ROM
	Write_DS18B20(0x44);	//转换温度
	
	do{
		init_ds18b20();			//复位
		Write_DS18B20(0xcc);	//跳过ROM
		Write_DS18B20(0xbe);	//读暂存器
		
		LSB = Read_DS18B20();	//先读取低8位
		MSB = Read_DS18B20();	//后读取高8位
		
		MSB = (MSB << 4) | (LSB >> 4);
	}while(MSB == 85);			//直到首次温度转换完成
								//避免上电后读取温度默认值85
}


void Delay(unsigned int t)
{
	while(t--);
}

void Display(unsigned char value,unsigned char pos)
{
	SELECT_HC138(7);
	P0=0xff;	
	SELECT_HC138(6);
	P0=0x01<<pos;
	SELECT_HC138(7);
	P0=value;
}


void DisplayAll(unsigned char dat)
{
	SELECT_HC138(6);
	P0=0xff;
	SELECT_HC138(7);
	P0=dat;
}

void DisplaySmg_temp()
{
	
	Display(SMGNoDot_CA[(temp%10)%10],7);
	Delay(100);
	Display(SMGDot_CA[((temp/10)%10)%10],6);
	Delay(100);
	Display(SMGNoDot_CA[((temp/100)%10)%10],5);
	Delay(100);
	Display(0xff,4);
	Delay(100);
	Display(0xff,3);
	Delay(100);
	Display(0xff,2);
	Delay(100);
	Display(0xff,1);
	Delay(100);
	Display(0xff,0);
	Delay(100);
	DisplayAll(0xff);//消除显示重叠或显示不完整情况
}


void Read_ds18b20_temp()
{
	unsigned char LSB,MSB;
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0x44);

	
	init_ds18b20();
	Write_DS18B20(0xcc);
	Write_DS18B20(0xbe);
	LSB=Read_DS18B20();
	MSB=Read_DS18B20();
	init_ds18b20();

	
	temp=MSB;
	temp=(temp<<8)|LSB;//合并为一个十六位的值
	
	if((temp & 0xf800)==0x0000)//判断温度是否为正数
	{
		temp>>=4;//温度值右移四位,temp是16位的有符号整数,最高四位位小数部分,剩下12位是整数部分
		temp=temp*10;//将12位整数部分转换为实际温度值
		temp=temp+(LSB & 0x0f)*0.625;//将LSB的低四位乘以0.625是为了得到小数部分的实际值
	
	}
	

}
void InitSystem()
{
	SELECT_HC138(4);
	P0=0xff;//关LED
	SELECT_HC138(5);
	P0=0xff;//关继电器和蜂鸣器
	DisplayAll(0xff);
	Init_Temp();//温度传感初始化
}
void main()
{
	InitSystem();
	while(1)
	{
	DisplaySmg_temp();
	Read_ds18b20_temp();
	}

}
	

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值