DHT11 temperature and humidity sensor driver programming based on C8051F410

Publisher:YuexiangLatest update time:2022-04-13 Source: eefocusKeywords:C8051F410 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This is also a little thing I made during the summer training. I bought some sensors online and spent a lot of money on them, so I used them to practice.


The output pin of DHT11 needs to be pulled up with a resistor, which is really troublesome. The program is also not very good. If there is a problem with the connection, it will freeze. It is recommended to refer to the next article for the driver writing of AM2305, but that thing costs more than one hundred yuan, and this one is only a few dollars.


#include                  // SFR declarations

#include

 

#define SYSCLK        24500000         // SYSCLK frequency in Hz

#define BAUDRATE      9600           // Baud rate of UART in bps

 

sbit led1 = P2^1;

sbit led2 = P2^3;

sbit dht_dat = P0^6;

 

void Oscillator_Init (void);

void Port_Init(void);

void UART0_Init (void);

void delay(unsigned int m);

void delay_ms(unsigned int m);

void read_dth(void);

 

 

void main (void)

{

   PCA0MD &= ~0x40;                    // WDTE = 0 (clear watchdog timer

                                       // enable)

 

   Oscillator_Init ();                 // Initialize system clock to

                                       // 24.5MHz

   Port_Init();

   UART0_Init();                       // Initialize UART0 for printf's

 

   EA = 1;                             // Enable global interrupts

 

   printf("Initialization completed!nn");

   delay_ms(5000);

   led1 = 1;

   led2 = 1;

   dht_dat = 1;

   while (1)

   {

    delay_ms(2000);

read_dth();

   }

}

 

void Oscillator_Init (void)

{

   OSCICN = 0x87;                      // Configure internal oscillator for

                                       // its highest frequency

   RSTSRC = 0x04;                      // Enable missing clock detector

}

 

void Port_Init (void)

{

   P1SKIP |= 0x40;                      // Skip all analog pins

 

   XBR0 = 0x01;                        // UART0 TX and RX pins enabled

   XBR1 = 0x40;                        // Enable crossbar and weak pull-ups

 

   P0MDOUT |= 0x10;                    // Enable TX0 as a push-pull output

 

}

 

void UART0_Init (void)

{

   SCON0 = 0x10;                       // SCON0: 8-bit variable bit rate

                                       //        level of STOP bit is ignored

                                       //        RX enabled

                                       //        ninth bits are zeros

                                       //        clear RI0 and TI0 bits

   if (SYSCLK/BAUDRATE/2/256 < 1) {

      TH1 = -(SYSCLK/BAUDRATE/2);

      CKCON |=  0x08;                  // T1M = 1; SCA1:0 = xx

   } else if (SYSCLK/BAUDRATE/2/256 < 4) {

      TH1 = -(SYSCLK/BAUDRATE/2/4);

      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 01

      CKCON |=  0x01;

   } else if (SYSCLK/BAUDRATE/2/256 < 12) {

      TH1 = -(SYSCLK/BAUDRATE/2/12);

      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 00

   } else if (SYSCLK/BAUDRATE/2/256 < 48) {

      TH1 = -(SYSCLK/BAUDRATE/2/48);

      CKCON &= ~0x0B;                  // T1M = 0; SCA1:0 = 10

      CKCON |=  0x02;

   } else {

      while (1);                       // Error.  Unsupported baud rate

   }

 

   TL1 = TH1;                          // Init Timer1

   TMOD &= ~0xF0;                      // TMOD: timer 1 in 8-bit autoreload

   TMOD |=  0x20;

   TR1 = 1;                            // START Timer1

   TI0 = 1;                            // Indicate TX0 ready

}

 

void delay(unsigned int m)

{

while(m--);

}

 

void delay_ms(unsigned int m)

{

while(m--)

delay(2046);

}

 

void read_dth(void)

{

unsigned char wei=0;

unsigned char humi_int=0,humi_float=0,temp_int=0,temp_float=0,chk_dat=0;

 

dht_dat=1;

printf("nnn started sending request.n");

delay_ms(100);

dht_dat=0;

delay_ms(25);

dht_dat=1;

delay(64);

while(1==dht_dat);

//printf("The lower computer has responded--current low level. n");

while(0==dht_dat);

//printf("The lower computer has responded--current high level. n");

while(1==dht_dat);

//printf("The lower computer starts to transmit data n");

 

//Start receiving data

for (wei=0;wei<8;wei++)

{

soil_int*=2;

while(0==dht_dat);

delay(62);

if(1==dht_dat)

{

soil_int++;

while(1==dht_dat);

}

} //Humidity integer acceptance completed

for (wei=0;wei<8;wei++)

{

ground_float*=2;

while(0==dht_dat);

delay(62);

if(1==dht_dat)

{

ground_float++;

while(1==dht_dat);

}

} //Humidity decimal reception completed

for (wei=0;wei<8;wei++)

{

temp_int*=2;

while(0==dht_dat);

delay(62);

if(1==dht_dat)

{

temp_int++;

while(1==dht_dat);

}

} // Temperature integer reception completed

for (wei=0;wei<8;wei++)

{

temp_float*=2;

while(0==dht_dat);

delay(62);

if(1==dht_dat)

{

temp_float++;

while(1==dht_dat);

}

} //Humidity decimal reception completed

for (wei=0;wei<8;wei++)

{

chk_dat*=2;

while(0==dht_dat);

delay(62);

if(1==dht_dat)

{

chk_dat++;

while(1==dht_dat);

}

} //Verification code received

printf("Humidity:%d.%dn",(int)humi_int,(int)humi_float);

printf("Temperature is: %d.%dn",(int)temp_int,(int)temp_float);

printf("The verification code is: %dn",(int)chk_dat);

 

 

 

delay_ms(1500);

}


I won't say much about the pins. Please note that there must be pull-ups. It won't work without them. I have tried it and AM2305 does not need pull-ups.

Keywords:C8051F410 Reference address:DHT11 temperature and humidity sensor driver programming based on C8051F410

Previous article:Design of Matrix Keyboard Based on C8051F410
Next article:Driver writing for am2305 temperature and humidity sensor based on C8051F410

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号