Design of DS18B20 temperature control heating and cooling system based on 51 single chip microcomputer

1 Development environment

Simulation diagram: proteus8.9 or above

Program code: KEIL4/KEIL5

Schematic: AD

Design number: A0007

2 Function Description Introduction

Combined with the actual situation, a queuing and calling system is designed based on 51 single-chip microcomputer. The functional requirements that the system should meet are:

The system consists of 51 single-chip microcomputers, DS18B20 temperature sensors, LCD1602 liquid crystal display screens, relays, fans, heating films, buttons, and buzzers.

The following basic functions can be realized:

1. It can display the temperature value in real time with an accuracy of 0.1 degrees Celsius;

2. Use the digital tube as the display device;

3. The detection range is -55~125 degrees Celsius;

4. The temperature alarm range can be set by pressing the button;

5. Once the temperature is too low, the green indicator light is on, the buzzer sounds, the heating film is heated, and the temperature rise device is simulated;

6. Once the temperature is too high, the red indicator light will be on, the buzzer will sound, and the small fan will rotate, simulating the cooling device;

Download link of this material (clickable)

https://docs.qq.com/doc/DTk5xUXRZdHFqem5O

3 Simulation diagram

AT89C51 can be replaced by AT89C52, and the real product can be replaced by STC89C51, STC89C52

AT89C51 is a low-voltage, high-performance CMOS 16-bit single-chip microcomputer produced by ATMEL Corporation of the United States. It contains 4k bytes of rewritable read-only program memory and 128 bytes of random access data memory. Produced with volatile storage technology, compatible with standard MCS-51 instruction system, built-in general-purpose 16-bit CPU and Flash storage unit, powerful AT89C51 single-chip microcomputer can be flexibly used in various control fields.

AT89C51 provides the following standard functions: 4k bytes of Flash memory, 128 bytes of internal RAM, 32 I/O lines, two 16-bit timers/counters, a 5-vector two-level interrupt structure, and a full-duplex serial Line communication port, on-chip oscillator and clock circuit. At the same time, AT89C51 can drop to 0Hz static logic operation, and supports two software-selectable power-saving operating modes. The idle mode stops the work of CPU, but allows RAM, timer/counter, serial communication port and interrupt system to continue to work. The power-down mode saves the content in RAM, but the oscillator stops working and prohibits all other components from working until the next hardware reset.

The central controller of this system adopts AT89C51 single-chip microcomputer, and the reset circuit adopts power-on reset circuit. The external crystal oscillator is a 12MHz crystal oscillator.

img

4 programs

The project file is opened with Keil4/keil5. Compile and generate hex and load it into the corresponding microcontroller.

img

the code

#include <reg52.h>			// 包含头文件
#include <intrins.h>

#define uchar unsigned char		// 以后unsigned char就可以用uchar代替
#define uint  unsigned int		// 以后unsigned int 就可以用uint 代替

sbit DQ = P1^1;						// DS18B20传感器的引脚定义
sbit w1 = P2^4; 					// 数码管第1位的控制引脚
sbit w2 = P2^5;						// 数码管第2位的控制引脚
sbit w3 = P2^6;						// 数码管第3位的控制引脚
sbit w4 = P2^7;			 			// 数码管第4位的控制引脚
sbit Buzzer  = P1^0;			// 蜂鸣器引脚
sbit JdqLow  = P2^0;			// 温度过低继电器控制(加热)
sbit JdqHig  = P2^1;			// 温度过高继电器控制(降温)
sbit LedLow  = P2^2;			// 温度低指示灯
sbit LedHig  = P2^3;			// 温度高指示灯
sbit KeySet  = P3^2;			// 设置按键
sbit KeyDown = P3^3;			// 减按键
sbit KeyUp   = P3^4;			// 加按键



/*   数码管的显示值:  0    1    2    3    4    5     6   7    8    9    -   */
uchar code Array1[]={ 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40 };

/*                     0.   1.   2.   3.   4.   5.    6.  7.   8.   9.  */
uchar code Array2[]={ 0xBf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef };


uchar Buff[4];					// 显示缓冲区
uchar ShowID=1;					// 当前显示的是哪一个数码管

