I. Project Introduction
The digital thermometer is an electronic measuring instrument that is widely used in daily life and industrial fields to detect ambient temperature and convert it into a digital signal for display. With the development of modern science and technology, digital thermometers have gradually replaced traditional mercury thermometers and other methods, with advantages such as fast response, high precision, portability, etc.
The digital thermometer designed based on 51 MCU is specifically applied to temperature detection in manufacturing, such as temperature controllers, oven temperature control, food processing, industrial furnaces and so on. The DS18B20 digital temperature sensor is used for temperature collection, the commonly used STC89C52 single-chip microcomputer control chip is used, and the 4-bit common anode digital tube is used to display the temperature data. When the set upper limit threshold is exceeded, the system will trigger the buzzer to alarm and prompt, thus ensuring accurate temperature control and safety.
DS18B20 is a digital temperature sensor produced by Maxim Integrated. It uses a 1-Wire bus interface, which only needs one data line to simultaneously transmit data and power supply. Its main features are high precision, fast response speed, small size, and low price, and it is widely used in various temperature measurement occasions.
The measurable temperature range of DS18B20 is -55°C to +125°C, with an accuracy of ±0.5°C (within the range of -10°C to +85°C). It integrates a temperature sensor, A/D converter and digital signal processing circuit inside, and can directly output digital temperature values.
The working principle of DS18B20 is to use the effect of temperature on the resistance value of semiconductor materials to convert temperature into resistance values, and then convert the resistance values into digital signals through the A/D converter and output them. The 1-Wire bus interface can connect multiple DS18B20 sensors in series, and only one controller is required to simultaneously read the temperature data of multiple sensors.
In the thermistor temperature measurement system, the DS18B20 sensor can be used to measure the ambient temperature, and transmit the temperature value to the controller for processing and display.
Below is the simulation diagram:
II. Design Ideas
2.1 System Architecture
The system hardware mainly consists of a single chip microcomputer control module, temperature sensor module, digital tube display module, key module, buzzer module. Among them, the STC89C52 is used as the main control chip in the single chip microcomputer control module, and functions such as temperature detection, control and alarm are realized by connecting peripheral circuits such as digital tubes, buttons, buzzers, and temperature sensors.
2.2 Technical Solutions
(1) Temperature sensor module This project uses the DS18B20 digital temperature sensor for temperature detection. This sensor has advantages such as high precision, fast response, and high reliability. Temperature data acquisition is realized by serial communication with the single chip microcomputer.
(2) Digital tube display module This project uses a 4-bit common anode digital tube to display temperature data. Dynamic scanning and display of data are implemented by setting the control IO port of the single chip microcomputer.
(3) Button module This project sets the button module to set the upper limit threshold of the temperature. Matrix buttons are used to implement multiple button functions.
(4) Buzzer module This project uses a buzzer as an alarm prompt. When the temperature exceeds the upper limit, the buzzer will emit a certain frequency alarm signal after being triggered by the single chip microcomputer control.
2.3 System Implementation Process
(1) Main program initialization: Set IO port mode, serial port configuration, timer interrupt and other parameters.
(2) Temperature detection: Collect temperature data through DS18B20 and parse the collected data into actual temperature values.
(3) Digital tube display: Display temperature values through digital tubes.
(4) Upper limit threshold setting: Set the upper limit threshold of the temperature through the button, and store the threshold in the EEPROM inside the single chip microcomputer.
(5) Alarm prompt: When the temperature value exceeds the threshold, the buzzer will trigger an alarm signal.
III. Code Implementation
3.1 4-bit Common Anode Digital Tube Display Code
The following is the code to control the STC89C52 to display the numbers 1234 on the 4-bit common anode digital tube through P1 port:
#include <reg52.h>
// Define digital tube IO ports
sbit Dig1 = P1^0;
sbit Dig2 = P1^1;
sbit Dig3 = P1^2;
sbit Dig4 = P1^3;
// Define digital tube segment codes
unsigned char code SegCode[] = {
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F
};
void main()
{
unsigned int num = 1234; // Number to display
unsigned char i, j, k, l; // Represent thousand, hundred, ten and unit digit respectively
while (1) {
// Decompose number into thousand, hundred, ten and unit digits
i = num / 1000;
j = num % 1000 / 100;
k = num % 100 / 10;
l = num % 10;
// Display thousand digit
Dig1 = 1;
P0 = SegCode[i];
Dig1 = 0;
// Display hundred digit
Dig2 = 1;
P0 = SegCode[j];
Dig2 = 0;
// Display ten digit
Dig3 = 1;
P0 = SegCode[k];
Dig3 = 0;
// Display unit digit
Dig4 = 1;
P0 = SegCode[l];
Dig4 = 0;
}
}
In this code, the IO ports connected to the digital tubes are defined, and then the segment codes of the digital tubes are defined. In the main
function, the number 1234 to be displayed is decomposed into thousands, hundreds, tens and units, and displayed in turn by controlling the four IO ports of P1. Since common anode digital tubes are used here, the corresponding IO port needs to be set to 0 to light up the digital tube.
3.2 Digital Thermometer Implementation Code
The following is the complete code for the digital thermometer.
#include <reg52.h>
// Define temperature sensor pin
sbit DQ = P3^7;
// Define digital tube pins
sbit DIG_1 = P2^0;
sbit DIG_2 = P2^1;
sbit DIG_3 = P2^2;
sbit DIG_4 = P2^3;
sbit SEG_A = P1^0;
sbit SEG_B = P1^1;
sbit SEG_C = P1^2;
sbit SEG_D = P1^3;
sbit SEG_E = P1^4;
sbit SEG_F = P1^5;
sbit SEG_G = P1^6;
sbit SEG_DP = P1^7;
// Define button pins
sbit KEY_SET = P0^0;
sbit KEY_ADD = P0^1;
sbit KEY_SUB = P0^2;
// Define global variables
unsigned char code DisplayChar[] = {
0xc0, 0xf9, 0xa4, 0xb0, 0x99,
0x92, 0x82, 0xf8, 0x80, 0x90}; // Digital tube display character encoding
unsigned char TempData[4] = {0, 0, 0, 0}; // Array to store displayed temperature value
unsigned char SetTemp = 25; // Set upper limit threshold of temperature
unsigned char LastKeyStatus = 0x07; // Button status
unsigned char Count = 0; // Digital tube scanning counter
bit IsAlarm = 0; // Alarm status
// Timer interrupt service routine
void Timer0_ISR() interrupt 1 {
TH0 = 0xfc;
TL0 = 0x67;
DIG_1 = DIG_2 = DIG_3 = DIG_4 = 1; // Turn off all digital tubes
Count++; // Digital tube scanning counter plus 1
switch (Count) {
case 1: // Scan 1st digital tube
DIG_1 = 0;
P0 = TempData[3];
break;
case 2: // Scan 2nd digital tube
DIG_2 = 0;
P0 = TempData[2];
break;
case 3: // Scan 3rd digital tube
DIG_3 = 0;
P0 = TempData[1];
break;
case 4: // Scan 4th digital tube
DIG_4 = 0;
P0 = TempData[0];
break;
default:
Count = 0;
break;
}
}
// Delay function
void Delay(unsigned int n) {
unsigned int i, j;
for(i=0; i<n; i++) {
for(j=0; j<125; j++);
}
}
// Digital thermometer initialization function
void Init() {
TMOD |= 0x01; // Timer 0 works in mode 1
TH0 = 0xfc; // Initial value of timer 0
TL0 = 0x67;
ET0 = 1; // Enable timer 0 interrupt
TR0 = 1; // Start timer 0
EA = 1; // Enable interrupt
}
// DS18B20 reset function
bit Reset() {
bit res;
DQ = 0;
Delay(480);
DQ = 1;
Delay(60);
res = DQ;
Delay(420);
return res;
}
// DS18B20 write byte function
void WriteByte(unsigned char dat) {
unsigned char i;
for(i=0; i<8; i++) {
DQ = 0;
Delay(2);
DQ = dat & 0x01;
Delay(60);
DQ = 1;
Delay(2);
dat >>= 1;
}
}
// DS18B20 read byte function
unsigned char ReadByte() {
unsigned char i, j, dat = 0;
for(i=0; i<8; i++) {
DQ = 0;
Delay(2);
DQ = 1;
Delay(2);
j = DQ;
Delay(60);
dat |= (j << i);
}
return dat;
}
// DS18B20 temperature conversion function
void TempConv() {
if(!Reset()) {
WriteByte(0xCC); // Skip ROM, directly access DS18B20
WriteByte(0x44); // Send temperature conversion command
}
}
// DS18B20 read temperature function
void ReadTemp() {
unsigned char TL, TH;
if(!Reset()) {
WriteByte(0xCC); // Skip ROM, directly access DS18B20
WriteByte(0xBE); // Send read temperature command
TL = ReadByte(); // Read lower 8 bits of temperature
TH = ReadByte(); // Read upper 8 bits of temperature
if(TH > 7) { // Temperature value is negative, convert to complement
TH = ~TH;
TL = ~TL;
TempData[0] = ((unsigned short)(TH << 8) | TL) * -0.0625 * 10 + 0.5; // Calculate and save temperature value
TempData[1] = DisplayChar[10]; // Display character "-"
} else { // Temperature value is positive
TempData[0] = ((unsigned short)(TH << 8) | TL) * 0.0625 * 10 + 0.5; // Calculate and save temperature value
TempData[1] = DisplayChar[TempData[0] / 10]; // Display integer part
}
TempData[2] = DisplayChar[TempData[0] % 10]; // Display decimal part
}
}
// Button detection function
void KeyCheck() {
unsigned char key_status = 0;
if(KEY_SET == 0) { // SET button pressed
key_status |= 0x01;
}
if(KEY_ADD == 0) { // ADD button pressed
key_status |= 0x02;
}
if(KEY_SUB == 0) { // SUB button pressed
key_status |= 0x04;
}
if(key_status != LastKeyStatus) { // Check if button event occurred
Delay(10); // Debounce delay
if(key_status != LastKeyStatus) { // Check again if button event occurred
switch(key_status) {
case 0x01: // SET button pressed
SetTemp++; // Increase temperature upper limit
if(SetTemp > 50) { // Upper limit cannot exceed 50°C
SetTemp = 50;
}
break;
case 0x02: // ADD button pressed
break;
case 0x04: // SUB button pressed
break;
default:
break;
}
}
LastKeyStatus = key_status; // Save current button status
}
}
// Alarm function
void Alarm() {
if(TempData[0] > SetTemp * 10 && !IsAlarm) { // Trigger alarm when temperature exceeds set threshold and no existing alarm
IsAlarm = 1; // Set alarm flag
while(TempData[0] > SetTemp * 10) { // Loop until temperature drops below threshold
P1 = 0xff; // Turn off digital tubes
P0 = 0x00; // Turn off buzzer
Delay(500); // Delay
P1 = 0x00; // Turn on digital tubes
P0 = 0xff; // Turn on buzzer
Delay(500); // Delay
}
} else if(TempData[0] <= SetTemp * 10) { // Cancel alarm when temperature is below or equal to set threshold
IsAlarm = 0; // Clear alarm flag
}
}
// Main function
void main() {
Init(); // Initialize digital thermometer
while(1) {
TempConv(); // Temperature conversion
ReadTemp(); // Read temperature
KeyCheck(); // Button detection
Alarm(); // Alarm handling
}
}
The design of this code is mainly divided into 4 modules:
(1) Digital tube display module: Use a four-digit common cathode digital tube to display the temperature value, and use timer interrupts to scan the four digital tubes for display.
(2) DS18B20 module: Obtain the current temperature value through the DS18B20 temperature sensor, and save the temperature value to the array for display by the digital tube display module.
(3) Button detection module: Implement functions such as setting upper limit temperature threshold, increasing and decreasing temperature by detecting button status.
(4) Alarm module: Trigger buzzer alarm when current temperature exceeds the set upper limit temperature threshold.
The code is mainly designed using 51 MCU, which includes temperature sensor reading of DS18B20, button detection, digital tube display, buzzer control and many other functions. The cooperation between the modules is achieved through the use of timer interrupts and loop structures, thereby jointly completing the design of the digital thermometer.
Comments