51 MCU-temperature controller design detailed explanation + circuit + code

Publisher:清新时光Latest update time:2015-05-18 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
This 51 single chip microcomputer circuit needs to use:
 
AT89C51 x1 Main control microcontroller
 
AT24C02 x1 EEPROM-2K memory
 
DS180B2 x1 temperature sensor
 
10uF x1, 47uF x1 polarized capacitors
 
104[0.1uF] x3, 30pF x2 non-polar capacitors
 
12MHz x1 Quartz Crystal Oscillator
 
1K [1 kilo ohm] x2 resistor
 
7cmx9cm x1 Universal Hole Circuit Board
 
Relays [Install them on the circuit according to the number of relays needed. The main control chip is designed to control eight relays (a whole P1 port)]
 
The total price seems to be around 12 yuan. I suggest you check Taobao. Different merchants sell components at different prices.
 
Q1: What is the function of EEPROM?
 
A1: Because EEPROM [voltage erasable ROM] allows the microcontroller to read and write external storage data through IIC, it is also convenient to change the temperature value of a control circuit in the future. It is impossible for the microcontroller to change the temperature value of a control circuit every time.
 
When the value is reached, reprogram the main control chip.
 
Q2: Why does the EEPROM need to be 2K in size?
 
A2: Too much storage space is meaningless. The maximum number of data bits used is only 26 bits. In addition, the EEPROM with a larger storage space is expensive.
 
AT24C512-DIP8 [512KB data storage space] costs about 17 yuan.
 
Q3: Can the main control chip be replaced by other 51 series chips?
 
A3: Yes, you can. You only need to modify the header file in the original code file to transplant the program to another model of chip. Similarly, you can transplant the program to AT89C2051.
 
One advantage of AT89C2051 over AT89C51 is that the chip size of C2051 series is smaller than that of C51 series, which can make the circuit smaller. If the circuit needs to be smaller, SOP package should be used.
 
SMD electronic components are a good choice, but soldering too small SOP components to the circuit will give you a headache, and the circuit layout will also affect the overall size of the circuit board to a certain extent.
 
Small..
 
Analysis of how EEPROM stores data:

The following is the data storage structure in the EEPROM [Hehe, the picture is a bit bad, please forgive the technical problems..] 

First, analyze from the main control entry code:

#define EEPROM_StartAddr 0x4E

#define EEPROM_StartFlag 0xB0

 LED=1;

BitData=read_add(EEPROM_StartAddr);

if (BitData!=EEPROM_StartFlag) while(1); 

ListTotal=read_add(EEPROM_StartAddr+1);