int AlarmLow=150;				// 默认报警的温度下限值是15度
int AlarmHig=300;				// 默认报警的温度上限值是30度


/*********************************************************/
// 毫秒级的延时函数,time是要延时的毫秒数
/*********************************************************/
void DelayMs(uint time)
{
	uint i,j;
	for(i=0;i<time;i++)
		for(j=0;j<112;j++);
}


/*********************************************************/
// 延时15微秒
/*********************************************************/
void Delay15us(void)
{
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
}


/*********************************************************/
// 复位DS18B20(初始化)
/*********************************************************/
void DS18B20_ReSet(void)
{
	uchar i;
	DQ=0;

	i=240;
	while(--i);

	DQ=1;

	i=30;
	while(--i);

	while(~DQ);

	i=4;
	while(--i);
}


/*********************************************************/
// 向DS18B20写入一个字节
/*********************************************************/
void DS18B20_WriteByte(uchar dat)
{
	uchar j;
	uchar btmp;
	
	for(j=0;j<8;j++)
	{
		btmp=0x01;
		btmp=btmp<<j;
		btmp=btmp&dat;
		
		if(btmp>0)		// 写1
		{
			DQ=0;
			Delay15us();
			DQ=1;
			Delay15us();
			Delay15us();
			Delay15us();
			Delay15us();
		}
		else			// 写0
		{
			DQ=0;
			Delay15us();
			Delay15us();
			Delay15us();
			Delay15us();
			DQ=1;
			Delay15us();
		}
	}
}


/*********************************************************/
// 读取温度值
/*********************************************************/
int DS18B20_ReadTemp(void)
{
	uchar j;
	int b,temp=0;	

	DS18B20_ReSet();							// 产生复位脉
	DS18B20_WriteByte(0xcc);			// 忽略ROM指令
	DS18B20_WriteByte(0x44);			// 启动温度转换指令

	DS18B20_ReSet();							// 产生复位脉
	DS18B20_WriteByte(0xcc);			// 忽略ROM指令
	DS18B20_WriteByte(0xbe);			// 读取温度指令

	for(j=0;j<16;j++)							// 读取温度数量
	{						
		DQ=0;
		_nop_();
		_nop_();
		DQ=1;	
		Delay15us();
		b=DQ;
		Delay15us();
		Delay15us();
		Delay15us();
		b=b<<j;
		temp=temp|b;
	}
	
	temp=temp*0.0625*10;					// 合成温度值并放大10倍					
	
	return (temp);								// 返回检测到的温度值
}


/*********************************************************/
// 定时器初始化
/*********************************************************/
void TimerInit()
{
	TMOD = 0x01;					// 使用定时器0,工作方式1	 
	TH0  = 248;						// 给定时器0的TH0装初值
	TL0  = 48;						// 给定时器0的TL0装初值
	ET0  = 1;							// 定时器0中断使能
	EA   = 1;							// 打开总中断
	TR0	 = 1;							// 启动定时器0
}


/*********************************************************/
// 显示温度值
/*********************************************************/
void ShowTemp(int dat)
{
	if(dat<0)												// 负号
	{
		Buff[0]=Array1[10];
		dat=0-dat;
	}
	else														// 百位
	{
		Buff[0]=Array1[dat/1000];
	}
	
	Buff[1]=Array1[dat%1000/100];		// 十位
	Buff[2]=Array2[dat%100/10];			// 个位
	Buff[3]=Array1[dat%10];					// 小数后一位
}


/*********************************************************/
// 报警判断
/*********************************************************/
void AlarmJudge(int dat)
{
	if(dat<AlarmLow)				// 判断温度是否过低
	{
		LedLow=0;							// 温度低指示灯亮
		LedHig=1;							// 温度高指示灯灭
		JdqLow=0;							// 温度过低的继电器闭合(开始加热)
		JdqHig=1;							// 温度过高的继电器断开(停止降温)
		Buzzer=0;							// 蜂鸣器报警
	}
	else if(dat>AlarmHig)		// 判断温度是否过高
	{
		LedLow=1;							// 温度低指示灯灭
		LedHig=0;							// 温度高指示灯亮
		JdqLow=1;							// 温度过低的继电器断开(停止加热)
		JdqHig=0;							// 温度过高的继电器闭合(开始降温)
		Buzzer=0;							// 蜂鸣器报警
	}
	else										// 温度正常
	{
		LedLow=1;							// 温度低指示灯灭
		LedHig=1;							// 温度高指示灯灭
		JdqLow=1;							// 温度过低的继电器断开(停止加热)
		JdqHig=1;							// 温度过高的继电器断开(停止降温)
		Buzzer=1;							// 蜂鸣器停止报警
	}
}


