I have been learning 51 MCU for more than a month, and stayed up all night to write the OLED+DHT11 program for a competition.
The microcontroller source program is as follows:
#include "REG51.h"
#include "oled.h"
#include typedef unsigned char BYTE; typedef unsigned int WORD; #define uint unsigned int #define uchar unsigned char sbit io=P1^2; //dht11data is connected to the P1^0 port of the microcontroller // sbit IN1 = P1^0; sbit IN2 = P1^1; //Input motor interface sbit K1 = P3^0; sbit K2 = P3^1; //button interface uchar data_byte; uchar RH,RL,TH,TL; int count1,a; void DelayUs2x(unsigned char t) { while(--t); } void DelayMs(unsigned char t) { while(t--) { //About 1mS delay DelayUs2x(245); DelayUs2x(245); } } void delay(uchar ms) //delay module// { uchar i; while(ms--) for(i=0;i<100;i++); } void delay1() //A for loop takes about 8 machine cycles. One machine cycle is 1us, and the crystal oscillator is 12MHz. This means that this function delays more than 8us. This delay function must be a little more precise. { uchar i; for(i=0;i<1;i++); } //Motor program void ZHUAN(){ IN2=0; } void speed(int cnt,int sd) { if(cnt IN1=1; } else { IN1=0; } } void turn(int cnt,int sd) { ZHUAN(); speed(cnt,sd); } void start()//start signal { io=1; delay1(); io=0; delay(25); // The host must pull the bus low for more than 18ms to ensure that DHT11 can detect the start signal io=1; //After sending the start signal, pull the level high and delay for 20-40us delay1(); //The following three delay functions are about 24us, which meets the requirements delay1(); delay1(); } uchar receive_byte() //Receive a byte // { uchar i,temp; for(i=0;i<8;i++) //Receive 8-bit data { while(!io); //Wait for 50us low level start signal to end delay1(); //After the start signal ends, delay 26us-28us, the following three delay functions delay1(); delay1(); temp=0; //The time is 26us-28us, indicating that the received data is '0' if(io==1) temp=1; //If it is still high level after 26us-28us, it means the received data is '1' while(io); //Wait for data signal high level (26us-28us for '0' and 70us for '1') data_byte<<=1; //The received data has the high bit first and is shifted right data_byte|=temp; } return data_byte; } void receive()//Receive data// { uchar T_H,T_L,R_H,R_L,check,num_check,i; start(); //Start signal// io=1; // Host is set as input to judge the response signal of slave DHT11 if(!io)//Judge whether the slave has a low level response signal// { while(!io); //Judge whether the slave has finished sending out the 80us low-level response signal // while(io); //Judge whether the slave has finished sending out a high level of 80us. If so, the host enters the data receiving state. R_H=receive_byte(); //High humidity R_L=receive_byte(); // Humidity low bit T_H=receive_byte(); // high temperature T_L=receive_byte(); // low temperature check=receive_byte(); //check bit io=0; //After the last bit of data is received, the slave device pulls the level down for 50us// for(i=0;i<7;i++)//About 50us delay delay1(); io=1; //The bus is pulled high by the pull-up resistor and enters the idle state num_check=R_H+R_L+T_H+T_L; if(num_check==check)//Judge whether the sum of the four data read is the same as the check bit { RH=R_H; RL=R_L; TH=T_H; TL=T_L; check=num_check; } } } int main(void) { TMOD=0x01; EA=1; TH0=(65536-1000)/256; TL0=(65536-1000)%256; ET0=1; TR0=1; a = 100; OLED_Init(); //Initialize OLED OLED_Clear(); OLED_ShowCHinese(36,0,0); OLED_ShowCHinese(54,0,1); OLED_ShowCHinese(72,0,2); OLED_ShowCHinese(0,2,3); OLED_ShowCHinese(18,2,5); OLED_ShowCHinese(0,4,4); OLED_ShowCHinese(18,4,5); OLED_ShowString(37,4,":",16); OLED_ShowString(37,2,":",16); while(1) { turn(count1,a); if(K1==0) { DelayMs(100); if(K1==0)// debounce { if(a>=0) a=a-10; } } if(K2==0) ……………………
Previous article:Detailed explanation of the single chip infrared sensor experiment
Next article:Graduation Project of Pulse Measurement Instrument Based on Single Chip Microcomputer
Recommended ReadingLatest update time:2024-11-15 15:31
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- 23 million Raspberry Pis have been sold
- Excellent materials: National undergraduate electronic design competition training course album
- LoRa Wireless Internet of Things wireless technology frequency band division
- [SAMR21 new gameplay] 18. Python development software
- OCL\OVL principle used in frequency converter
- Six questions and six answers, let’s get to know the “voltage stabilizer” again!
- Help! How to control multiple motors with a single motor driver
- FRAM-based MCU MSP430FR57xx design
- Share a project I made this year - a multifunctional smart watch (with ionizing radiation sensor module)
- Buck Converters - From Discrete Circuits to Fully Integrated Modules