找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 953|回复: 5
打印 上一主题 下一主题
收起左侧

求助一个51单片机浇花程序

[复制链接]
跳转到指定楼层
楼主
ID:111875 发表于 2023-1-14 22:21 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位论坛的老师你们好,本人不才,单片机太菜了,近期过年回家放心不下房子里种的洋芋怕干死了,想用手头的东西做一个自动浇花装置,材料有51单片机,PCF8591,电容式土壤湿度检测传感器模块,继电器若干,水泵若干。
希望能得到论坛各位大侠的帮助,本人不胜感激
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:213173 发表于 2023-1-15 21:37 | 只看该作者
这是坛友写的仿真及程序,可以参考。 自动浇水器.rar (70.24 KB, 下载次数: 39)

回复

使用道具 举报

板凳
ID:944207 发表于 2023-1-15 23:20 | 只看该作者
我有一个用的是51和ADC0832,代码如下,希望有帮助,代码如下#include <reg52.h>  #include "intrins.h"                                                                                            #define     u8                          unsigned char #define     u16                   unsigned int #define     uchar                          unsigned char #define     uint                   unsigned int uchar yushe_Moisture=20;                         uchar yushe_MoistureH=40;                         uchar Moisture;                                                          //运行模式   uchar Mode=0;                                                          uchar Mode_1=0;                                                  //管脚声明 sbit LED_Moisture= P1^6;                  void delay_ms(uint q) {         uint i,j;         for(i=0;i<q;i++)                 for(j=0;j<110;j++); } /********************************************************************** LCD1602相关函数 **********************************************************************/ //LCD管脚声明  sbit LCDRS = P2^7; sbit LCDEN = P2^6; //LCD延时 void LCDdelay(uint z)// {   uint x,y;   for(x=z;x>0;x--)     for(y=10;y>0;y--); } //写命令 void write_com(uchar com) {   LCDRS=0;                                     P0=com;   LCDdelay(5);   LCDEN=1;   LCDdelay(5);   LCDEN=0; } //写数据 void write_data(uchar date) {   LCDRS=1;         LCD_WriteData(date);   P0=date;   LCDdelay(5);   LCDEN=1;   LCDdelay(5);   LCDEN=0; } //选择写入位置 void SelectPosition(unsigned char x,unsigned char y)  {              if (x == 0)          {                      write_com(0x80 + y);              }         else          {                       write_com(0xC0 + y);               }         } //写入字符串函数 void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)  {              SelectPosition(x,y) ;         while (*s)          {                      write_data( *s);                      s ++;              } } void LCD_Write_Char(u8 x,u8 y,u16 s,u8 l)  {              SelectPosition(x,y) ; if(l>=5)                 write_data(0x30+s/10000%10);                 if(l>=4)                 write_data(0x30+s/1000%10);                         if(l>=3)                 write_data(0x30+s/100%10);                         if(l>=2)                 write_data(0x30+s/10%10);                                 if(l>=1)                 write_data(0x30+s%10);                 }   write_com(0x38);    write_com(0x0c);      write_com(0x0d);       write_com(0x0d);     //1602初始化 void Init1602() {   uchar i=0;   write_com(0x38);       write_com(0x0c);       write_com(0x06);       write_com(0x01);            } void Display_1602(yushe_Moisture,yushe_MoistureH,temp) {         //显示预设湿度         LCD_Write_Char(0,12,yushe_MoistureH,3) ;         LCD_Write_Char(1,12,yushe_Moisture,3) ;             LCD_Write_Char(0,3,temp,3) ; } /**********************************************************************ADC0832相关函数 **********************************************************************/ sbit ADCS         =P1^3;       sbit ADCLK  =P1^0;     sbit ADDI         =P1^1;                       sbit ADDO         =P1^1;              unsigned int Adc0832(unsigned char channel) {         uchar i=0;         uchar j;         uint dat=0;         uchar ndat=0;         uchar  Vot=0; if(channel==0)channel=2;         if(channel==1)channel=3;         ADDI=1;         _nop_();         _nop_();         ADCS=0;              _nop_();         _nop_();         ADCLK=1;            _nop_();         _nop_();         ADCLK=0;             _nop_();         _nop_();         ADCLK=1;           ADDI=channel&0x1;         _nop_();         _nop_();         ADCLK=0;             _nop_();         _nop_();         ADCLK=1;              ADDI=(channel>>1)&0x1;         _nop_();         _nop_();         ADCLK=0;              ADDI=1;                _nop_();         _nop_();         dat=0;         for(i=0;i<8;i++)         {                 dat|=ADDO;                   ADCLK=1;                 _nop_();                 _nop_();                 ADCLK=0;                    _nop_();                 _nop_();                 dat<<=1;                 if(i==7)dat|=ADDO;         }         for(i=0;i<8;i++)         {                 j=0;                 j=j|ADDO;                    ADCLK=1;                 _nop_();                 _nop_();                 ADCLK=0;                    _nop_();                 _nop_();                 j=j<<7;                 ndat=ndat|j;                 if(i<7)ndat>>=1;         }         ADCS=1;             ADCLK=0;           ADDO=1;             dat<<=8;         dat|=ndat;          return(dat);            //return ad data } /**********************************************************************按键检测相关函数 **********************************************************************/ sbit Key1=P1^4;                                  sbit Key2=P3^2;                                  sbit Key3=P3^3;                                  sbit Key4=P3^4;                                   #define KEY_SET                 1                 #define KEY_ADD                        2                 #define KEY_MINUS                3                  u8 Key_Scan() {                  static u8 key_up=1;   //按键按松开标志         if(key_up&&(Key1==0||Key2==0||Key3==0))         {                 delay_ms(10);     //去抖动                  key_up=0;                 if(Key1==0)                        return 1;                 else if(Key2==0)return 2;                 else if(Key3==0)return 3;         }         else if(Key1==1&&Key2==1&&Key3==1)                 key_up=1;                       return 0; } void main (void) {         u8 key;         Moisture=Adc0832(0);                                         Moisture=100-(Moisture*100/256);                                         Init1602();                                   LCD_Write_String(0,0,"Ms:000% SEH:000%");           LCD_Write_String(1,0,"MODE:A  SEL:000%");           delay_ms(1000);         Moisture=Adc0832(0);                                         Moisture=100-(Moisture*100/256);                                         while (1)                                                         {                 key=Key_Scan();                                                         Moisture=Adc0832(0);                                                 Moisture=100-(Moisture*100/256);                         if(key==KEY_SET)                 {                         Mode++;                 }                 switch(Mode)                                                                 {                         case 0:                                                                                         {                                 if(key==KEY_ADD)                                 {                                         Mode_1=!Mode_1;                                         if(Mode_1)                                                 LCD_Write_String(1,5,"H");                                           else                                                 LCD_Write_String(1,5,"A");                                   }                                 if(Mode_1)                                 {                                         if(key==KEY_MINUS)                                         {                                                 LED_Moisture=!LED_Moisture;                                         }                                 }                         Display_1602(yushe_Moisture,yushe_MoistureH,Moisture);                                   if(!Mode_1)                                 {                                         if(Moisture>=yushe_MoistureH)                                         {                                                                              LED_Moisture=1;                                                                                   }                                                                      if(Moisture<=yushe_Moisture)                                         {                                                                              LED_Moisture=0;                                                                                   }                                 }                                 break;                         }                         case 1:                                                         {                                 SelectPosition(0,11) ;                                                            write_com(0x0d);                                                                         if(key==KEY_ADD)                                                                 {                                         if(yushe_MoistureH>=99)                                               yushe_MoistureH=99;                                                  yushe_MoistureH++;                                                         LCD_Write_Char(0,12,yushe_MoistureH,3) ;                                 }                                 if(key==KEY_MINUS)                                                                                 {                                         if(yushe_MoistureH<=1)                                                                         yushe_MoistureH=1;                                                           yushe_MoistureH--;                                                                                   LCD_Write_Char(0,12,yushe_MoistureH,3) ;                                 }                                 break;                         }                         case 2:                                                         {                                 SelectPosition(1,11) ;                                                                    write_com(0x0d);                                                                         if(key==KEY_ADD)                                                                                         {                                         if(yushe_Moisture>=99)                                                 yushe_Moisture=99;                                                     yushe_Moisture++;                                                          LCD_Write_Char(1,12,yushe_Moisture,3) ;                                 }                                 if(key==KEY_MINUS)                                                                                 {                                         if(yushe_Moisture<=1)                                                                         yushe_Moisture=1;                                                           yushe_Moisture--;                                                                                   LCD_Write_Char(1,12,yushe_Moisture,3) ;                                 }                                 break;                         }                         default        :                                 {                                 write_com(0x38);                                 write_com(0x0c);                                 Mode=0;                                                         break;                         }                 }         } }
回复

使用道具 举报

地板
ID:45879 发表于 2023-1-16 09:30 | 只看该作者
洋芋不是马铃薯嘛  挺耐旱的。
回复

使用道具 举报

5#
ID:111875 发表于 2023-1-16 14:17 | 只看该作者
wulin 发表于 2023-1-15 21:37
这是坛友写的仿真及程序,可以参考。

感谢分享,实在不行的话就用里面的adc
回复

使用道具 举报

6#
ID:111875 发表于 2023-11-19 14:06 | 只看该作者
wulin 发表于 2023-1-15 21:37
这是坛友写的仿真及程序,可以参考。

感谢感谢
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表