/*********************************************************/
// 按键扫描
/*********************************************************/
void KeyScanf()
{
	if(KeySet==0)									// 如果设置按键被按下
	{
		/* 设置温度下限 */
		LedLow=0;										// 点亮绿色灯(代表当前正在设置温度下限)
		LedHig=1;										// 熄灭红色灯
		Buzzer=1;										// 关闭蜂鸣器
		ShowTemp(AlarmLow);					// 显示温度下限值
		DelayMs(10);								// 延时去抖
		while(!KeySet);							// 等待按键释放
		DelayMs(10);								// 延时去抖
		
		while(1)
		{
			if(KeyDown==0)								// 如果“减”按键被按下
			{
				if(AlarmLow>-550)						// 判断当前温度下限是否大于-55度
				{
					AlarmLow--;								// 温度下限值减去0.1度
					ShowTemp(AlarmLow);				// 刷新显示改变后的温度下限值
					DelayMs(200);							// 延时
				}
			}
			
			if(KeyUp==0)									// 如果“加”按键被按下					
			{
				if(AlarmLow<1250)						// 判断当前温度下限是否小于125度
				{
					AlarmLow++;								// 温度下限值加上0.1度
					ShowTemp(AlarmLow);				// 刷新显示改变后的温度下限值
					DelayMs(200);							// 延时
				}
			}
			
			if(KeySet==0)									// 如果“设置”按键被按下
			{
				break;											// 退出温度下限值的设置,准备进入上限值的设置
			}
		}
		
		/* 设置温度上限 */
		LedLow=1;										// 熄灭绿色灯
		LedHig=0;										// 点亮红色灯(代表当前正在设置温度上限)
		ShowTemp(AlarmHig);					// 显示温度上限值
		DelayMs(10);								// 延时去抖
		while(!KeySet);							// 等待按键释放
		DelayMs(10);								// 延时去抖
		
		while(1)
		{
			if(KeyDown==0)							// 如果“减”按键被按下							
			{
				if(AlarmHig>-550)					// 判断当前温度上限是否大于-55度
				{
					AlarmHig--;							// 温度上限值减去0.1度
					ShowTemp(AlarmHig);			// 刷新显示改变后的温度上限值
					DelayMs(200);						// 延时
				}
			}
			
			if(KeyUp==0)								// 如果“加”按键被按下					
			{
				if(AlarmHig<1250)					// 判断当前温度上限是否小于125度
				{
					AlarmHig++;							// 温度上限值加上0.1度
					ShowTemp(AlarmHig);			// 刷新显示改变后的温度上限值
					DelayMs(200);
				}
			}
			
			if(KeySet==0)								// 如果“设置”按键被按下
			{
				break;										// 准备退出设置模式
			}
		}
		
		/* 退出设置模式 */
		LedLow=1;									// 熄灭绿灯
		LedHig=1;									// 熄灭红灯
		DelayMs(10);							// 延时去抖
		while(!KeySet);						// 等待按键释放
		DelayMs(10);							// 延时去抖
	}
}


/*********************************************************/
// 主函数
/*********************************************************/
void main()
{
	int temp;
	uchar i;
	
	TimerInit();										// 定时器0的初始化(数码管的动态扫描)
	
	Buff[0]=Array1[0];							// 刚上电显示4个0
	Buff[1]=Array1[0];
	Buff[2]=Array1[0];
	Buff[3]=Array1[0];
	
	for(i=0;i<8;i++)								// 由于传感器刚上电读出的温度不稳定,因此先进行几次温度的读取并丢弃
	{
		DS18B20_ReadTemp();
		DelayMs(120);
	}
	
	while(1)
	{
		EA=0;													// 屏蔽中断
		temp=DS18B20_ReadTemp();			// 读取温度值
		EA=1;													// 恢复中断
		
		ShowTemp(temp);								// 显示温度值
		
		AlarmJudge(temp);							// 判断是否需要报警
		
		for(i=0;i<100;i++)						// 延时并进行按键扫描
		{
			KeyScanf();					
			DelayMs(10);								
		}
	}
}


