DH11 C language test program based on 89C52

Publisher:dadigtLatest update time:2015-08-26 Source: eefocusKeywords:89C52  DH11 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//MCU: AT89S52 or STC89C52RC

// Function: Serial port sends temperature and humidity data at a baud rate of 9600

//MCU tutorial DH11 test program

#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 */

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 */

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 */

typedef float  F32; // single precision floating point variable (32 bits) single precision floating point number (32 bits in length)

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;

}

}

}

Keywords:89C52  DH11 Reference address:DH11 C language test program based on 89C52

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

Application of voice circuit reading circuit based on single chip microcomputer 89C52 in fire control system
In most modern control systems, light-emitting diodes (LEDs), digital tubes, liquid crystal displays, buzzers, etc. are usually used for status/result display and fault alarm. If digital language technology is used on the display alarm instrument to transmit information suitable for auditory transmission by
[Analog Electronics]
Two Examples of Serial Communication Programming for 89C52 Single Chip Microcomputer
1. When the 89C52 serial port performs serial data communication in working mode 1, assuming the baud rate is 1200b/s and data is transmitted in interrupt mode, please write a full-duplex communication program. #include at89x52.h unsigned char data txbuf; unsigned char data rebuf;   void main() { unsigne
[Microcontroller]
Temperature Control System Based on PID Algorithm and 89C52 Single Chip Microcomputer
0 Introduction Temperature control technology plays a very important role in both industrial production and daily life. In industries such as metallurgy, petroleum, chemical industry, electric power and modern agriculture, temperature is one of the most important and common thermal parameters. In ordinary h
[Microcontroller]
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号