Design and production of temperature measurement alarm system based on 51 microcontroller

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.

Article directory

  • Preface
  • 1. Purpose of internship
  • 2. Internship tasks
    • 2.1 Design the hardware circuit of the temperature measurement alarm system
    • 2.2 Temperature measurement and alarm system software programming, simulation and debugging;
    • 2.3 Complete the physical production and debugging of the temperature measurement and alarm system;
  • 3. Internship Content and Internship Results
    • 3.1 Functional requirements of temperature measurement alarm system
    • 3.2 Design method of temperature measurement alarm system
  • 3.3 Design process of temperature measurement alarm system
    • 3.3.1 proteus simulation schematic diagram
    • 3.3.2 Programming
  • 3.4 Internship results of temperature measurement alarm system
    • 3.4.1 Simulation results
    • 3.4.2 Physical results
  • 4. Summary
  • Experimental reference materials (gitee)

Foreword

This system uses keil5 and Proteus software to finally implement a small project of temperature detection and alarm on the hardware (simulation can also be achieved using only Proteus)

Tips: The following is the text of this article, the following cases are for reference

1. Purpose of internship

1. Master 51 microcontroller application programming and debugging;
2. Master Proteus circuit diagram design and simulation;
3. Master the use of digital temperature sensor DS18B20;
4. Master the basic characteristics and communication process of the single-bus protocol, and master the single-bus timing control program of the microcontroller IO port simulation;
5. Cultivate the ability to comprehensively apply knowledge and engineering design.

2. Internship tasks

2.1 Design the temperature measurement alarm system hardware circuit

(1) Make the correct selection of components;
(2) Complete the design of each module circuit and system circuit in proteus;
(3) Conduct circuit simulation and testing in Proteus;

2.2 Temperature measurement and alarm system software programming, simulation and debugging;

(1) Implement functions such as thermometer measurement, display, alarm, and upper and lower limit settings based on Keil;
(2) Carry out software simulation and debugging;

2.3 Complete the physical production and debugging of the temperature measurement and alarm system;

(1) Carry out physical welding of the thermometer;
(2) Actual testing and final debugging to complete the work.

3. Internship content and internship results

3.1 Functional requirements of temperature measurement alarm system

The microcontroller detects the status of the temperature sensor DS18B20 chip in real time and processes the data obtained by the DS18B20 chip. After powering on, the digital tube displays the current ambient temperature, and the buzzer sounds once to prompt for power on. S1 serves as the reset button, S2 and S3 serve as the temperature adjustment buttons, and S4 serves as the mode selection button. After pressing it, you can choose to adjust the upper temperature limit H or the lower limit L. When pressed for the third time, the digital tube resumes displaying the real-time temperature. When the detected temperature is higher or lower than the set alarm value, the buzzer will sound and the alarm light will flash. The temperature detection is accurate to 0.1 degrees. The data is stored in the internal EEPOM of the microcontroller.

3.2 Design method of temperature measurement alarm system

First, carry out proteus hardware design for the following six modules. After the hardware schematic is completed, the software design is carried out according to the modules. After the software is designed, load the program into proteus for simulation to test whether the program can normally achieve the expected functions. After the expected function is achieved, the physical object can be welded, and finally the program can be burned into the physical object and debugged again. Finally achieve the desired goal.
Design Process

3.3 Design process of temperature measurement alarm system

3.3.1 proteus simulation schematic

3.3.2 Programming

#include <REGX52.h> //Call the microcontroller header file
#define uint unsigned int
typedef unsigned char uchar;
uint Sign_Negative = 0; //Default is positive number

//Nigital tube segment selection definition 0 1 2 3 4 5 6 7 8 9 H L
uchar code smg_du[]={<!-- -->0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x89,0xC7}; //segment code

//Digital tube bit selection definition
uchar code smg_we[]={<!-- -->0x10,0x20,0x40,0x80};
uchar dis_smg[4] = {<!-- -->0};

sbit DQ = P2^4; //18b20 IO port definition
sbit beep = P2^3; //buzzer IO port definition

uint temperature; //
bit flag_300ms=1;
uchar menu_1; //Menu design variables
uint t_high = 450,t_low = 150; //upper and lower temperature limit alarm values

/******************************1ms delay function********************** *******/
void delay_1ms(uint q)
{<!-- -->
uint i,j;
for(i=0;i<q;i + + )
for(j=0;j<120;j + + );
}