/*********************************************************/
// 定时器0服务程序
/*********************************************************/
void Timer0(void) interrupt 1
{
	TH0  = 248;				// 给定时器0的TH0装初值
	TL0  = 48;				// 给定时器0的TL0装初值

	P0=0x00;					// 先关闭所有显示
	w1=1;
	w2=1;
	w3=1;
	w4=1;

	if(ShowID==1)  			// 判断是否显示第1位数码管
	{
		w1=0;		   				// 打开第1位数码管的控制开关
		P0=Buff[0];	   		// 第1位数码管显示内容	
	}
	
	if(ShowID==2)  			// 判断是否显示第2位数码管
	{
		w2=0;		   				// 打开第2位数码管的控制开关
		P0=Buff[1];	   		// 第2位数码管显示内容	
	}
	
	if(ShowID==3)  			// 判断是否显示第3位数码管
	{
		w3=0;		   				// 打开第3位数码管的控制开关
		P0=Buff[2];	   		// 第3位数码管显示内容	
	}
	
	if(ShowID==4)  			// 判断是否显示第4位数码管
	{
		w4=0;		   				// 打开第4位数码管的控制开关
		P0=Buff[3];	   		// 第4位数码管显示内容	
	}	
	
	ShowID++;	  				// 切换到下一个数码管的显示
	if(ShowID==5)
		ShowID=1;
}

The flowchart is shown in the figure below.

img

5 Schematic

The schematic diagram is drawn by AD, and there are differences between the schematic diagram and the simulation diagram. The schematic diagram requires a power supply and a power switch module. The design information is detailed, the hardware manual information and pictures are detailed, and I am not responsible for hardware debugging, and some basic skills are required to make the real thing. The main control chip can be replaced by STC89C51/STC89C52

img

The whole system uses the AT89C51 single-chip microcomputer as the core device, and cooperates with the resistor, capacitor and crystal oscillator to form the minimum system of the single-chip microcomputer. The other modules revolve around the minimum system of single-chip microcomputer. Among them, the sensor adopts DS18B20, which is responsible for collecting temperature data and sending it to the microcontroller. The display device adopts 4 common negative digital tubes to display the detected temperature value. The button module is mainly used to set the alarm value. The alarm module adopts the mode of buzzer + LED, and it will give an audible and visual alarm if it exceeds the alarm range. At the same time, there are heating and cooling devices to keep the temperature within a certain range.

img

6 video explanation

Code explanation + simulation explanation + simulation demonstration + schematic diagram explanation

Design of DS18B20 temperature control heating and cooling system based on 51 single chip microcomputer

7 Design report

Queuing theory (also known as random service system) is a discipline that studies the law of queuing (or congestion) in the system due to the interference of random factors. It is applicable to all service systems, including public service systems, communication systems, computer systems, etc. . It can be said that any system with congestion phenomenon belongs to the random service system. An object receiving service through the congestion system must go through three links, namely arrival, waiting in line for processing, receiving service and leaving. For example, in a hospital, the queuing process is as follows: the patient receives the queuing number while registering, and then waits in the waiting area; after completing the diagnosis for the previous patient, the doctor calls the next patient in the queue through this system, and the patient can go directly to Empty clinics line up for service.

On the other hand, with the continuous growth of business volume and types of services in the service industry, waiting in line has become a practical problem that people often face. In the business halls of banks, hospitals, telecommunications, taxation, industry and commerce, etc., crowding in front and back, and chaotic waiting in line are commonplace, which affects the quality of service. Therefore, improving the quality of service, establishing a good corporate image, solving the tired queuing phenomenon of customers, and creating a humanized service environment have become urgent problems to be solved. Designing a service system for queuing and drawing numbers can well solve various problems caused by queuing.

