【蓝桥杯练习-ds18b20温度】

ds18b20温度练习

内容来自B站小蜜蜂老师

一、操作流程

在这里插入图片描述

二、数据相关处理

在这里插入图片描述

三、代码实现

在编写代码过程中会给出相关驱动程序,所以我们只需掌握
如何更具现有的驱动来实现我们的功能,当然在给出的驱动
代码中也可能会设计一些小细节需要我们自己去修改或者添加
譬如(没有在头文件中添加函数声明、没有定义相关引脚)等等
#include "reg52.h"
#include "absacc.h"
#include "onewire.h"


unsigned char temp = 0x00; //存储温度

//共阳数目管段码
//0-f  0xbf代表 -
unsigned char code SMG_Duan[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,
															   0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0xbf};


//带小数点
unsigned char code distab[] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,
																0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0e};


//------------------------------简单延时-----------------------------
void Delay(unsigned int t)
{
 	while(t--);
}
//-------------------------------------------------------------------


//---------------------------74HC138初始化---------------------------
void Init_74HC138(unsigned char n)
{
 	switch(n)
	{
	 	case 4:P2 = (P2 | 0x1f) ; P2 = 0x80; break;
		case 5:P2 = (P2 | 0x1f) ; P2 = 0xa0; break;
		case 6:P2 = (P2 | 0x1f) ; P2 = 0xc0; break;
		case 7:P2 = (P2 | 0x1f) ; P2 = 0xe0; break;  
	}
}


void InitSystem(void)
{
 	  Init_74HC138(5);
	  P0 = 0x00; //关闭继电器和蜂鸣器
		Init_74HC138(4);
		P0 = 0xff; //关闭led
}
//------------------------------------------------------------


//-----------------------------数码管功能实现------------------
void SMG_DisplayBit(unsigned char pos ,unsigned char dat)
{
	Init_74HC138(6);
	P0 = 0x01 << pos;
	Init_74HC138(7);
	P0 = dat;	
}

void SMG_CLOSEALL(unsigned char dat)
{
 	Init_74HC138(6);
	P0 = 0xff;
	Init_74HC138(7);
	P0 = dat;
}


void SMG_DisplayDat(void)
{
 	 SMG_DisplayBit(5,SMG_Duan[temp/100]);  //百位
	 Delay(200);
	 SMG_DisplayBit(6,SMG_Duan[temp/10%10]); //十位
	 Delay(200);
	 SMG_DisplayBit(7,SMG_Duan[temp%10]);		 //个位
	 Delay(200);
	 SMG_DisplayBit(0,0xff);
	 Delay(200);
	 SMG_DisplayBit(1,0xff);
	 Delay(200);
	 SMG_DisplayBit(2,0xff);
	 Delay(200);
	 SMG_DisplayBit(3,0xff);
	 Delay(200);
	 SMG_DisplayBit(4,0xff);
	 Delay(200);
	 SMG_CLOSEALL(0xff);
	 Delay(200);

}
//-----------------------------------------------------------


//-------------------------------温度采集处理-----------------

void SMG_Delay(unsigned int t)
{

 	while(t--)
	{
		SMG_DisplayDat();
	}
}


void Temp_Tackle(void)
{
	unsigned char LSB ,MSB;		 //分别存储低第0字节和第一字节
 	init_ds18b20();	 //DS18B20复位初始化
	Write_DS18B20(0xcc);  //跳过ROM指令
	Write_DS18B20(0x44);  //开始温度转换
	SMG_Delay(800);	 //延时700-900ms

	init_ds18b20();	//DS18B20复位初始化
	Write_DS18B20(0xcc);  //跳过ROM指令
	Write_DS18B20(0xbe);	 //读取高速暂存器

	LSB = Read_DS18B20(); //暂存器第0字节
	MSB = Read_DS18B20();//暂存器第1字节
	temp = MSB;
	temp = (temp << 8) | LSB;			//整体16字节数据
	temp = temp >> 4;	 //移除小数部分
	//下面为显示小数处理
 /*
	if((temp & 0xf800) == 0x0000)
	{
	 	temp >>= 4; //将小数移除
		temp = temp *10;
		temp = temp + (LSB & 0x0f)*0.625;
	}
	*/
}
//----------------------------------------------------------

void main(void)
{
	InitSystem();
	while(1)
	{
		Temp_Tackle();
		SMG_DisplayDat();
	}

}
在本次学习中,掌握了对ds18b20的基础使用在听老师讲解过程中才发现其实实现温度采集并不难,不过还是希望自己可以去掌握驱动代码的编写,虽然会提供但在学习中我们更应该知其所有然。
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小殷学长

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

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

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

打赏作者

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

抵扣说明:

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

余额充值