/******************************Small delay function********************** *******/
void delay_uint(uint q)
{<!-- -->
while(q--);
}


/******************************Digital display function************************ ******/
void display()
{<!-- -->
static uchar i;
i + + ;
if(i >= 4)
i = 0;
P1 = 0xff; //blanking
P3 = smg_we[i]; //bit selection
P1 = dis_smg[i]; //Segment selection
}



/******************************18b20 initialization function****************************** ******/
void init_18b20()
{<!-- -->
bit q;
DQ = 1; //Take the bus high
delay_uint(1);
DQ = 0; //Give reset pulse
delay_uint(80);
DQ = 1; //Put the bus high and wait
delay_uint(10);
q = DQ; //Read 18b20 initialization signal
delay_uint(20);
DQ = 1; //Put the bus high and release the bus
}

/****************Write the data in 18b20******************/
void write_18b20(uchar dat)
{<!-- -->
uchar i;
for(i=0;i<8;i + + )
{<!-- --> //Writing data starts with the low bit
DQ = 0; //Bring the bus low and the write time gap begins
DQ = dat & amp; 0x01; //Write data to 18b20 bus
delay_uint(5);
DQ = 1; //Release the bus
dat >>= 1;
}
}

/******************Read the data in 18b20******************/
uchar read_18b20()
{<!-- -->
uchar i,value;
for(i=0;i<8;i + + )
{<!-- -->
DQ = 0; //Bring the bus low and the read time gap begins
value >>= 1; //read data starting from the low bit
DQ = 1; //Release the bus
if(DQ == 1) //Start reading and writing data
value |= 0x80;
delay_uint(5);
}
return value; //return data
}

/******************Read the temperature value. What is read is a decimal***************/
uint read_temp()
{<!-- -->
    uint wendu = 0;
uchar low = 0;
uchar high = 0;
init_18b20();
write_18b20(0xcc); //Skip 64-bit ROM
write_18b20(0x44); //Start a temperature conversion command
delay_uint(50);
init_18b20(); //Initialize 18b20
write_18b20(0xcc); //Skip 64-bit ROM
write_18b20(0xbe); //Issue a read register command
low = read_18b20();
high = read_18b20();
wendu = high;
wendu <<= 8;
wendu |= low;
if(wendu & 2048)
{<!-- -->
Sign_Negative = 1;
wendu = ~wendu + 1;
} else {<!-- -->
Sign_Negative = 0;
}
wendu = wendu * 0.0625;
wendu = wendu*10 + 0.5;
\t
return wendu; //Return the read temperature with decimals
}

/******************Timer 0 initialization program******************/
void time_init()
{<!-- -->
EA = 1; //Enable total interrupt
TMOD = 0X01; //Timer 0, timer 1 working mode 1
ET0 = 1; //Enable timer 0 interrupt
TR0 = 1; //Allow timer 0 timing
}

/************************Independent key program******************/
uchar key_can; //Key value

void key() //Independent key program
{<!-- -->
 key_can = 0; //Restore key value
if((P2 & amp; 0x07) != 0x07) //Button pressed
{<!-- -->
 delay_1ms(10); //Button debounce
if((P2 & amp; 0x07) != 0x07)
{<!-- --> //Confirm that the button is pressed
 switch(P2 & 0x07)
{<!-- -->
case 0x06: key_can = 3; break; //Get the k3 key value
case 0x05: key_can = 2; break; //Get the k2 key value
case 0x03: key_can = 1; break; //Get the k1 key value
}
 }
while ((P2 & amp; 0x07)!= 0X07); //Release detection
}
}

/******************Button processing digital tube display function******************/
void key_with()
{<!-- -->
if(key_can == 1) //Set key
{<!-- -->
menu_1 + + ;
if(menu_1 >= 3)
{<!-- -->
menu_1 = 0;
 }
}
if(menu_1 == 1) //Set high temperature alarm
{<!-- -->
 if(key_can == 2)
{<!-- -->
 t_high + + ; //Add 1 to the upper temperature limit value
 if(t_high > 990)
t_high = 990;
}
if(key_can == 3)
{<!-- -->
 t_high -- ; //Decrease the upper temperature limit by 1
 if(t_high <= t_low)
t_high = t_low + 1;
}
dis_smg[3] = smg_du[t_high % 10]; //Take decimal display
dis_smg[2] = smg_du[t_high / 10 % 10]; //Take the ones digit to display
dis_smg[2] &= 0x7f;
dis_smg[1] = smg_du[t_high / 100 % 10]; //Take the tens digit to display
dis_smg[0] = smg_du[10]; //H
}
if(menu_1 == 2) //Set low temperature alarm
{<!-- -->
 if(key_can == 2)
{<!-- -->
 t_low + + ; //Add 1 to the lower temperature limit value
 if(t_low >= t_high)
t_low = t_high - 1;
}
if(key_can == 3)
{<!-- -->
 t_low -- ; //Subtract 1 from the lower temperature limit value
 if(t_low <= 1)
t_low = 1;
}
dis_smg[3] = smg_du[t_low % 10]; //Take decimal display
dis_smg[2] = smg_du[t_low / 10 % 10]; //Take the units digit to display
dis_smg[2] &= 0x7f;
dis_smg[1] = smg_du[t_low / 100 % 10]; //Take the tens digit to display
dis_smg[0] = smg_du[11]; //L
}
delay_1ms(150);
}

