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
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;
}
}
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Solution to the problem that the zigbee terminal cannot reconnect
- Lei Jun lost the 1 billion bet with Dong Mingzhu. It seems that the typhoon still can't blow up the pigs.
- The pad is 0.3mm/center distance is 0.5mm and the wire cannot come out
- [Repost] This article tells you the difference between Ethernet and broadband
- The CC3200 LaunchPad crashed before I even got it warm?
- TMS320F28377S Study Notes 3 Building a fully portable CCS9.3 project
- Recruiting part-time lecturers in motor control related majors
- It is recommended to merge the ATMEL and PIC microcontroller sections
- TI TMS320C6678 Evaluation Module
- IoT wireless communication data transmission module: multi-host gateway working mode, understand it in one article