找回密码
 立即注册

QQ登录

只需一步,快速开始

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

这个单片机程序咋样做到电机反转?求指导

[复制链接]
跳转到指定楼层
楼主

总共6个按键,左边第一个是开窗,显示屏显示hand1,下面接着的是关窗,显示hand2,后面是停止键,显示hand0,此时电机应该要停下来,右边3个第一个和第二个都是限位开关,比如此时是hand1状态,按一下这两个中的任何一个都会变为hand0,就代表电机停下来的意思,右边最后一个键是模式选择键,可以调到语音模式,此时要是闭合最上面的第一个刀开关,显示sound1,开窗,闭合第二个就显示sound2,关窗,两个都打开就是sound0,代表停止

单片机源程序如下:
  1. #include<reg52.h>
  2. #include<intrins.h>
  3. #include<stdio.h>
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. sbit E=P2^7;
  7. sbit RW=P2^6;
  8. sbit RS=P2^5;
  9. sbit K2=P3^4;
  10. sbit K1=P2^4;
  11. sbit xianwei1=P2^0;
  12. sbit xianwei2=P2^1;
  13. sbit S1=P2^2;
  14. sbit S2=P2^3;
  15. unsigned char code F_Rotation[8]={0x08,0x18,0x10,0x30,0x20,0x60,0x40,0x48};
  16. unsigned char code B_Rotation[8]={0x48,0x40,0x60,0x20,0x30,0x10,0x18,0x08};
  17. uchar xwflag=0;
  18. uchar FB_flag=0;
  19. uchar a1=0,a2=0;
  20. uchar b1=0,b2=0;
  21. uchar c1=0,c2=0;
  22. uchar d1=0,d2=0;
  23. uchar moshi=0;
  24. void Delay(uint i)
  25. {     
  26.        uchar x,j;
  27.        for(j=0;j<i;j++)
  28.        for(x=0;x<=148;x++);
  29. }
  30. void delays()
  31. {
  32.       _nop_();
  33.       _nop_();
  34.       _nop_();
  35.       _nop_();
  36.       _nop_();
  37. }
  38. bit Busy(void)
  39. {
  40.       bit busy_flag=0;
  41.       RS=0;
  42.       RW=1;
  43.        E=1;
  44.        delays();
  45.        busy_flag=(bit)(P0 & 0x80);
  46.        E=0;
  47.        return busy_flag;
  48. }
  49. void wcmd(uchar del)
  50. {
  51.      while(Busy());
  52.      RS=0;
  53.       RW=0;
  54.       E=0;
  55.       delays();
  56.       P0=del;
  57.       delays();
  58.      E=1;
  59.       delays();
  60.       E=0;
  61. }
  62. void wdata (uchar del)
  63. {
  64.       while(Busy());
  65.      RS=1;
  66.       RW=0;
  67.       E=0;
  68.       delays();
  69.       P0=del;
  70.       delays();
  71.       E=1;
  72.       delays();
  73.       E=0;
  74. }
  75. void L1602_init(void)
  76. {
  77.      wcmd(0x38);
  78.      wcmd(0x0c);
  79.       wcmd(0x06);
  80.       wcmd(0x01);
  81. }
  82. void L1602_char(uchar hang, uchar lie, char sign)
  83. {
  84.       uchar a;
  85.       if(hang==1) a=0x80;
  86.      if(hang==2) a=0xc0;
  87.       a=a+lie -1;
  88.       wcmd(a);
  89.        wdata(sign);
  90. }
  91. void L1602_string(uchar hang, uchar lie, uchar *p )
  92. {
  93.         uchar a,b=0;
  94.          if(hang==1) a=0x80;
  95.      if(hang==2) a=0xc0;
  96.       a=a+lie -1;
  97.       while(1)
  98.          {
  99.                 wcmd(a++);
  100.                 if((*p=='\0') || (b==16))  break;
  101.                 b++;
  102.                 wdata(*p);
  103.                  p++;
  104.            }
  105. }
  106. void main(void)
  107. {
  108.         unsigned char i;
  109.         IT0=1;
  110.          EX0=1;
  111.          PX0=1;
  112.          IT1=1;
  113.          EX1=1;
  114.          PX1=1;
  115.           EA=1;
  116.          L1602_init();
  117.           Delay(100);
  118.           L1602_char(1,1,'M');
  119.           L1602_char(1,2,'o');
  120.           L1602_char(1,3,'d');
  121.           L1602_char(1,4,'e');
  122.           L1602_char(1,5,':');
  123.         while(1)
  124.       {
  125.              L1602_char(2,6,FB_flag%10+48);
  126.              if(i>7) i=0;
  127.              if(FB_flag==1) {  P1=F_Rotation[i]; }
  128.               else if(FB_flag==2) {  P1= B_Rotation[i]; }
  129.                   else{ P1=0xff; }
  130.                    i++;
  131.                    Delay(100);
  132.                    if(K1==0){ a1=1; a2=0; }
  133.                    if( a1==1&&a2==0)
  134.                          {
  135.                               FB_flag=0;
  136.                                a2=1;
  137.                          }
  138.                      if(K1==1){ a1=0;a2=0; }
  139.                      if( K2==0)   { d1=1;d2=0; }
  140.                       if(d1==1&&d2==0)
  141.                             {
  142.                                     FB_flag=1;
  143.                                     xwflag=0;
  144.                                     d2=1;
  145.                               }
  146.                        if(K2==1) { d1=0;d2=0; }
  147.                         if(moshi==0)
  148.                          {
  149.                                  L1602_char(1,6,'H');
  150.                                   L1602_char(1,7,'a');
  151.                                   L1602_char(1,8,'n');
  152.                                   L1602_char(1,9,'d');
  153.                                    L1602_char(1,10,' ');
  154.                                     L1602_char(1,16, FB_flag%10+48);
  155.                                   L1602_char(2,16,' ');
  156.                          }
  157.                       if(moshi==1)
  158.                    {
  159.                        L1602_char(1,6,'S');
  160.                        L1602_char(1,7,'o');
  161.                        L1602_char(1,8,'u');
  162.                        L1602_char(1,9,'n');
  163.                        L1602_char(1,10,'d');
  164.                        if(S1==0)  L1602_char(2,1,0+48);  else L1602_char(2,1,1+48);
  165.                        if(S2==0)  L1602_char(2,2,0+48);  else L1602_char(2,2,1+48);
  166.                        if(S1==1&&S2==0)  {  xwflag=0;FB_flag=1; L1602_char(1,16,FB_flag%10+48) ;}
  167.                         if(S1==0&&S2==1)  {  xwflag=0;FB_flag=2; L1602_char(1,16,FB_flag%10+48) ;}
  168.                          if(S1==1&&S2==1) { FB_flag=0; L1602_char(1,16,FB_flag%10+48) ;}
  169.                          L1602_char(2,16,' ');
  170.                    }
  171.                      
  172.                  if(xianwei1==0) { b1=1;b2=0;}
  173.                  if(b1==1&&b2==0)
  174.                     {
  175.                             FB_flag=0;
  176.                              xwflag=1;
  177.                              b2=1;
  178.                         }
  179.                    if(xianwei1==1)  { b1=0;b2=0;}
  180.                    if(xianwei2==0)  { c1=1;c2=0; }
  181.                     if(c1==1&&c2==0)
  182.                           {
  183.                                  FB_flag=0;
  184.                                   xwflag=2;
  185.                                   c2=1;
  186.                              }
  187.                      if(xianwei2==1)  { c1=0;c2=0; }
  188.                      if(xwflag!=0)  FB_flag=0;
  189.                          }
  190.                     }
  191.                         
  192. void it0() interrupt 0
  193. {            
  194.              moshi++;
  195.               if(moshi==3)  moshi=0;
  196.               FB_flag=0;
  197. }
  198. void it1() interrupt 2
  199. {
  200.              FB_flag=2;
  201.              xwflag=0;
  202. }
复制代码

全部资料51hei下载地址:
仿真.zip (21.64 KB, 下载次数: 3)
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:704156 发表于 2020-4-3 12:40 | 只看该作者
我感觉是程序的错误,但不知道错在哪,
回复

使用道具 举报

板凳
ID:704156 发表于 2020-4-3 16:55 | 只看该作者
总共6个按键,左边第一个是开窗,显示屏显示hand1,下面接着的是关窗,显示hand2,后面是停止键,显示hand0,此时电机应该要停下来,右边3个第一个和第二个都是限位开关,比如此时是hand1状态,按一下这两个中的任何一个都会变为hand0,就代表电机停下来的意思,右边最后一个键是模式选择键,可以调到语音模式,此时要是闭合最上面的第一个刀开关,显示sound1,开窗,闭合第二个就显示sound2,关窗,两个都打开就是sound0,代表停止
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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