蓝桥杯电子类单片机组模块——温度传感器

在蓝桥杯电子类单片机组历年的省赛以及国赛中,温度传感器模块常常被考察到。而组委会为减轻考生负担,提前给出了关于温度传感器模块的代码,剩下的便需要我们自己去编写(以第十届为例)。

现在,我们来介绍以下温度传感器模块剩余代码的编写以及模块的运用方法。
1、补全模块C文件

#include "reg52.h"

sbit DQ = P1^4;  


//************************下面的代码需要自己写***********************//
void Delay_OneWire(unsigned int t)  //STC89C52RC
{
	int 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;
}
//************************下面的代码需要自己写***********************//
float rd_temprature()
{
		unsigned char low,high;
		unsigned int temp;
		float temprature;
	
		init_ds18b20();
		Write_DS18B20(0xcc);
		Write_DS18B20(0x44);
		Delay_OneWire(200);
	
		init_ds18b20();
		Write_DS18B20(0xcc);
		Write_DS18B20(0xbe);
	
		low=Read_DS18B20();
		high=Read_DS18B20();
	
		temp=(low|(high<<8));
		temprature=temp*0.0625;
		return temprature;
		

}
//************************上面的代码需要自己写***********************//

2、补全模块H文件

#ifndef __ONEWIRE_H
#define __ONEWIRE_H
bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
void Delay_OneWire(unsigned int t);  //STC89C52RC

//************************下面的代码需要自己写***********************//
float rd_temprature();
//************************上面的代码需要自己写***********************//

#endif

3、在主函数中运用温度传感器模块

#include "STC15F2K60S2.H"
#include "onewire.h"
#include "intrins.h"

#define u8 unsigned char 

u8 code t_display[]={                       //????
//   0    1    2    3    4    5    6    7    8    9    A    B    C    D    E    F
    0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black  -     H    J    K    L    N    o   P    U     t    G    Q    r   M    y
    0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
    0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};    //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1

u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};      //??

void Timer0Init(void)		//[email protected]
{
	AUXR |= 0x80;		//?????1T??
	TMOD &= 0xF0;		//???????
	TL0 = 0xCD;		//??????
	TH0 = 0xD4;		//??????
	TF0 = 0;		//??TF0??
	TR0 = 1;		//???0????
	ET0 = 1;
	EA  = 1;
}

void Delay100ms()		//延时100ms,为新添加函数
{
	unsigned char i, j, k;

	_nop_();
	_nop_();
	i = 5;
	j = 52;
	k = 195;
	do
	{
		do
		{
			while (--k);
		} while (--j);
	} while (--i);
}

u8 temprature_display[8];
void main()
{
		u8 i;
		u8 temp;
		P0=~0x00;P2=0X80;P2=0X00;
		P2=0XA0;P0=0X00;P2=0X00;
		for(i=0;i<=10;i++)
		{
			temp=(u8)rd_temprature();
		}

	
		Timer0Init();
	
		while(1)
		{
			temp=(u8)rd_temprature();
			temprature_display[0]=t_display[temp/100];
			temprature_display[1]=t_display[temp/10%10];
			temprature_display[2]=t_display[temp%10];
		}
}


void Timer0()	interrupt 1
{
		static int smg_count=0,i=0;
		smg_count++;
		
		if(smg_count==2)
		{
			smg_count=0;
			P2=0XC0;P0=0;P2=0;
			P2=0XE0;P0=~temprature_display[i];P2=0;
			P2=0XC0;P0=T_COM[i];P2=0;
			i++; 
			if(i==8){i=0;}


		}


}

4、另外,在运用温度传感器模块的时候还涉及到了数码管模块。大家可以参照下面的blog链接:https://blog.csdn.net/qq_44629819/article/details/104490649

祝大家在蓝桥杯比赛中获得优异的成绩!
——南昌大学 电子183刘昊

发布了10 篇原创文章 · 获赞 9 · 访问量 813

猜你喜欢

转载自blog.csdn.net/qq_44629819/article/details/104522903