51 microcontroller STC89C52RC is the chip of choice for this solution. With its 8-bit processing and extremely low price, it is very suitable for this experiment. The model chosen for the LCD display this time is 1602A.
After realizing the basic functions of 51 microcontroller to realize real-time monitoring of temperature and humidity sensors and LCD display, you can also connect the device to the Internet of Things through the serial communication of the microcontroller and the WIFI module or Zigbee module, so that the device can connect to the Internet, and then connect to the Internet of Things through TCP connection. The entire solution includes real-time monitoring of data via a mobile APP, or the mobile APP can remotely turn on the fan to adjust temperature or humidity, but these are not detailed in this solution. I have already implemented the above functions, but the technology is not perfect, so I will not announce it here.
51 microcontroller internal program:
C
#include#include#include#include#include#include
typedef unsigned char U8; /* defined for unsigned 8-bits integer variable unsigned 8-bit integer variable */
typedef signed char S8; /* defined for signed 8-bits integer variable signed 8-bit integer variable */
typedef unsigned int U16; /* defined for unsigned 16-bits integer variable unsigned 16-bit integer variable*/
typedef signed int S16; /* defined for signed 16-bits integer variable signed 16-bit integer variable */
typedef unsigned long U32; /* defined for unsigned 32-bits integer variable unsigned 32-bit integer variable*/
typedef signed long S32; /* defined for signed 32-bits integer variable signed 32-bit integer variable */
typedef float F32; /* single precision floating point variable (32bits) single precision floating point number (32 bits length) */
typedef double F64; /* double precision floating point variable (64bits) double precision floating point variable (64 bits length) */
//
#define uchar unsigned char
#define uint unsigned int
#define Data_0_time 4
//------------------------------------------------//
//----------------IO port definition area--------------------//
//------------------------------------------------//
sbit P2_0 = P2^0;
//------------------------------------------------//
//----------------Definition area--------------------//
//------------------------------------------------//
U8 U8FLAG,k;
U8 U8count,U8temp;
U8 U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
U8 U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
U8 U8comdata;
U8 outdata[5]; //Define the number of bytes sent
U8 indata[5];
U8 count, count_r=0;
U8 str[5]={"RS232"};
U16 U16temp1,U16temp2;
U8 a[]={"Temperature: "};
U8 b[]={"Humidity: "};
U8 wd_sw,wd_gw,sd_sw,sd_gw;
void Delay(U16 j)
{
U8i;
for(;j>0;j--)
{
for(i=0;i<27;i++);
}
}
void Delay_10us(void)
{
U8i;
i--;
i--;
i--;
i--;
i--;
i--;
}
voidCOM(void)
{
U8i;
for(i=0;i<8;i++)
{
U8FLAG=2;
while((!P2_0)&&U8FLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
U8FLAG=2;
while((P2_0)&&U8FLAG++);
//Exit the for loop when timeout occurs
if(U8FLAG==1)break;
//Determine whether the data bit is 0 or 1
// If the high level is higher than the predetermined 0 high level value, the data bit is 1
U8comdata<<=1;
U8comdata|=U8temp; //0
}//rof
}
//--------------------------------
//-----Humidity reading subroutine------------
//--------------------------------
//----The following variables are all global variables--------
//----High temperature 8 bits== U8T_data_H------
//----Temperature low 8 bits== U8T_data_L------
//----Humidity high 8 bits== U8RH_data_H-----
//----Humidity low 8 bits== U8RH_data_L-----
//----Check 8 digits == U8checkdata-----
//----Call the relevant subroutines as follows----------
//---- Delay();, Delay_10us();,COM();
//--------------------------------
voidRH(void)
{
//The host pulls down for 18ms
P2_0=0;
Delay(180);
P2_0=1;
//The bus is pulled up by the pull-up resistor and the host delays for 20us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
//The host is set as input to determine the slave response signal
P2_0=1;
//Judge whether the slave has a low-level response signal. If it does not respond, it will jump out. If it responds, it will run downward.
if(!P2_0) //T !
{
U8FLAG=2;
//Determine whether the slave sends an 80us low-level response signal and whether it ends
while((!P2_0)&&U8FLAG++);
U8FLAG=2;
//Determine whether the slave sends a high level of 80us. If so, it will enter the data receiving state.
while((P2_0)&&U8FLAG++);
//data receiving status
COM();
U8RH_data_H_temp=U8comdata;
COM();
U8RH_data_L_temp=U8comdata;
COM();
U8T_data_H_temp=U8comdata;
COM();
U8T_data_L_temp=U8comdata;
COM();
U8checkdata_temp=U8comdata;
P2_0=1;
//Data validation
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
if(U8temp==U8checkdata_temp)
{
U8RH_data_H=U8RH_data_H_temp;
U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}//fi
}//fi
}
void main()
{
U8i,j;
LcdInit();
Delay(1); //Delay 100US (12M crystal oscillator)
while(1)
{
RH();//Call the temperature and humidity reading subroutine
str[0]=U8T_data_H;
str[1]=U8T_data_L;
str[2]=U8RH_data_H;
str[3]=U8RH_data_L;
str[4]=U8checkdata;
wd_sw=U8T_data_H/10%10+0x30;
wd_gw=U8T_data_H%10+0x30;
sd_sw=U8RH_data_H/10%10+0x30;
sd_gw=U8RH_data_H%10+0x30;
a[12]=wd_sw;
a[13]=wd_gw;
a[14]=0xdf;
a[15]='C';
a[16]='';
LcdWriteCom(0x00+0x80);
for(i=0;i<16;i++)
{
LcdWriteData(a[i]);
}
b[9]=sd_sw;
b[10]=sd_gw;
b[11]='%';
b[12]='';
LcdWriteCom(0x42+0x80);
for(i=0;i<12;i++)
{
LcdWriteData(b[i]);
}
//The reading module data cycle is not easy to be less than 2S
Previous article:How to use 51 microcontroller to implement IIC communication
Next article:What is the function of the microcontroller startup file .s?
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Recruiting embedded software development engineers
- Teacher's Day Auction [Third Round]: Freescale FRDM-KL02Z
- Smart LED Octahedron
- Tektronix's new MSO6 mixed signal oscilloscope, ultra-low noise, ultra-high resolution - a new benchmark for mid-range oscilloscopes
- [Lazy self-care fish tank control system] BLE_MESH fish tank light peripheral production
- Verilog001
- Pyboard Hardware
- Inter-core Communication Method of TMS320C6678 Multi-core DSP
- Direct imaging digital exposure technology "prints" the innovative future
- Share the summary of reasons for failure of chip inductors