DHT11 temperature and humidity sensor AVR microcontroller routine

Publisher:SereneWhisperLatest update time:2019-11-16 Source: eechinaKeywords:DHT11 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/************************************************************************
Program: ICC-AVR 
Function: Baud rate 9600 Serial port sends temperature and humidity data  
Data format: humidity temperature checksum For   
          example, the serial port sends data 59 00 24 00 53, then the humidity is 59, the temperature is 24, and the checksum is 53 
Company: DHT11
Chip: M8
Crystal oscillator: external 7.3728Mhz
******************************************************** **********************/
#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-bit integer variable /* defined for signed 16-bit integer variable */
typedef signed int S16; /* defined for signed 16-bit integer variable */
typedef unsigned long U32; /* defined for unsigned 32-bit integer variable */
typedef signed long S32; /* defined for signed 32-bit integer variable */
typedef float F32; /* single precision floating point variable (32bits) Single precision floating point number (32-bit length) */
typedef double F64; /* double precision floating point variable (64bits) Double precision floating point number (64-bit length) */

/**************************************************************************
                    I/O space definition
*************************************************************************/
// B0_IN DDRB &= ~(1 << PB0) /*Set input*/
// B0_OUT DDRB |= (1 << PB0) /*Set output*/
// B0_CLR PORTB &= ~(1 << PB0) /*Set low level*/
// B0_SET PORTB |= (1 << PB0) /*Set high level*/
// B0_R PINB & (1 <#define C4_IN DDRC &= ~(1 << PC4) /*Set input*/
#define C4_OUT DDRC |= (1 << PC4) /*Set output*/
#define C4_CLR PORTC &= ~(1 << PC4) /*Set low level*/
#define C4_SET PORTC |= (1 << PC4) /*Set high level*/
#define C4_R PINC & (1 </******************************************************************************
                    Communication I/O space definition
***********************************************************************/
#define COM_IN C4_IN  
#define COM_OUT C4_OUT
#define COM_CLR C4_CLR
#define COM_SET C4_SET
#define COM_R C4_R

/******************************************************************************
                    Global variable definition area
*******************************************************************/
      U8 i;
      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;
      U16   ReceiveHighByte;
      U16   ReceiveLowByte;
/**************************************************************************
***************************************************************************/
void port_init(void)
{
PORTB = 0x00;
DDRB  = 0xFF;
PORTC = 0x03;                    
DDRC  = 0x0C;
PORTD = 0x00;
DDRD  = 0xF0;
}
void watchdog_init(void)
{
  //WDR();        //this prevents a timout on enabling
  //WDTCR = 0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs
}

void Delay(unsigned int i)
{
while(i--);
}
void DelaymS (U16 dly)                   
{
U16 i;
for ( ; dly>0; dly--)
{
     WDR();
  for (i=0; i<1032; i++);        
}
}

       void  Delay_10us(void)
      {
        U8 i;                     
        i++;i++;i++;i++;i++;i++;i++;i++;i++;
  i++;i++;i++;i++;i++;i++;i++;i++;i++;
  i++;i++;i++;i++;i++;i++;i++;i++;i++;
  i++;i++;i++;i++;i++;i++;i++;i++;i++;
  i++;i++;i++;i++;i++;i++;i++;i++;i++;
  i++;i++;i++;i++;i++;i++;i++;i++;i++;
  i++;i++;i++;i++;i++;i++;i++;i++;i++;
  i++;i++;
       }
        void  COM(void)
      {
     
         U8 i;
       for(i=0;i<8;i++)    
     {
  
         U8FLAG=2;
     while((!(COM_R))&&U8FLAG++);
   if(U8FLAG==1)break;
   Delay_10us();
   Delay_10us();
      Delay_10us();
   Delay_10us();
     U8temp=0;
      if(COM_R)U8temp=1;
      U8FLAG=2;
     while((COM_R)&&U8FLAG++);
     //超时则跳出for循环    
      if(U8FLAG==1)break;
     //判断数据位是0还是1  
        
  // 如果高电平高过预定0高电平值则数据位为 1 
      
     U8comdata<<=1;
        U8comdata|=U8temp;        //0
      }//rof
    
}
//--------------------------------
//-----湿度读取子程序 ------------
//--------------------------------
//----以下变量均为全局变量--------
//----温度高8位== U8T_data_H------
//----温度低8位== U8T_data_L------
//----湿度高8位== U8RH_data_H-----
//----湿度低8位== U8RH_data_L-----
//----校验 8位 == U8checkdata-----
//----调用相关子程序如下----------
//---- Delay();, Delay_10us();,COM(); 
//--------------------------------
void RH(void)
{
  
   //Host pulls down 18ms 
       COM_OUT;
       COM_CLR;
    DelaymS(18);
    COM_IN;
    COM_SET;
   
  //The bus is pulled up by the pull-up resistor and the host delays 20us
    Delay_10us();
    Delay_10us();
    Delay_10us();
    Delay_10us();
  //Host is set as input to judge the response signal of the slave 
  //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 !  
    if(COM_R)U8FLAG=1;
    while(!(COM_R))   
    {
    U8FLAG=2;
  //Judge whether the slave sends a low-level response signal of 80us and whether it ends  
    //while((!P2_0)&&U8FLAG++);
    while((!(COM_R))&&U8FLAG++);
    if(U8FLAG==1)break;
    U8FLAG=2;
  //Judge whether the slave sends out a high level of 80us. If so, enter the data receiving state.
  //while((P2_0)&&U8FLAG++);
    while((COM_R)&&U8FLAG++);
    if(U8FLAG==1)break;
  //Data receiving state   
    COM();
    if(U8FLAG==1)break;
    U8T_data_H_temp=U8comdata;
    COM();
    if(U8FLAG==1)break;
    U8T_data_L_temp=U8comdata; 
    COM();
    if(U8FLAG==1)break;
    U8RH_data_H_temp=U8comdata;
    COM();
    if(U8FLAG==1)break;
    U8RH_data_L_temp=U8comdata;
    COM();
    if(U8FLAG==1)break;
    U8checkdata_temp=U8comdata;
    break;
     }//while
    //P2_0=1;
    COM_IN;
    COM_SET;
  //Data check 
  
    U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);
    if(U8temp==U8checkdata_temp)
    {
       U8RH_data_H=U8T_data_H_temp;
       U8RH_data_L=U8T_data_L_temp;
       U8T_data_H=U8RH_data_H_temp;
       U8T_data_L=U8RH_data_L_temp;
       U8checkdata=U8checkdata_temp;
    }//fi
   
   
    COM_IN;
    COM_SET;
}

