[Single-chip microcomputer graduation project] [mcuclub-hj-001] Temperature control thermostatic box insulating box DS18B20

I recently designed a project based on a temperature control system based on a microcontroller, and I would like to share it with you:

1. Basic introduction

Project name: Temperature control system design based on microcontroller

Design of thermostatic box based on single-chip microcomputer

Design of thermal insulation box based on single-chip microcomputer

Project number: mcuclub-hj-001

Microcontroller type: STC89C52, STM32F103C8T6

Specific functions:

1. Measure the ambient temperature through DS18B20. When the temperature is not within the set upper and lower limits, the corresponding heating and cooling will be performed, and a sound and light alarm will be issued.

2. Set the upper and lower temperature limits by pressing the buttons, and you can manually control heating and cooling and switch modes.

3. Display data through the display screen (STC89C52 uses LCD1602, STM32F103C8T6 uses OLED)

Extended functions: Send measurement data to the mobile phone through the Bluetooth module. The mobile phone can set the upper and lower temperature limits, and can remotely control heating and cooling, and switch modes.

2. 51 actual picture

Microcontroller model: STC89C52

The board is a green PCB board, a two-layer board, 1.2 thick, with copper covering the top and bottom for grounding. The components are basically pin type, and some buck chips will use patch type.

Power supply interface: TYPE-C

Three, 51 simulation diagram

Simulation software version: proteus8.9

Circuit connection method: network label connection method

Note: Some physical components are not included in the simulation. They will be replaced by other components with similar working principles in the simulation. This may cause the physical program to be different from the simulation program.

IV. 32 actual picture

Microcontroller model: STM32F103C8T6

The board is a green PCB board, a two-layer board, 1.2 thick, with copper covering the top and bottom for grounding. The components are basically pin type, and some buck chips will use patch type.

Power supply interface: TYPE-C

5. Schematic diagram

Software version: AD2013

Circuit connection method: network label connection method

Note: The schematic diagram only shows the pin diagram of the module, not the internal structure schematic diagram of the module.

6. PCB diagram

Exported from the schematic diagram, a large part of the package is drawn by the author himself. No package library is provided, only the connected source files are provided. There is a project number in the middle, which is hidden under the base of the microcontroller and will not be seen after inserting the microcontroller.

Two-layer board, top and bottom covered with copper and grounded.

7. System block diagram

This design uses a single-chip microcomputer as the core controller, plus other modules to form the entire system of this design, including the central control part, the input part and the output part. The central control part adopts a single-chip microcomputer controller, whose main function is to obtain the data of the input part, and finally control the output part after internal processing and logical judgment. The input consists of three parts. The first part is the temperature detection module, through which the temperature of the current environment is detected; the second part is the button module, through which the interface can be switched, thresholds set, modes switched, etc.; the third part is the power supply module. The entire system can be powered through this module. The output consists of three parts. The first part is the display module, through which the monitored data and the set threshold can be displayed; the second part is the relay module, which controls heating and cooling respectively through two relays; the third part is the sound and light alarm module , when the monitored value is not within the set threshold, an audible and visual alarm will be issued. In addition, the Bluetooth module serves as both input and output. The Bluetooth module is connected to the mobile phone and can transmit the monitored data to the user’s mobile phone. The user can also send instructions through the mobile phone to control the work of the relay and its mode switching. The specific system block diagram is shown in the figure.

8. Software design process

The main flow chart of the system is shown in the figure. In the main program: first initialize each module, and then enter the while main loop. In the main loop, first enter the first function key function. This function is mainly divided into two parts. The first part is to call the key scan function to obtain the key key. value, the second part performs corresponding processing operations through the key value, including switching interfaces, setting thresholds, etc.; then enters the second function monitoring function, which mainly obtains the measured value by calling the corresponding driver function, and passes the Bluetooth module to The monitored data is transmitted to the mobile phone, and the user can also send instructions through the mobile phone, and the device performs corresponding processing according to the instructions sent by the user; then enters the third function display function, which displays the monitoring value and threshold; finally enters the fourth function. A function processing function, which mainly determines whether the current temperature is within the set threshold. If the temperature is greater than the set maximum value, an audible and visual alarm will be triggered, and the refrigeration relay will be turned on to cool down. If the temperature is less than the set minimum value, an audible and visual alarm will be activated. Alarm, and open the heating relay for heating. If the temperature is within the set threshold, cancel the audible and visual alarm, and close the heating and cooling relays.

9. Partial program display

Software version: keil5

The logic program and driver are separated and distributed in main.c and other .c files

/************************************
include header files
************************************/
#include "main.h"
#include "lcd1602.h"
#include "key.h"
#include "ds18b20.h"
#include "uart.h"
/**********************************
Variable definitions
************************************/
uchar key_num = 0; //key scan flag
uchar flag_display = 0; //Display interface flag bit
uint time_num = 0; //10ms timing variable
uint temp_value = 0; //Temperature value
uint temp_max = 30; //maximum temperature
uint temp_min = 20; //Minimum temperature value
bit flag_mode = 0; //Mode flag bit
uchar temp_buf[8]; //temperature storage value
uchar flag_ctrl = 0; //Relay flag bit
uchar fasong_buf[32]; //Send special array
bit flag_alarm = 0; //alarm flag bit


/**********************************
function declaration
************************************/
void Delay_function(uint x); //Delay function (ms)
void Key_function(void); //Key function
void Monitor_function(void); //Monitoring function
void Display_function(void); //Display function
void Manage_function(void); //processing function


