I made an automatic watering system based on the stc89c51 microcontroller. It has both automatic and manual modes. The upper and lower limits of humidity can be set and the display is displayed using lcd1602. One regret is that there is no cursor flashing when setting the upper and lower limits of humidity. I hope you can give me some suggestions and help improve it. I have attached the source code here.
The simulation schematic diagram is as follows
:
MCU source code:
#include #define uchar unsigned char #define uint unsigned int sbit ad_cs=P1^3; //ADC0832 control bit sbit ad_clk=P1^0; sbit ad_dat=P1^1; sbit lcd_rs=P2^7; //LCD1602 control bit sbit lcd_e=P2^6; sbit key1=P1^4; //Independent key sbit key2=P3^2; sbit key3=P3^3; sbit relay=P1^6; //relay bit flag=1; //Working mode flag bit flag_motor=1; //In manual mode, pump switch flag bit flag_jia=0; bit flag_jian=0; uchar Hum_H=60; //In automatic mode, the humidity is set to the maximum value uchar Hum_L=30; //In automatic mode, the minimum humidity setting fly num=0; /********************Delay function 1ms********************* void delay_ms(uint a) { uint i; while(a--) for(i=0;i<125;i++); } *************************************/ /*******************Delay function 50us*********************/ void delay_50us(uint a) { uint i; while(a--) for(i=0;i<19;i++); } /*************************************/ /*******************ADC read*******************/ fly read_ad(fly channel) { flying i; float dat1,dat2; ad_clk=0; ad_cs=0; ad_dat=1; ad_clk=1; ad_clk=0; //Start signal ad_dat=1; ad_clk=1; ad_clk=0; //Select single channel mode ad_dat=channel; //0 corresponds to channel 0, 1 corresponds to channel 1 ad_clk=1; ad_clk=0; ad_dat=1; for(i=0;i<8;++i) { ad_clk=1; ad_clk=0; dat1=dat1<<1; if(ad_dat) dat1|=0x01; } for(i=0;i<8;i++) { dat2=dat2>>1; if(ad_dat) dat2|=0x80; ad_clk=1; ad_clk=0; } ad_cs=1; return (dat1==dat2)?dat1:0; } /*************************************/ /*******************LCD1602 initialization******************/ void write_com(uchar com) //write command { lcd_e=0; lcd_rs=0; P0=com; delay_50us(10); lcd_e=1; delay_50us(10); lcd_e=0; } void write_data(uchar dat) //write data { lcd_e=0; lcd_rs=1; P0=that; delay_50us(10); lcd_e=1; delay_50us(10); lcd_e=0; } void init_lcd1602() //LCD initialization { delay_50us(300); write_com(0x38); delay_50us(100); write_com(0x38); delay_50us(100); write_com(0x38); write_com(0x38); write_com(0x08); write_com(0x01); write_com(0x06); write_com(0x0c); } void display_shu(uchar add,uchar dat) //display numbers { flying l,m; // k=that/100; l=that%100/10; m=that%10; write_com(0x80+add); // write_data(k+0x30); write_data(l+0x30); write_data(m+0x30); } void display_string(uchar add,uchar *dat) //display string { flying i; write_com(0x80+add); while(dat!=0&&(*dat!='')&&i<16) { write_data(*dat); that++; i++; if(i==15) i=0; } } /*****************************************/ /********************Main function********************/ void main() { EA=1; //Open the general interrupt EX0=1; //Open external interrupt 0 EX1=1; //Open external interrupt 1 IT0=0; //External interrupt 0 trigger mode is low level IT1=0; //External interrupt 1 trigger mode is low level init_lcd1602(); display_string(0,"Hum: %"); //Real-time display of current humidity display_string(11,"H: %"); //Display the maximum humidity setting display_string(0x40,"Mode:"); //Display working mode display_string(0x40+11,"L: %"); //Display the minimum humidity setting while(1) { flying i; i=100-read_ad(0)*0.39; display_shu(4,i); display_shu(13,Hum_H); display_shu(0x40+13,Hum_L); if(key1==0) { delay_50us(100); if(key1==0) { while(!key1); num++; if(num>2) num=0; } switch (num) //Select the working mode and adjust the maximum and minimum values of humidity { case 0:flag=~flag; flag_jia=0; flag_jian=0; //write_com(0x80+0x40); //write_com(0x0f); break; case 1://write_com(0x80+9); //write_com(0x0f); //delay_50us(1000); //write_com(0x0c); flag_jia=1; flag_jian=0; break; case 2://write_com(0x80+0x40+9); //write_com(0x0f); //delay_50us(1000); //write_com(0x0c); flag_jian=1; flag_jia=0; break; } if(flag==0&&num==1) { flag=~flag; num=0; flag_jia=0; flag_jian=0; } } if(flag==0) //manual mode { display_string(0x40+5,"MT"); if(flag_motor) relay=1; else if(i } else //Automatic mode { display_string(0x40+5,"AT"); if(i<(Hum_L+Hum_H)/2) //The average of the maximum and minimum values is used as the judgment condition for pumping water { relay=0; } else relay=1; } } } /********************************************/ /***********************External Interruption************************/ void int0() interrupt 0 { delay_50us(20); if(key2==0) while(!key2); flag_motor=~flag_motor; if(flag&&flag_jia) { //write_com(0x80+9); //write_com(0x0f); //delay_50us(1000); //write_com(0x0c); //delay_50us(1000); Hum_H++; if(Hum_H>=99) Hum_H=99; } if(flag&&flag_jian) { //write_com(0x80+0x40+9); //write_com(0x0f); //delay_50us(1000); //write_com(0x0c); //delay_50us(1000); Hum_L++; if(Hum_L>=Hum_H) Hum_L=Hum_H-1; } } void int1() interrupt 2 { delay_50us(20); if(key3==0) while(!key3); if(flag&&flag_jian) { //write_com(0x80+0x40+9); //write_com(0x0f); //delay_50us(1000); //write_com(0x0c); //delay_50us(1000); Hum_L--; if(Hum_L<=1) Hum_L=1; } if(flag&&flag_jia) { //write_com(0x80+9); //write_com(0x0f); //delay_50us(1000); //write_com(0x0c); //delay_50us(1000); Hum_H--; if(Hum_H<=Hum_L) Hum_H=Hum_L+1; } } /********************************************/
Previous article:Design of OLED12864 display based on DHT11 thermometer and hygrometer
Next article:51 single-chip LED light gradual PWM dimming (gradually bright and dim) C language and assembly source program
Recommended ReadingLatest update time:2024-11-16 13:05
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Types of Motion Controllers
- What are the uses of SVC and PendSV exceptions?
- MSP430F5529 IO port pin interrupt study notes
- The value of an electronics engineer
- When using RTT for recording data output, data loss is particularly serious. Does anyone know how to increase the RTT rate?
- Thank you for your company in my spare time
- NXP imx8m series product data sheet (based on Mir MYD-JX8MX)
- Serial communication between Matlab and microcontroller
- CC2640 Software Architecture
- When installing the emulator in CCS, a prompt appears saying that the bh2usb.sys file is needed. Solution