/******************Alarm function****************/
void clock_h_l()
{<!-- -->
 if((temperature <= t_low) || (temperature >= t_high))
{<!-- -->
 beep = !beep; //Buzzer alarm
 }
else
{<!-- -->
beep = 1;
}
}

sbit P27 = P2^7;//Test probe

/******************Main function****************/
void main()
{<!-- -->
beep = 0; //Beep once when powering on
delay_1ms(150);
beep = 1; // Stop after beeping, judge the temperature to decide whether to beep or not
P0 = P1 = P2 = P3 = 0xff;
time_init(); //Initialize timer
while(1)
{<!-- -->
key(); //key program
if(key_can != 0)
{<!-- -->
key_with(); //Set alarm temperature
}
if(flag_300ms == 1) //process the temperature program once every 300ms
{<!-- -->
\t\t
flag_300ms = 0;
temperature = read_temp(); //Read the temperature value first
clock_h_l(); //alarm function
if(menu_1 == 0)
{<!-- -->
 dis_smg[3] = smg_du[temperature % 10]; //Take the decimal display of the temperature
dis_smg[2] = smg_du[temperature / 10 % 10]; //Get the units display of temperature
dis_smg[2] & amp;= 0x7f;//Display decimal point
dis_smg[0] = dis_smg[1] = 0xFF;
 if (temperature >= 100 & amp; & amp; temperature < 1000)//When the temperature is >=10 & amp; & amp; <100
{<!-- -->
dis_smg[1] = smg_du[temperature / 100 % 10]; //Get the ten-digit display of the temperature
if (Sign_Negative) {<!-- -->//If it is a negative number, add a negative sign
                        dis_smg[0] = 0xBF;
}
} else {<!-- -->//When fabs (temperature) is less than 10
                    if (Sign_Negative) {<!-- -->//If it is a negative number, add a negative sign
                        dis_smg[1] = 0xBF;
}
}
}
}
}
}

/****************Timer 0 Interrupt Service Routine******************/
void time0_int() interrupt 1
{<!-- -->
static uchar value; //interrupt once every 2ms
TH0 = 0xf8;
TL0 = 0x30; //2ms
display(); //Nigital tube display function
value + + ;
if(value >= 150)
{<!-- -->
value = 0;
flag_300ms = 1;
}
}

3.4 Internship results of temperature measurement and alarm system

3.4.1 Simulation results

3.4.2 Physical results

4. Summary

Three problems were encountered in this experiment. The first problem: the digital tube cannot display numbers correctly. Finally, it was discovered that it was caused by the mismatch between the computer and the digital tube. After I modified the code table, the problem was solved. The second question: lies in the calculation of temperature conversion. The value I have always calculated is very different from the standard value of 0.0625. Finally, through the teacher’s explanation, I found that I did not convert the base system correctly, causing the value I calculated to be incorrect. Third problem: When I press the button, the temperature adjusts too quickly. Finally, I solved this problem by adding a release detection program to the button processing digital tube display function. Design shortcomings: each click can only add or subtract 0.1 value. If the set temperature difference is small, the number of presses will be relatively easy. If you want to adjust 10 degrees, you need to press it 100 times. This is really a headache. Therefore, I added a delay function to it. If the pressing time exceeds 2ms, the set temperature upper limit will increase rapidly. The same goes for minus. After thinking about it, I found that it was very reasonable. Finally, by modifying the code, I successfully completed the optimization of this button module.

Experimental reference materials (gitee)

Program and code: https://gitee.com/shiguangliushui/Temperature-measurement-alarm-system.git