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 #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.
Previous article:Design of Matrix Keyboard Based on C8051F410
Next article:Driver writing for am2305 temperature and humidity sensor based on C8051F410
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Does anyone have a 2440 driver for DM9000 and a TCP/IP protocol stack port?
- Academician criticizes the challenges faced by commercialization of electric vehicles with a range of 1,000 kilometers
- Theme Month - Advantages and Disadvantages of AC and DC Transmission Lines
- TB5128 circuit file
- "Power amplifier experimental case" Application of power amplifier in magnetoacoustic imaging method of ultrasonic detection
- The use and introduction of Crazy Shell AI open source drone ground station host computer
- Please recommend a single-supply precision dual op amp, millivolt level, preferably in SOP-8 package
- swd download program error
- Solution sharing: Type-C fast charging solution for fascia gun
- 3. Signal Filter