// Function: Serial port sends temperature and humidity data at a baud rate of 9600
//MCU tutorial DH11 test program
#include
#include
typedef unsigned char
typedef signed char
typedef unsigned int
typedef signed int
typedef unsigned long
typedef signed long
typedef float
typedef double F64; // double precision floating point variable (64bits) Double precision floating point number (64-bit length)
#define uchar unsigned char
#define uint unsigned int
#define Data_0_time 4
//------------------------------------------------//
//----------------IO port definition area--------------------//
//------------------------------------------------//
sbit P2_0 = P2^0 ; //DH11
//------------------------------------------------//
//----------------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;
SendData(U8 *a)
{
outdata[0] = a[0];
outdata[1] = a[1];
outdata[2] = a[2];
outdata[3] = a[3];
outdata[4] = a[4];
count = 1;
SBUF=outdata[0];
}
void Delay(U16 j)
{ U8 i;
for(;j>0;j--)
{
for(i=0;i<27;i++);
}
}
void Delay_10us(void)
{
U8 i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void COM(void)
{
U8 i;
for(i=0;i<8;i++)
{
U8FLAG=2;
//---------------------
P2_1=0; //T
P2_1=1; //T
//---------------------
while((!P2_0)&&U8FLAG++);
Delay_10us();
Delay_10us();
// Delay_10us();
U8temp=0;
if(P2_0)U8temp=1;
U8FLAG=2;
while((P2_0)&&U8FLAG++);
//---------------------
P2_1=0; //T
P2_1=1; //T
//---------------------
//Timeout if the for loop is exited
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 8 bits of temperature == U8T_data_H------
//----Temperature low 8 bits == U8T_data_L------
//----Humidity high 8 bits == U8RH_data_H-----
//----Humidity lower 8 bits == U8RH_data_L-----
//----Check 8 bits == U8checkdata-----
//----Call the relevant subroutines as follows----------
//---- Delay();, Delay_10us();,COM();
//--------------------------------
void RH(void)
{
//Host pulls down 18ms
P2_0=0;
Delay(180);
P2_0=1;
//The bus is pulled high by the pull-up resistor and the host delays 20us
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
// Set the host as input to determine the slave response signal
P2_0=1;[page]
//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;
//Judge whether the slave has sent out a low-level response signal of 80us
while((!P2_0)&&U8FLAG++);
U8FLAG=2;
//Judge whether the slave sends out a high level of 80us, if so, 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
}
//------------------------------------------------
//main() Function description: AT89C51 11.0592MHz serial port send
//Send temperature and humidity data, baud rate 9600
//------------------------------------------------
void main()
{
U8 i,j;
//uchar str[6]={"RS232"};
TMOD = 0x20; //Timer T1 uses working mode 2
TH1 = 253; // Set the initial value
TL1 = 253;
TR1 = 1; // Start timing
SCON = 0x50; //Working mode 1, baud rate 9600bps, receiving is allowed
ES = 1;
EA = 1; // Enable all interrupts
TI = 0;
RI = 0;
SendData(str); //Send to the serial port
Delay(1); //Delay 100US (12M crystal oscillator)
while(1)
{
//------------------------
//Call the temperature and humidity reading subroutine
RH();
//Serial port display program
//--------------------------
str[0]=U8RH_data_H;
str[1]=U8RH_data_L;
str[2]=U8T_data_H;
str[3]=U8T_data_L;
str[4]=U8checkdata;
SendData(str); //Send to the serial port
//The module data reading cycle is not likely to be less than 2S
Delay(20000);
}//elihw
}//main
void RSINTR() interrupt 4 using 2
{
U8 InPut3;
if(TI==1) //Send interrupt
{
TI=0;
if(count!=5) //5 bits of data sent
{
SBUF= outdata[count];
count++;
}
}
if(RI==1) //Receive interrupt
{
InPut3=SBUF;
indata[count_r]=InPut3;
count_r++;
RI=0;
if (count_r==5) // Receive 4 bits of data
{
//Data reception is complete.
count_r=0;
str[0]=indata[0];
str[1]=indata[1];
str[2]=indata[2];
str[3]=indata[3];
str[4]=indata[4];
P0=0;
}
}
}
Previous article:51 single chip microcomputer 16×16 dot matrix (scrolling display)
Next article:Temperature measurement and 24l01 wireless transmission design (I)
Recommended ReadingLatest update time:2024-11-16 21:55
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Siemens Motion Control Technology and Engineering Applications (Tongxue, edited by Wu Xiaojun)
- How to read electrical control circuit diagrams (Classic best-selling books on electronics and electrical engineering) (Zheng Fengyi)
- MCU C language programming and Proteus simulation technology (Xu Aijun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Photodiode Problems
- Interesting animated tutorial "Principles of Dialogue Communication"
- What interesting skills do Chinese people have? What about engineers?
- Design principles of fuel gauge circuit based on BQ40z80
- DSP28335 usage issues
- [Project source code] Using math library in Linux application reports undefined reference to `sin', etc.
- 11. "Wanli" Raspberry Pi car - socket learning (sent from Android)
- 4. Analysis of Demo Project
- 【Me and gui-guider②】Button control
- Qorvo Point-of-Care Diagnostics Platform Achieves Key Development Milestone