找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3175|回复: 1
收起左侧

51单片机土壤湿度监控程序 DCS

[复制链接]
ID:222477 发表于 2019-4-15 23:40 | 显示全部楼层 |阅读模式
以51单片机为内核,土壤湿度传感器检测湿度,电机驱动模块驱动水泵,1602显示,矩阵键盘控制电机转速

Cache_79c699d2cc891340..jpg Cache_1477b8187f123a2c..jpg 原理图.jpg

单片机源程序如下:
  1. #include<reg52.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. unsigned char i=100;
  5. unsigned char j,k,temp,key;

  6. sbit lcden=P3^4;
  7. sbit rs=P3^2;
  8. sbit rw=P3^3;        
  9. sbit D1=P3^5;
  10. sbit TR=P3^6;
  11. uchar table1[]=" soil humidity  ";
  12. uchar table2[]="    ok";
  13. uchar table3[]="Not ok";
  14. void delay(uint x)
  15. {
  16.         uint a,b;
  17.         for(a=x;a>0;a--)
  18.                 for(b=10;b>0;b--);
  19. }

  20. void write_com(uchar com)
  21. {
  22.         P1=com;
  23.         rs=0;
  24.         lcden=0;
  25.         delay(10);
  26.         lcden=1;
  27.         delay(10);
  28.         lcden=0;
  29.         
  30. }

  31. void write_date(uchar date)
  32. {
  33.         P1=date;
  34.         rs=1;
  35.         lcden=0;
  36.         delay(10);
  37.         lcden=1;
  38.         delay(10);
  39.         lcden=0;
  40.         
  41. }

  42. void init()
  43. {
  44.     rw=0;
  45.         write_com(0x38);   //显示模式设置:16×2显示,5×7点阵,8位数据接口
  46.         delay(20);
  47.         write_com(0x0f);   //显示模式设置
  48.         delay(20);
  49.         write_com(0x06);   //显示模式设置:光标右移,字符不移
  50.         delay(20);
  51.         write_com(0x01);   //清屏幕指令,将以前的显示内容清除
  52.         delay(20);        
  53. }

  54. unsigned char key_scan()
  55. {
  56.         P2=0xfe;
  57.     temp=P2;
  58.     temp=temp&0xf0;
  59.     if(temp!=0xf0)
  60.     {
  61.       delay(5);
  62.       if(temp!=0xf0)
  63.       {
  64.         temp=P2;
  65.         switch(temp)
  66.         {
  67.           case 0xee:
  68.                key=0;
  69.                break;

  70.           case 0xde:
  71.                key=15;
  72.                break;

  73.           case 0xbe:
  74.                key=14;
  75.                break;

  76.           case 0x7e:
  77.                key=13;
  78.                break;
  79.          }
  80.          while(temp!=0xf0)
  81.          {
  82.            temp=P2;
  83.            temp=temp&0xf0;
  84.          }
  85.                  return key;
  86.       }
  87.     }
  88.     P2=0xfd;
  89.     temp=P2;
  90.     temp=temp&0xf0;
  91.     if(temp!=0xf0)
  92.     {
  93.       delay(5);
  94.       if(temp!=0xf0)
  95.       {
  96.         temp=P2;
  97.         switch(temp)
  98.         {
  99.           case 0xed:
  100.                key=12;
  101.                break;

  102.           case 0xdd:
  103.                key=9;
  104.                break;

  105.           case 0xbd:
  106.                key=6;
  107.                break;

  108.           case 0x7d:
  109.                key=3;
  110.                break;
  111.          }
  112.          while(temp!=0xf0)
  113.          {
  114.            temp=P2;
  115.            temp=temp&0xf0;
  116.          }
  117.                  return key;
  118.       }
  119.       }
  120.     P2=0xfb;
  121.     temp=P2;
  122.     temp=temp&0xf0;
  123.     if(temp!=0xf0)
  124.     {
  125.       delay(5);
  126.       if(temp!=0xf0)
  127.       {
  128.         temp=P2;
  129.         switch(temp)
  130.         {
  131.           case 0xeb:
  132.                key=0;
  133.                break;

  134.           case 0xdb:
  135.                key=8;
  136.                break;

  137.           case 0xbb:
  138.                key=5;
  139.                break;

  140.           case 0x7b:
  141.                key=2;
  142.                break;
  143.          }
  144.          while(temp!=0xf0)
  145.          {
  146.            temp=P2;
  147.            temp=temp&0xf0;
  148.          }
  149.                  return key;
  150.       }
  151.       }
  152.     P2=0xf7;
  153.     temp=P2;
  154.     temp=temp&0xf0;
  155.     if(temp!=0xf0)
  156.     {
  157.       delay(5);
  158.       if(temp!=0xf0)
  159.       {
  160.         temp=P2;
  161.         switch(temp)
  162.         {
  163.           case 0xe7:
  164.                key=11;
  165.                break;

  166.           case 0xd7:
  167.                key=7;
  168.                break;

  169.           case 0xb7:
  170.                key=4;
  171.                break;

  172.           case 0x77:
  173.                key=1;
  174.                break;
  175.          }
  176.          while(temp!=0xf0)
  177.          {
  178.            temp=P2;
  179.            temp=temp&0xf0;
  180.          }
  181.                  return key;
  182.       }
  183.     }
  184.         return 16;
  185. }

  186. void main()
  187. {
  188.         uchar a;
  189.         uchar b;
  190.         uchar c;
  191.         D1=0;
  192.         init();
  193.         write_com(0x80);         //将第一个字符写在向右偏移17个字符处,为后面的由右向左划入做准备。
  194.         delay(20);
  195.         for(a=0;a<16;a++)
  196.         {
  197.         write_date(table1[a]);
  198.         delay(20);               
  199.         }
  200.         while(1)
  201.         {
  202.                 b=key_scan();
  203.                 if(b!=16)
  204.                 {
  205.                   c=b;
  206.                   write_com(0xc0|0x07);
  207.                  write_date('0'+c);
  208.                   write_date('D');
  209.                 }
  210.                 if(TR==1)
  211.                 {
  212.                         D1=0;
  213.                         write_com(0xc0);
  214.                         delay(1);
  215.                         for(a=0;a<6;a++)
  216.                         {
  217.                         write_date(table2[a]);
  218.                         delay(1);               
  219.                         }
  220.                 }
  221.                 else
  222.                 {
  223.                         D1=1;
  224.                         delay(c*20);
  225.                         D1=0;
  226.                         write_com(0xc0);
  227.                         for(a=0;a<6;a++)
  228.                         {
  229.                         write_date(table3[a]);               
  230.                         }
  231.                         
  232.                 }
  233.                         
  234.         }
  235. }
复制代码

所有资料51hei提供下载:
源代码.rar (24.19 KB, 下载次数: 26)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:1 发表于 2019-4-17 01:23 | 显示全部楼层
本帖需要重新编辑补全电路原理图,源码,详细说明与图片即可获得100+黑币(帖子下方有编辑按钮)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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