Single chip computer automatic watering system

Publisher:大头玩家Latest update time:2019-10-17 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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 relay=1;

                }                                

                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;

          }

}

/********************************************/


Keywords:MCU Reference address:Single chip computer automatic watering system

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

Brief description of 8051 microcontroller architecture
The basic knowledge and pin diagram description of the 8051 microcontroller have been introduced before. In this article, we will focus on the hardware aspects of the 8051 microcontroller, such as I/O ports, RAM, ROM, timers and Serial port etc. Basic components of a microcontroller The difference between microproce
[Microcontroller]
Brief description of 8051 microcontroller architecture
Introduction to the intelligent development platform solution based on single chip microcomputer
1. The significance of MCU application development platform The single-chip microcomputer is a branch of the original large-scale computer, mainly used in: industrial automation control, intelligent instruments, meters, navigation, aerospace, smart home appliances and other fields. As a mainstream control
[Microcontroller]
Introduction to the intelligent development platform solution based on single chip microcomputer
Micro SD card reading and writing with ATmega128L microcontroller
Introduction With the enhancement of the functions of industrial control systems, the system has higher and higher requirements for the security, capacity and performance of storage media. Micro SD card is a multifunctional memory card developed on the basis of SD card and Mini SD card. It has serial and random acce
[Microcontroller]
Micro SD card reading and writing with ATmega128L microcontroller
Design of temperature measurement system based on single chip microcomputer and PC serial port communication
    Temperature is one of the main controlled parameters in industrial control. Temperature detection and control are often used in daily life and industrial and agricultural production. Therefore, it is of great significance to study the temperature measurement method and device. Traditional methods mostly use ther
[Microcontroller]
Design of temperature measurement system based on single chip microcomputer and PC serial port communication
Design of fan simulation control system based on single chip microcomputer
introduction The principle of single-chip microcomputer is widely used. According to the requirements of the single-chip microcomputer principle and application course, two aspects of design are mainly carried out, namely the single-chip microcomputer minimum system and memory expansion design, and interface technolog
[Microcontroller]
Design of fan simulation control system based on single chip microcomputer
Single chip microcomputer realizes multi-gas detection system
  A gas sensor is a device that can convert information about the type of gas and its concentration into electrical signals. Based on the strength of these electrical signals, information about the presence of the gas to be tested in the environment can be obtained, so that detection, monitoring and alarm can be perfo
[Microcontroller]
Single chip microcomputer realizes multi-gas detection system
Domestic and foreign MCU manufacturers
The single-chip microcomputer was born in 1971, and has gone through three stages: SCM, MCU, and SoC. The single-chip microcomputer has developed from 1-bit, 4-bit, 8-bit, and 16-bit to the current 32-bit and even 64-bit. With the rapid development of consumer electronic products after the 1990s, the technology of sin
[Microcontroller]
Domestic and foreign MCU manufacturers
Design of measuring light curtain for object classification equipment based on C51 series single chip microcomputer
1. Introduction Light curtain is a kind of equipment that is widely used in electronic measurement system. It can measure a series of data such as height, length or width of objects on constant speed conveyor belt, so as to provide corresponding parameters for the subsequent electronic system. This paper
[Microcontroller]
Design of measuring light curtain for object classification equipment based on C51 series single chip microcomputer
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号