/*
Serial port initialization function
System frequency: 2MHz
Stop bit: 1
Baud rate: 9600
Data length: 8
*/
void Uart_Init(void) {
UCSRA = 0x02; /*2x speed */
UCSRB = 0x18; /*Allow receiving and sending*/
UCSRC = 0x06; /*8-bit data */
UBRRH = 0x00;
//UBRRL = 47; /*9600 3.6864M */  
UBRRL = 95; /*9600 7.3728M */ 
}
/*
Send string data in
query mode
*/
void Uart_Transmit(unsigned char i) {
/* Wait for the send buffer to be empty*/
while (!(UCSRA & (1</* Put data into the buffer and send data*/
UDR = i;
}
/*
Receive string data
in query mode
*/
unsigned char Uart_Receive( void ) {
/* Wait for receiving data*/
while (!(UCSRA & (1</* Get and return data from the buffer*/
// Uart_Transmit(UDR);
return UDR;
}
void init_devices(void) 
{
unsigned char i;
     CLI();                     
     port_init();
watchdog_init();
    Uart_Init();
    MCUCR = 0x03;
    GICR = 0x00; // Turn off external interrupt 0
    TIMSK = 0x00; // Turn off timer 1 interrupt
           SEI(); //re-enable interrupts
                           

/******************************************************************************

                            Main function
       
       
       
       
**************************************************************************/

void main(void)
{
init_devices() ;
COM_IN;
COM_SET;

while(1)
{

//Read DHT11   
  RH();
  
  //Convert the hexadecimal data of DHT11 into decimal format  
  //Humidity Temperature Checksum   
  //For example, if the serial port sends data 59 00 24 00 53, the humidity is 59, the temperature is 24, and the checksum is 53 
  
  
  
  U8temp=U8RH_data_H/10;
  U8temp<<=4;
  U8temp|=U8RH_data_H%10;
  U8RH_data_H=U8temp;
  Uart_Transmit(U8RH_data_H); 
  Uart_Transmit(U8RH_data_L);
  
  U8temp=U8T_data_H/10;
  U8temp<<=4;
  U8temp|=U8T_data_H%10;
  U8T_data_H=U8temp;
  Uart_Transmit(U8T_data_H);  
  Uart_Transmit(U8T_data_L); 
  
  Uart_Transmit(U8checkdata); 
  //Delay 2S
  for(U8temp=0;U8temp<200;U8temp++)
  {
  DelaymS(10);

  }//rof
  
}

}



Keywords:DHT11 Reference address:DHT11 temperature and humidity sensor AVR microcontroller routine

Previous article:avr 12864 matrix keyboard
Next article:How to sample 8-channel AD in AVR 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号