for(i=0;i

{

CtrlList[i].AlarmTemp=read_add((EEPROM_StartAddr+2)+i*3);

CtrlList[i].CtrlPort =read_add((EEPROM_StartAddr+2)+i*3+1);

}  

LED = 0;

The code will first read the data in 0x4E from the EEPROM and compare it with the EEPROM start data bit flag to determine the correctness of the EEPROM data format.

The main control chip verifies each data bit, so this is a simple verification method. If each bit of data needs to be verified, a reliable and efficient algorithm is required.

Although it is safe, it will have a certain performance impact on the initialization of the main control chip

After failing to determine the start data bit, the main control chip will enter a stuck state [see code while(1);, an infinite loop, the processor will never jump out of this loop]

After the start data bit is successfully determined, the next step is to read the total number of items that need to control the temperature from the 0x4F bit of the EEPROM, and then perform data bit offset according to the total number of items to read the items to be controlled.

The temperature value and alarm control port of the control. Note that the two control data structures need to be separated by a NULL [i.e. 0x00] to prevent the data structure from being disrupted.

After reading the data from the EEPROM, the main control chip working indicator lights up and starts to enter the temperature control

Main code:

while(1)

{

TempData = get_temp();

BitData=(TempData%1000/100)*10; //Ten digits of temperature.

              BitData=(TempData%1000%100/10)+BitData; //Temperature in units.

for(i=0;i

         {

 CtrlElec=(CtrlList[i].AlarmTemp>=BitData)?0:1;

 CtrlListPort=CtrlList[i].CtrlPort;

 CtrlPort(CtrlListPort,CtrlElec); // Output low voltage if the conditions are met, and high voltage if the conditions are not met

 delayb(100);

         }

delayb(200);      

}

This is not like SetTimer(). The function specified by SetTimer() does not need to be added with a while loop. Just treat the above code as a thread and let the code segment running in this thread run forever.

This is it. Once the thread code is executed (that is, it jumps out of the while loop), it will close itself and release its own TLS (thread local storage)

First read the temperature value from DS18B02, then convert the temperature, and compare the converted temperature with the data structure read from EEPROM

A temperature control judgment cycle is about one second [calculated at 12MHz, it should be slightly longer than one second but not less than one second].

Below is the circuit of the temperature controller:

5V power supply circuit:

Main control chip and peripheral device circuit:

The circuit uses a relay to control the opening and closing of the external circuit. G [common terminal] B [normally closed terminal] K [normally open terminal] is to let the relay control the switch of the external circuit. The 5V voltage and P1^0 port are used to control the G terminal and B, K terminal of the relay. The principle is as follows [page]

The microcontroller controls the peripheral circuit by outputting 1 or 0 on the pin. For example, when P1^0 outputs 1, the pin will generate a 5V high level. When P1^0 outputs 0, the pin will generate a 0V low level.


 

 

Note: The relay schematic is from Baidu Encyclopedia -> http://baike.baidu.com/view/39560.htm

Relay principle: When current flows through the two ends of the control circuit, electromagnetic induction occurs between electricity and metal, causing the armature to be attracted by magnetic force and control the switch of the working circuit.
 
The relationship between the relay and the P1^0 pin is shown in the circuit diagram below:
 

 
 
When P1^0 outputs 1 [i.e. 5V], no current flows through the two ends of the control circuit because there is no voltage difference, so the relay still turns on switch BG. However, when P1^0 outputs 0, the current on the left side is
 
The voltage on the right is 5V, and the voltage on the right is 0V, so the current flows from left to right. At this time, the relay will turn the switch KG into a pass.

 
Similarly, the working indicator light of the main control chip also works on this principle. You can look at the EEPROM data reading code above and combine it with the main control chip and peripheral device circuit. I believe you will also
 
I will understand this principle.

 

 
The actual picture of the eight-way temperature controller is as follows [Only one of the eight-way is used, and the program has been run in practice. Due to the indoor temperature adjustment problem, the relay does not respond because the EEPROM is set to 30 degrees alarm

 
Temperature, and the sub-zero alarm code is not written and the alarm temperature decimal place judgment is not written]:

 

 


 

 

The physical picture and the circuit schematic are basically the same, so don't worry about it.

 

 
The code is shared with everyone, and you can improve it on this basis. You are also welcome to find bugs and modify them when using the code.

 
Download code and circuit diagram: http://www.51hei.com/f/DS18B20+EEPROM.rar

Below is the main.c file:
#include
#define uchar unsigned char 
#define uint unsigned int

#define EEPROM_StartAddr 0x4E
#define EEPROM_StartFlag 0xB0
/*
#define EEPROM_EndFlag 0xC0
#define EEPROM_EndAddr 0x4E+2+24 //8 data in total x 3 data bits
//EEPROM_EndAddr=0x4E+2+8x03 Each data occupies 3 bytes (two main data and one NULL), 0x49 is the total number of data items
*/
typedef int CTRL_TOTAL; //Total items in the list

typedef struct EEPROM_DataList //EEPROM data structure
{
  int AlarmTemp; //Alarm temperature
  int CtrlPort; //Control port
} CTRL_DATALIST;

uint get_temp(); 
void tmpchange(void); 
void delayb(uint count); 
void init();  
void write_add(uchar address,uchar date);
uchar read_add(uchar address);

void CtrlPort(int PortCode,int Ctrl);

sbit Beep=P3^4; // For testing, can be omitted
sbit LED=P2^7;

sbit CtrlPort1=P1^0;
sbit CtrlPort2=P1^1;
sbit CtrlPort3=P1^2;
sbit CtrlPort4=P1^3;
sbit CtrlPort5=P1^4;
sbit CtrlPort6=P1^5;
sbit CtrlPort7=P1^6;
sbit CtrlPort8=P1^7;

CTRL_TOTAL ListTotal;
CTRL_DATALIST CtrlList[8];
void delayb(uint count) ;
void main()

  int BitData; // EEPROM bit data cache  
  int i; // Counting loop variable
  int TempData; // Current temperature value
  int CtrlListPort; // Pin to be controlled
  int CtrlElec; // Output level of the pin to be controlled

  LED=1;
  init();
  BitData=read_add(EEPROM_StartAddr);

  if (BitData!=EEPROM_StartFlag){while(1);} // Stuck it, don't let it run again
  // For information about getting data from EEPROM, you can get instructions from the pictures in the same folder
  ListTotal=read_add(EEPROM_StartAddr+1);
  for(i=0;i   {
   CtrlList[i].AlarmTemp=read_add((EEPROM_StartAddr+2)+i*3);
   CtrlList[i].CtrlPort =read_add((EEPROM_StartAddr+2)+i*3+1);
  }  
  tmpchange();
  
  for(i=0;i<=7;i++) CtrlPort(i,1); //Output high potential
  LED=0; //Work indicator light is on

  while(1)
  {
   TempData=get_temp();
   BitData=(TempData%1000/100)*10; //Ten-digit temperature.
       BitData=(TempData%1000%100/10)+BitData;//One-digit temperature.
   for(i=0;i       {
     CtrlElec=(CtrlList[i].AlarmTemp>=BitData)?0:1;
     CtrlListPort=CtrlList[i].CtrlPort;
     CtrlPort(CtrlListPort,CtrlElec); //If the condition is met, output low voltage, otherwise output high voltage
     delayb(100);
      }
   delayb(200);                   
  }
}

void CtrlPort(int PortCode,int Ctrl)//Control P1.0-P1-7
{
     switch(PortCode)
  {
      case 1: CtrlPort1=Ctrl;
       case 2: CtrlPort2=Ctrl;
      case 3: CtrlPort3=Ctrl;
       case 4: CtrlPort4 =Ctrl;
      case 5: CtrlPort5=Ctrl;
      case 6: CtrlPort6=Ctrl;
      case 7: CtrlPort7=Ctrl;
      case 8: CtrlPort8=Ctrl;
     }
}

Reference address:51 MCU-temperature controller design detailed explanation + circuit + code

Previous article:A simple design of single chip DC adjustable voltage regulated power supply
Next article:MPU6050 uses I2C protocol to read the 51 single-chip microcomputer program of the X-axis raw data

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号