找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机蓝牙控制小车前后左右+寻迹,到底哪里出了问题 ,小车通电之后完全不动

[复制链接]
跳转到指定楼层
楼主
ID:703117 发表于 2020-5-6 09:06 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #include "reg52.h"
  2. #include <intrins.h>
  3. sbit IN1=P1^0;  //定义驱动模块IN1为P1.0                  左上电机 M2                OUT1 OUT2
  4. sbit IN2=P1^1;        //定义驱动模块IN2为P1.1
  5. sbit IN3=P1^2;        //定义驱动模块IN3为P1.2                  左下电机 M3                OUT3 OUT4
  6. sbit IN4=P1^3;        //定义驱动模块IN4为P1.3
  7. sbit IN5=P1^4;        //定义驱动模块IN5为P1.4                  右上电机 M1                OUT5 OUT6
  8. sbit IN6=P1^5;        //定义驱动模块IN6为P1.5
  9. sbit IN7=P1^6;        //定义驱动模块IN7为P1.6                  右下电机 M4                OUT7 OUT8
  10. sbit IN8=P1^7;        //定义驱动模块IN8为P1.7
  11. //寻迹探头
  12. sbit left_led=P2^5;           //定义左路寻迹为P2.5
  13. sbit middle_led=P2^6;  //定义中路寻迹为P2.6
  14. sbit right_led=P2^7;   //定义右路寻迹为P2.7

  15. #define Left_moto_go      {IN1=1,IN2=0,IN3=1,IN4=0;}    //左边两个电机向前走
  16. #define Left_moto_back    {IN1=0,IN2=1,IN3=0,IN4=1;}         //左边两个电机向后转
  17. #define Left_moto_Stop    {IN1=0,IN2=0,IN3=0,IN4=0;}    //左边两个电机停转                             
  18. #define Right_moto_go     {IN5=1,IN6=0,IN7=1,IN8=0;}        //右边两个电机向前走
  19. #define Right_moto_back   {IN5=0,IN6=1,IN7=0,IN8=1;}        //右边两个电机向后转
  20. #define Right_moto_Stop   {IN5=0,IN6=0,IN7=0,IN8=0;}        //右边两个电机停转

  21. #define M2_moto_pwm          IN2         //PWM信号端        左上电机M2
  22. #define M3_moto_pwm          IN4         //                                左下电机M3
  23. #define M1_moto_pwm          IN6         //                                右上电机M1
  24. #define M4_moto_pwm          IN8         //                                右下电机M4
  25. //PWM 寻迹中调速  
  26. unsigned char pwm_val_left  =0;//变量定义
  27. unsigned char push_val_left =0;//左电机占空比N/10
  28. unsigned char pwm_val_right =0;//变量定义
  29. unsigned char push_val_right=0;//右电机占空比N/10

  30. bit Right_moto_stop=1;                  //在右电机调速时,如果右电机停止,PWM小于PUSH,PWM置1,否则为0;若PWM≥10,PWM关断
  31. bit Left_moto_stop =1;                  //在左电机调速时,如果右电机停止,PWM小于PUSH,PWM置1,否则为0;若PWM≥10,PWM关断

  32. unsigned  int  timer1=0;

  33. //        sfr T2CON   = 0xC8;
  34.     sfr T2MOD   = 0xC9;
  35. // sfr RCAP2L  = 0xCA;
  36. // sfr RCAP2H  = 0xCB;
  37. // sfr TL2     = 0xCC;
  38. // sfr TH2     = 0xCD;

  39. //蓝牙数据发送
  40.         #define left     'C'         //左转发送数据C
  41.     #define right    'D'         //右转发送数据D
  42.         #define up       'A'         //前进发送数据A
  43.     #define down     'B'         //后退发送数据B
  44.         #define stop     'F'         //停止发送数据F
  45.         #define Car      '1'         //手机软件1号键        表示寻迹按钮

  46.         char code str[] =  "收到指令,向前!\n";
  47.         char code str1[] = "收到指令,向后!\n";
  48.         char code str2[] = "收到指令,向左!\n";
  49.         char code str3[] = "收到指令,向右!\n";
  50.         char code str4[] = "收到指令,停止!\n";

  51.         unsigned char  i=0;
  52.         unsigned char  dat=0;
  53.     unsigned char  buff[5]=0; //接收缓冲字节        

  54. //        程序功能
  55.         bit  flag_REC=0;                                 //串行接收标去位
  56.         bit  flag    =0;                                  //
  57.         bit  flag_xj =0;                                //寻迹标志
  58. //延时函数         
  59. void delay(unsigned int k)
  60. {   
  61.      unsigned int x,y;
  62.          for(x=0;x<k;x++)
  63.          for(y=0;y<2000;y++);
  64. }
  65. //前进函数         加入PWM
  66. void  run_pwm(void)
  67. {
  68.          push_val_left=9;        //速度调节变量 0-9。。。9最小,0最大
  69.          push_val_right=9;        //速度调节变量 0-9。。。9最小,0最大
  70.          Left_moto_go ;    //左电机往前走
  71.          Right_moto_go ;   //右电机往前走
  72. }
  73. //后退函数         加入PWM
  74. void  backrun_pwm(void)
  75. {
  76.     push_val_left=9;        //速度调节变量 0-9。。。9最小,0最大
  77.         push_val_right=9;        //速度调节变量 0-9。。。9最小,0最大
  78.         Left_moto_back ;   //左电机往后走
  79.         Right_moto_back ;  //右电机往后走
  80. }
  81. //左转函数          加入PWM
  82. void  leftrun_pwm(void)
  83. {
  84.      push_val_left=9;        //速度调节变量 0-9。。。9最小,0最大
  85.          push_val_right=9;        //速度调节变量 0-9。。。9最小,0最大
  86.          Left_moto_Stop ;   //左电机往前走
  87.          Right_moto_go ;    //右电机往后走
  88. }
  89. //右转函数          加入PWM
  90. void  rightrun_pwm(void)
  91. {
  92.      push_val_left=9;        //减低一半速度         速度调节变量 0-9。。。9最小,0最大
  93.          push_val_right=9;        //减低一半速度         速度调节变量 0-9。。。9最小,0最大
  94.          Left_moto_go ;     //左电机往前走
  95.          Right_moto_Stop ;  //右电机往后走
  96. }
  97. //小车停止
  98. void  keepstop(void)
  99. {
  100.          Left_moto_Stop ;   //左电机往前走
  101.          Right_moto_Stop ;  //右电机往前走
  102. }
  103. //小车前进          全速
  104. void  run(void)
  105. {
  106.          Left_moto_go ;    //左电机往前走
  107.          Right_moto_go ;   //右电机往前走
  108. }
  109. //小车后退    全速
  110. void  backrun(void)
  111. {
  112.         Left_moto_back ;   //左电机往后走
  113.         Right_moto_back ;  //右电机往后走
  114. }
  115. //左转函数          全速
  116. void  leftrun(void)
  117. {
  118.          Left_moto_Stop ;   //左电机往前走
  119.          Right_moto_go ;    //右电机往后走
  120. }
  121. //右转函数          全速
  122. void  rightrun(void)
  123. {
  124.          Left_moto_go ;     //左电机往前走
  125.          Right_moto_Stop ;  //右电机往后走
  126. }

  127. //左电机调速
  128. void pwm_out_left_moto(void)
  129. {  
  130.    if(Left_moto_stop)
  131.    {
  132.     if(pwm_val_left<=push_val_left)
  133.                {
  134.                      M2_moto_pwm=1;          //M2的PWM
  135.                      M3_moto_pwm=1;          //M3的PWM
  136.                    }
  137.           else
  138.                {
  139.                  M2_moto_pwm=0;
  140.                      M3_moto_pwm=0;
  141.                    }
  142.         if(pwm_val_left>=10)
  143.                pwm_val_left=0;
  144.    }
  145.     else   
  146.           {
  147.            M2_moto_pwm=0;
  148.            M3_moto_pwm=0;
  149.                   }
  150. }
  151. //右电机调速
  152. void pwm_out_right_moto(void)
  153. {
  154.   if(Right_moto_stop)
  155.    {
  156.     if(pwm_val_right<=push_val_right)
  157.               {
  158.                M1_moto_pwm=1;            //M1
  159.                    M4_moto_pwm=1;            //M4
  160.                    }
  161.         else
  162.               {
  163.                    M1_moto_pwm=0;
  164.                    M4_moto_pwm=0;
  165.                   }
  166.         if(pwm_val_right>=10)
  167.                pwm_val_right=0;
  168.    }
  169.    else   
  170.           {
  171.            M1_moto_pwm=0;
  172.            M4_moto_pwm=0;
  173.               }
  174. }
  175. void Time1(void) interrupt 3    //3 为定时器1的中断号  1 定时器0的中断号 0 外部中断1 2 外部中断2  4 串口中断

  176. {
  177.     TH1 = 0xFF;
  178.         TL1 = 0x9C;
  179.     timer1++;  
  180.         pwm_val_left++;
  181.         pwm_val_right++;
  182.         pwm_out_left_moto();
  183.         pwm_out_right_moto();
  184. }
  185. //中断号为4 串口中断
  186. void sint() interrupt 4          //中断接收3个字节
  187. {

  188.     if(RI)                         //是否接收中断
  189.     {
  190.        RI=0;
  191.        dat=SBUF;
  192.        if(dat=='O'&&(i==0)) //接收数据第一帧
  193.          {
  194.             buff[i]=dat;
  195.             flag=1;        //开始接收数据
  196.          }
  197.        else
  198.       if(flag==1)
  199.      {
  200.       i++;
  201.       buff[i]=dat;
  202.       if(i>=2)
  203.       {i=0;flag=0;flag_REC=1 ;}  // 停止接收
  204.      }
  205.          }
  206. }
  207. //主函数
  208. void mian(void)
  209. {                         //         T1          T0
  210.   TMOD=0x11; //        0001 0001 工作方式3        两个独立8位计数器
  211.   TH1=0xFF;  //100uS定时          高8位
  212.   TL1=0x9c;         //                                  低8位
  213.   TH0=0;
  214.   TL0=0;

  215.     T2CON=0x34;                  //8位寄存器0011 0100           串行发送时钟 TCLK=1, T2工作于波特率发生器方式
  216.         T2MOD=0x00;                  //                                           串行接收时钟 RCLK=1,T2工作于波特率发生器方式
  217.         RCAP2H=0xFF;          //                                           为1时外部事件计数,为0时T2为定时器         
  218.         RCAP2L=0xDC ;          //波特率位4800  12/[32*(65536-x)]
  219.     SCON=0x50;  
  220.     PCON=0x00;
  221.     PS=1;
  222.         TR1=1;                          // 定时器1工作
  223.         ET1=1;                          // 打开定时器1中断
  224.         ES=1;                           // 串行口中断
  225.     EA=1;                            // 总中断允许
  226.         while(flag_xj)                           //循线标志位
  227.                   {
  228.                                if(flag_REC==1)
  229.                                  {
  230.                                   break;
  231.                                  }
  232.                     
  233.                             //有信号为0  没有信号为1
  234.               if(right_led==1&&middle_led==1&&left_led==1) //三路检测到黑线
  235.                           {
  236.                              Left_moto_stop=1;
  237.                              Right_moto_stop=1;
  238.                              run_pwm();
  239.                           }

  240.                           else
  241.                          {                           
  242.                             if(right_led==1&&middle_led==0&&left_led==0)                //右边检测到黑线
  243.                                   {         
  244.                                       Left_moto_stop=1;
  245.                                           Right_moto_stop=1;
  246.                                       rightrun_pwm();
  247.                                   }
  248.                                 if(right_led==0&&middle_led==0&&left_led==1)            //左边检测到黑线
  249.                                    {
  250.                                       Right_moto_stop=1;
  251.                                           Left_moto_stop=1;
  252.                                           leftrun_pwm();
  253.                               }
  254.                           }
  255.                   }
  256.          if(flag_REC==1)                                    //
  257.            {
  258.                 flag_REC=0;
  259.                 if(buff[0]=='O'&&buff[1]=='N')        //第一个字节为O,第二个字节为N,第三个字节为控制码
  260.                 switch(buff[2])
  261.              {
  262.                       case up :                                                    // 前进
  263.                 //          send_str( );
  264.                           flag_xj=0; //寻迹置0
  265.                           Left_moto_stop=0;
  266.                           Right_moto_stop=0;
  267.                           run();
  268.                           break;

  269.                       case down:                                                // 后退
  270.                         //  send_str1( );
  271.                            flag_xj=0;
  272.                           Left_moto_stop=0;
  273.                           Right_moto_stop=0;
  274.                           backrun();
  275.                           break;

  276.                       case left:                                                // 左转
  277.                 //          send_str2( );
  278.                           flag_xj=0;
  279.                           leftrun();
  280.                           break;

  281.                       case right:                                                // 右转
  282.                 //          send_str3( );
  283.                           flag_xj=0;
  284.                           Left_moto_stop=0;
  285.                           Right_moto_stop=0;
  286.                           rightrun();
  287.                           break;

  288.                       case stop:                                                // 停止
  289.                 //          send_str4( );
  290.                           flag_xj=0;
  291.                           Left_moto_stop=0;
  292.                           Right_moto_stop=0;
  293.                           keepstop();
  294.                           break;

  295.                           case Car :                                                //                 
  296.                           flag_xj=1;
  297.                           break;
  298.              }                                 
  299.          }
  300. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:282850 发表于 2020-5-6 11:27 | 只看该作者
程序是copy别人的吗?如果是自己写的,检查硬件、程序
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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