This system directly expands the independent keyboard through the AT89C51 single-chip microcomputer to complete the process of queuing and taking the number. The single-chip microcomputer controls the LCD1602 to display the waiting situation in the queue, and controls the buzzer to sound to complete the function of calling the number. The system has a good human-computer interaction interface, simulates queuing management, and scientifically handles various queuing situations. It is easy to operate, flexible in control, clear in display, low in production cost, and high in cost performance.

7.1 Design purpose

(1) Consolidate and deepen the understanding of microcontroller principles and interface technology knowledge;

(2) Cultivate the ability to select reference books, consult manuals and literature according to the needs of the subject;

(3) Learn the comparative methods of program demonstration, broaden knowledge, and initially master the basic methods of engineering design;

(4) Master the correct use of commonly used instruments and meters, and learn how to design and debug software and hardware;

(5) Able to write course design reports according to the requirements of course design, correctly reflect design and experimental results, and draw circuit diagrams, simulation diagrams and flow charts by computer.

7.2 Overview

The 21st century is an information age with rapid development of science and technology, and the application of electronic technology and micro-single-chip microcomputer technology is unprecedentedly extensive. With the continuous development of science and technology and production, it is necessary to measure the temperature of various parameters. Therefore, the word temperature appears more and more frequently in production and life. Correspondingly, temperature measurement and control have also become frequently used words in life and production. At the same time, they also play an important role in all walks of life. For example, in the increasingly developed industries, temperature measurement and control are used to ensure the normal operation of production. In agriculture, it is used to ensure the constant temperature and production of vegetable greenhouses.

Temperature is a physical quantity that characterizes the degree of hot or cold of an object, and temperature measurement is a very important and common parameter in the process of industrial and agricultural production. Temperature measurement and control play a very important role in ensuring product quality, improving production efficiency, saving energy, production safety, and promoting the development of the national economy. Due to the universality of temperature measurement, the number of temperature sensors ranks first among various sensors. And with the continuous development of science and technology and production, the types of temperature sensors are still increasing and enriching to meet the needs of production and life.

Single-chip temperature measurement is an effective measurement of temperature, and can be widely used in industrial production, especially in important fields such as power engineering, chemical production, machinery manufacturing, metallurgical industry, and agriculture. In daily life, it can also be widely used in various household room temperature measurement and industrial equipment temperature measurement occasions such as geothermal heat, air conditioners, and electric heaters.

7.3 Research status at home and abroad

The research on temperature control technology abroad started earlier in the 1970s. First, an analog combination instrument is used to collect on-site information and perform instructions, records and controls. Distributed control systems appeared in the late 1980s. At present, the multi-factor comprehensive control system of the computer data acquisition control system is being developed and developed. In the mid-1990s, the intelligent temperature controller came out, which is the crystallization of microelectronics technology, computer technology and automatic testing technology. At present, a variety of intelligent temperature control product series have been developed in the world. The intelligent temperature controller contains temperature sensors, AD converters, signal processors and interface circuits. Some products also have multiplexers, central controllers, random memory and read-only memory, etc. Now the temperature measurement and control technology in all countries in the world is developing rapidly, and some countries are developing towards complete automation and unmanned on the basis of automation.

my country's research on temperature measurement and control technology is relatively late, starting in the 1980s. On the basis of absorbing the temperature measurement and control technology of developed countries, our country's engineering and technical personnel have mastered the temperature indoor microcomputer control technology, which is limited to the control of a single environmental factor of temperature. The computer application of temperature measurement and control facilities in our country is transitioning and developing from the digestion and absorption and simple application stage to the practical and comprehensive application stage. Technically, most single-parameter and single-loop systems are controlled by single-chip microcomputers, and there is no real multi-parameter integrated control system. Compared with developed countries, there is a big gap. The current situation of temperature measurement and control in our country is far from reaching the level of factoryization. There are still many problems in the actual production that plague us, such as poor equipment matching ability, low degree of industrialization, backward environmental control level, and software and hardware resources that cannot be shared and reliable. Poor sex and other shortcomings.

img

8 Download link of data sheet

See the video at the beginning of the article

img

Download link of this document:

https://docs.qq.com/doc/DTk5xUXRZdHFqem5O

Guess you like

Origin blog.csdn.net/Jack_0220/article/details/128783521