wiring:
Code:
main.c file
/*
Use the DHT11 module to detect temperature and humidity and display them on the LCD1602. Because the 1602 display does not always work, serial port printing is added to make it more intuitive.
*/
#include #include "lcd.h" #include #include sbit Temp_data=P3^6; //Signal pin unsigned int rec_dat[4]; unsigned char rec_dat_lcd0[6]; unsigned char rec_dat_lcd1[6]; unsigned char rec_dat_lcd2[6]; unsigned char rec_dat_lcd3[6]; void DHT11_delay_us(unsigned char n); void DHT11_delay_ms(unsigned int z); void DHT11_start(); unsigned char DHT11_rec_byte(); void DHT11_receive(); void InitUART(void); //Main function void main() { InitUART(); //Serial port interrupt initialization P1=0xf0; InitLcd1602(); LcdShowStr(0,0,"Shidu:"); LcdShowStr(0,1,"Wendu:"); while(1) { DHT11_delay_ms(150); DHT11_receive(); //This function is a print character function, which should play the role of numerical conversion. sprintf(rec_dat_lcd0,"%d",rec_dat[0]); sprintf(rec_dat_lcd1,"%d",rec_dat[1]); sprintf(rec_dat_lcd2,"%d",rec_dat[2]); sprintf(rec_dat_lcd3,"%d",rec_dat[3]); DHT11_delay_ms(100); //humidity LcdShowStr(7,0,rec_dat_lcd0); LcdShowStr(9,0,"."); LcdShowStr(10,0,rec_dat_lcd1); LcdShowStr(11,0," %"); //temperature LcdShowStr(7,1,rec_dat_lcd2); LcdShowStr(9,1,"."); LcdShowStr(10,1,rec_dat_lcd3); LcdShowStr(11,1,"C"); //The following prints the temperature through the serial port assistant //I also wonder why it can print without using the interrupt service function. Why do we need this interrupt? Can't we print directly? This is my question. printf("Humi:%d.%d n",rec_dat[0],rec_dat[1]); printf("Temp:%d.%d °Cn",rec_dat[2],rec_dat[3]); } } //DHT11 start signal void DHT11_start() { Temp_data=1; DHT11_delay_us(2); Temp_data=0; DHT11_delay_ms(20); Temp_data=1; DHT11_delay_us(13); } //Receive a byte unsigned char DHT11_rec_byte() { unsigned char i,dat; for(i=0;i<8;i++) { while(!Temp_data); DHT11_delay_us(8); dat<<=1; if(Temp_data==1) { dat+=1; } while(Temp_data); } return dat; } //Receive temperature and humidity data void DHT11_receive() { unsigned int R_H,R_L,T_H,T_L; unsigned char RH,RL,TH,TL,revise; DHT11_start(); Temp_data=1; if(Temp_data==0) { while(Temp_data==0); //Wait for high DHT11_delay_us(40); //Delay 80us after pulling high R_H=DHT11_rec_byte(); //Receive high eight bits of humidity R_L=DHT11_rec_byte(); //Receive the lower eight bits of humidity T_H=DHT11_rec_byte(); //Receive high eight bits of temperature T_L=DHT11_rec_byte(); //Receive the lower eight bits of temperature revise=DHT11_rec_byte(); //Receive correction bit DHT11_delay_us(25); //End if((R_H+R_L+T_H+T_L)==revise) //correction { RH=R_H; RL=R_L; TH=T_H; TL=T_L; } /*Data processing, convenient display*/ rec_dat[0]=RH; rec_dat[1]=RL; rec_dat[2]=TH; rec_dat[3]=TL; } } //Delay us --2*n+5us void DHT11_delay_us(unsigned char n) { while(--n); } //Delay in ms void DHT11_delay_ms(unsigned int z) { unsigned int i,j; for(i=z;i>0;i--) for(j=110;j>0;j--); } //Use timer 1 as the serial port baud rate generator void InitUART(void) { TMOD=0x20; //Timer 1 working mode 2 SCON=0x40; //Serial communication working mode 1 REN=1; //Allow receiving TH1=0xFD,TL1=0xFD; //Baud rate=9600 TI=1; // Pay attention here TR1=1; EA = 1; // Enable general interrupt } Lcd.c file #include "lcd.h" void Lcd1602_Write_Cmd(unsigned char cmd) //write command { //Read_Busy(); LCD1602_RS = 0; LCD1602_RW = 0; LCD1602_DB = cmd; LCD_Delay10ms(1); LCD1602_EN = 1; LCD_Delay10ms(1); LCD1602_EN = 0; } void Lcd1602_Write_Data(unsigned char dat) //write data { //Read_Busy(); LCD1602_RS = 1; LCD1602_RW = 0; LCD1602_DB = dat; LCD_Delay10ms(1); LCD1602_EN = 1; LCD_Delay10ms(1); LCD1602_EN = 0; } //Start displaying data at the specified position! void LcdSetCursor(unsigned char x,unsigned char y) //Coordinate display { unsigned char addr; if(y == 0) addr = 0x00 + x; //Starting from the first line, x represents the xth number in a line else addr = 0x40 + x; //Starting from the second line, x represents the xth number in a line Lcd1602_Write_Cmd(addr|0x80); } void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str) //display string { LcdSetCursor(x,y); //The coordinates of the current character while(*str != '') { Lcd1602_Write_Data(*str++); } } void InitLcd1602() //1602 initialization { Lcd1602_Write_Cmd(0x38); //Open, 5*8, 8-bit data Lcd1602_Write_Cmd(0x0c); Lcd1602_Write_Cmd(0x06); Lcd1602_Write_Cmd(0x01); //Clear screen } void LCD_Delay10ms(unsigned int c) //Error 0us { unsigned char a,b; for(;c>0;c--) for(b=38;b>0;b--) for(a=130;a>0;a--); } Lcd.h file #ifndef __LCD_H_ #define __LCD_H_ /********************************** Defined when 4-bit data transmission is used, Use 8 bits to unset this definition **********************************/ //#define LCD1602_4PINS /********************************** Include header files **********************************/ #include //---Redefine keywords---// #ifndef uchar #define uchar unsigned char #endif #ifndef uint #define uint unsigned int #endif /********************************** PIN port definition **********************************/ #define LCD1602_DB P0 //data bus data bus sbit LCD1602_RS = P2^6; sbit LCD1602_RW = P2^5; sbit LCD1602_EN = P2^7; /********************************** Function declaration **********************************/ /*Delay function under 12MHZ clock of 51 MCU*/ //void Lcd1602_Delay1ms(uint c); //error 0usvo void LCD_Delay10ms(unsigned int c); //void Read_Busy(); //Busy detection function, if bit7 is 0, execution is allowed; if bit7 is 1, execution is prohibited void Lcd1602_Write_Cmd(unsigned char cmd); //write command void Lcd1602_Write_Data(unsigned char dat); //write data void LcdSetCursor(unsigned char x,unsigned char y); //Coordinate display void LcdShowStr(unsigned char x,unsigned char y,unsigned char *str); //Display string void InitLcd1602(); //1602 initialization #endif operation result:
Previous article:51 MCU Basics DS1302
Next article:51 MCU Basics - Serial Communication
- 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
- 【GD32L233C-START Review】5. USB custom HID device lighting
- pybCN's UF2 bootloader
- DIY Wireless Charging Cool Light
- USB Type-C and USB Power Delivery Power Path Design Considerations
- xds510 simulator transfer
- Use of TI DSP Integrated Development Environment CCS
- TVS Tube
- C6000 timer application (I)
- How to handle different tasks at the same time in the time slice polling method
- Dingxin launches my country's first PHS/PAS (Phone-to-phone) mobile phone RF chip MCU