Program design of real-time monitoring of temperature and humidity based on microcontroller

Publisher:美好的人生Latest update time:2023-07-11 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Reference address:Program design of real-time monitoring of temperature and humidity based on microcontroller

Previous article:How to use 51 microcontroller to implement IIC communication
Next article:What is the function of the microcontroller startup file .s?

Latest Microcontroller Articles
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号