The use of temperature sensor DS18B20 of Lanqiao cup single-wire communication protocol

1. Experimental tasks

Read the temperature.

2. Principle analysis

There is not much introduction here, if you need to understand the principle, you can move to https://editor.csdn.net/md/?articleId=112068955, MCU review.

3. Experimental process

Read the temperature
1. Build a frame (simple frame, skippable)

#include<stc15f2k60s2.h>
#include<intrins.h>

#define uchar unsigned char
#define uint unsigned int

uchar tab[]={
    
    0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,
							0X40,0X79,0X1A,0X30,0X19,0X12,0X02,0X78,0X00,0X10,0XBF,0XFF};
uchar yi,er,san,si,wu,liu,qi,ba;

void Allinit(void);
void Delayms(int ms);
void Display1(uchar yi,uchar er);
void Display2(uchar san,uchar si);
void Display3(uchar wu,uchar liu);
void Display4(uchar qi,uchar ba);

void main(void)
{
    
    
	Allinit();
	yi=21;er=21;san=21;si=21;wu=21;liu=21;qi=21;ba=21;
	while(1)
	{
    
    
		Display1(yi,er);
		Display2(san,si);
		Display3(wu,liu);
		Display4(qi,ba);
	}
}


void Display1(uchar yi,uchar er)
{
    
    
	P2=0XC0;
	P0=0X01;
	P2=0XE0;
	P0=tab[yi];
	Delayms(1);
	
	P2=0XC0;
	P0=0X02;
	P2=0XE0;
	P0=tab[er];
	Delayms(1);
}

void Display2(uchar san,uchar si)
{
    
    
	P2=0XC0;
	P0=0X04;
	P2=0XE0;
	P0=tab[san];
	Delayms(1);
	
	P2=0XC0;
	P0=0X08;
	P2=0XE0;
	P0=tab[si];
	Delayms(1);
}

void Display3(uchar wu,uchar liu)
{
    
    
	P2=0XC0;
	P0=0X10;
	P2=0XE0;
	P0=tab[wu];
	Delayms(1);
	
	P2=0XC0;
	P0=0X20;
	P2=0XE0;
	P0=tab[liu];
	Delayms(1);
}

void Display4(uchar qi,uchar ba)
{
    
    
	P2=0XC0;
	P0=0X40;
	P2=0XE0;
	P0=tab[qi];
	Delayms(1);
	
	P2=0XC0;
	P0=0X80;
	P2=0XE0;
	P0=tab[ba];
	Delayms(1);
}

void Allinit(void)
{
    
    
	P2=0XA0;//打开控制蜂鸣器的573
	P0=0X00;//关闭蜂鸣器继电器
	
	P2=0XC0;//打开控制数码管位选的573
	P0=0XFF;//选中所有数码管
	P2=0XE0;//打开控制数码管段选的573
	P0=0XFF;//关闭所有的数码管
	
	P2=0X80;//打开控制LED的573
	P0=0XFF;//关闭所有的LED
}

void Delayms(int ms)
{
    
    
	int i,j;
	for(i=0;i<ms;i++)
		for(j=845;j>0;j--);
}


2. Load the driver, the official driver code will be provided, if these driver codes are under the main function, remember to declare in advance.


void Init_DS18b20(void)
{
    
    
	DQ=0;
	Delay500us();
	DQ=1;
	Delay500us();
}

void DS18B20_WiteByte(unsigned char dat)
{
    
    
	unsigned char i;
	for(i=0;i<8;i++)
	{
    
    
		DQ=0;
		DQ=dat&0x01;//1100 1100 & 0000 0001 = 0000 0000
		Delay80us();
		DQ=1;
		dat >>=1; //  0110 0110  //0011 0011 // 0000 0000
 	}
}

unsigned char DS18B20_ReadByte(void)
{
    
    
	unsigned char i;
	unsigned char dat;//0000 0000 
	
	for(i=0;i<8;i++)
	{
    
    
		DQ=0;
		dat >>=1;					 // 0101 0000 
		DQ=1;
		if(DQ==1)
		{
    
    
			dat=dat|0x80;  // 0000 0000 | 1000 0000 = 1000 0000
		}
		Delay80us();
	}
	
	return dat;
}

3. Add newly defined functions and variables

sbit DQ=P1^4;//温度传感器的数据引脚
uchar num;
long Wendu;
unsigned int tt=0;


void Delay500us();
void Delay100us();
void Delay80us();//自定义延时

long TemperGet(void);//获取数据,并进行转换
long TemperGet(void)
{
    
    
	unsigned char low,high;
	long temp;
		
	Init_DS18b20();
	DS18B20_WiteByte(0XCC);//1100 1100 
	DS18B20_WiteByte(0X44);
	Delay500us();
	
	Init_DS18b20();
	DS18B20_WiteByte(0XCC);
	DS18B20_WiteByte(0XBE);
	
	low=DS18B20_ReadByte();
	high=DS18B20_ReadByte();
	
//	temp=high<<4;  //0000 1010;  1010 0000
//	temp=temp|(low>>4);		// 1010 0110 ;;  0000 1010   1010 1010

//0.0625	
	temp=(high&0x0f);  //0000 1010;  1010 0000
	temp <<=8;
	temp|=low;		// 1010 0110 ;;  0000 1010   1010 1010
	
	temp=temp*625;

	
	return temp;
}

void Delay80us()		//@11.0592MHz
{
    
    
	unsigned char i, j;

	_nop_();
	i = 1;
	j = 217;
	do
	{
    
    
		while (--j);
	} while (--i);
}

void Delay100us()		//@11.0592MHz
{
    
    
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 2;
	j = 15;
	do
	{
    
    
		while (--j);
	} while (--i);
}


void Delay500us()		//@11.0592MHz
{
    
    
	unsigned char i, j;

	_nop_();
	_nop_();
	i = 6;
	j = 93;
	do
	{
    
    
		while (--j);
	} while (--i);
}

4. To realize the function, just call and display it in the main function

while(1){
    
    
		Wendu=TemperGet();
		yi=Wendu/10;er=Wendu%10;	
}

Summary
In this article, the temperature sensor has been learned and used in conjunction with the digital tube. The use of the digital tube also includes the display with a decimal point. Uncomment the several lines of code commented out in the above TempGet function, and comment out the few lines of code with similar functions. The value obtained by the main function summary should be divided by 1000 times on the original basis.

—————————————————————————————————————————
This article is purely original, if there is any infringement, please contact to delete, if there is any error, please criticize and correct, thank you all.

Guess you like

Origin blog.csdn.net/G_Shengn/article/details/116068764