/****
******* Main function
*****/
void main()
{
Lcd1602_Init(); //LCD1602 initialization
Delay_function(50); //Delay 50ms
lcd1602_clean(); //Clear screen
Delay_function(50); //Delay 50ms
Ds18b20_Init(); //DS18B20 initialization
Delay_function(50); //Delay 50ms
Uart_Init(); //Serial port initialization
\t
while(1)
{
Key_function(); //Key function
Monitor_function(); //Monitoring function
Display_function(); //Display function
Manage_function(); //processing function

Delay_function(10); //Delay 10ms
time_num + + ; //Timing variable + 1
if(time_num == 5000)
{
time_num = 0;
}
}
}

/****
******* Delay x ms function
*****/
void Delay_function(uint x)
{
uint m,n;
for(m=x;m>0;m--)
for(n=110;n>0;n--);
}

/****
*******Key function
*****/
void Key_function(void)
{
key_num = Chiclet_Keyboard_Scan(0); //Key scan
if(key_num != 0) //A key is pressed
{
switch(key_num)
{
case 1: //Button 1, switch setting interface
flag_display + + ;
if(flag_display >= 3)
flag_display = 0;
lcd1602_clean(); //Clear screen
break;

case 2: //Button 2
switch(flag_display)
{
case 0: //Interface 0: Manual control of heating and cooling
flag_mode = 1;
flag_alarm = 0;
flag_ctrl + + ;
break;
\t\t\t\t\t
case 1: //Interface 1: maximum temperature + 1
if(temp_max < 99)
temp_max + + ;
break;

case 2: //Interface 2: minimum temperature + 1
if(temp_min < temp_max-1)
temp_min + + ;
break;

default:
break;
}
break;

case 3: //Button 3
switch(flag_display)
{
case 0: //Interface 0: switch mode
flag_mode = 0;
flag_ctrl = 0;
break;
\t\t\t\t\t
case 1: //Interface 1: maximum temperature value -1
if(temp_max > temp_min + 1)
temp_max--;
break;
\t\t\t\t\t
case 2: //Interface 2: minimum temperature value -1
if(temp_min > 0)
temp_min--;
break;
\t\t\t\t\t
default:
break;
}
break;

default:
break;
}
}
}

/****
*******Monitoring function
*****/
void Monitor_function(void)
{
if(time_num % 10 == 0) //Detect once every 100ms
{
temp_value = Ds18b20_Read_Temp(); //Get the temperature value
}
if(time_num % 300 == 0) //Send once every 3000ms
{
sprintf(fasong_buf,"Temp:%d.?\r\
",temp_value/10,temp_value );
Uart_Sent_Str(fasong_buf); //Send temperature
}
}

/****
*******Display function
*****/
void Display_function(void)
{
switch(flag_display) //Display different interfaces according to different display mode flags
{
case 0: //Interface 0:
      lcd1602_display_str(1,0,"Max:"); //Display the maximum temperature value
sprintf(temp_buf,"? ",temp_max);
lcd1602_display_str(1,4,temp_buf);

      lcd1602_display_str(1,8,"Min:"); //Display the minimum temperature value
sprintf(temp_buf,"? ",temp_min);
lcd1602_display_str(1,12,temp_buf);

lcd1602_display_str(2,0,"Temp:"); //Display temperature measurement value
lcd1602_display_temp(2,5,temp_value);

if(flag_mode == 0) //Display mode
lcd1602_display_str(2,12,"Auto");
else
lcd1602_display_str(2,12,"Manu");
break;

case 1: //Interface 1, displays the set maximum temperature
lcd1602_display_str(1,2,"Set Temp Max");
if(time_num % 20 == 0)
{
sprintf(temp_buf,"%d",temp_max);
lcd1602_display_str(2,7,temp_buf);
}
if(time_num % 40 == 0)
{
lcd1602_display_str(2,7," ");
}
break;
\t\t
case 2: //Interface 2, displays the set minimum temperature
lcd1602_display_str(1,2,"Set Temp Min");
if(time_num % 20 == 0)
{
sprintf(temp_buf,"%d",temp_min);
lcd1602_display_str(2,7,temp_buf);
}
if(time_num % 40 == 0)
{
lcd1602_display_str(2,7," ");
}
break;

default:
break;
}
}

/****
*******Handling function
*****/
void Manage_function(void)
{
if(flag_display == 0) //Measurement interface
{
if(flag_mode == 0) //In automatic mode
{
if(temp_value > temp_max*10) //The temperature is greater than the set maximum value, the cooling relay is closed
{
RELAY_ZL = 0;
RELAY_JR = 1;
flag_alarm = 1;
}
else if(temp_value < temp_min*10) //The temperature is less than the set minimum value, the heating relay is closed
{
RELAY_ZL = 1;
RELAY_JR = 0;
flag_alarm = 1;
}
else //The temperature is between the set upper and lower limits, and the two relays are disconnected
{
RELAY_ZL = 1;
RELAY_JR = 1;
flag_alarm = 0;
}
}
else //Manual mode controls the relay according to the button press
{
      flag_alarm = 0;
if(flag_ctrl == 1)
{
RELAY_JR = 0;
RELAY_ZL = 1;
}
else if(flag_ctrl == 2)
{
RELAY_JR = 1;
RELAY_ZL = 0;
}
else
{
flag_ctrl = 0;
RELAY_JR = 1;
RELAY_ZL = 1;
}
}
    if(flag_alarm == 1 & amp; & amp; time_num == 0) //If the temperature is abnormal, sound and light alarm
{
LED = ~LED;
BEEP = ~BEEP;
}
    else if(flag_alarm == 0)
    {
      LED = 1;
      BEEP = 1;
    }
}
else //Set up interface
{
LED = 1;
BEEP = 1;
RELAY_ZL = 1;
RELAY_JR = 1;
}
}

http://www.mcubee.comicon-default.png?t=N7T8http://For information, please enter the official website to view and download: http://www.mcubee.com

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. MySQL entry-level skills treeHomepageOverview 76562 people are learning the system