找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1672|回复: 5
收起左侧

做的寻迹小车proteus仿真时,出现一个电机正转,一个反转,这是我程序出问题了吗

[复制链接]
ID:912376 发表于 2021-4-30 14:02 | 显示全部楼层 |阅读模式
我用仿真寻迹时,出现一个正转,一个反转,
但仿真蓝牙时,就没有出现这种情况。
回复

使用道具 举报

ID:96682 发表于 2021-4-30 17:53 来自手机 | 显示全部楼层
问题是没图程序不好找问题
回复

使用道具 举报

ID:912376 发表于 2021-5-1 14:42 | 显示全部楼层
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar temp,signal,tt1,t0,t1,t2,t3,t4,t5,t6;


void delay_1ms(uint d)
{
        uint i;
        while(d--)
                for(i=0;i<75;i++);
}

void motor_run()                   //电机起动
{
          P1=0x35;
        delay_1ms(200);
        P1=0x53;
        delay_1ms(10);
/*        ENA=1;

        OUT1=0;

        OUT2=1;
        delay_1ms(800);
        OUT2=0;
        delay_1ms(200);
       
        ENB=1;

        OUT4=0;

        OUT3=1;
        delay_1ms(800);
        OUT3=0;
        delay_1ms(200);
*/
}
void motor_left()                         //左进
{
        P1=0x30;
        delay_1ms(200);
        P1=0x00;
        delay_1ms(20);
}

void motor_right()                   //右进
{
        P1=0x05;
        delay_1ms(200);
        P1=0x00;
        delay_1ms(20);
}

void motor_big_right()                         //粗右进
{
        P1=0x55;
        delay_1ms(200);
        P1=0x00;
        delay_1ms(20);
}
void motor_big_left()                                 //粗左转
{
        P1=0x33;
        delay_1ms(200);
        P1=0x00;
        delay_1ms(20);
}
void motor_stop()                                        //电机停止
{
         P1=0x00;
}
void motor_back()                                        //电机后退
{
        P1=0x53;
}
void main()
{
        t0=0;
        t1=0;
        t2=0;
        t3=0;
        t4=0;
        t5=0;
        t6=0;

        tt1=0;
        EA=1;
        ET1=1;
        TR1=1;
        TMOD=0x01;
        TH1=-(1000/256);
        TL1=-(1000%256);
        while(1)
        {

                 temp=P2;
                signal=temp&0xff;                   //得到红外反向信号
                switch(signal)
                {
                        case 0xff:           //无偏差
                                motor_run();
                                t0++;
                                if(t0==10)
                                {
                                        t0=0;
                                        motor_left();
                                        motor_right();
                                }
                                t1=t2=t3=t4=t5=t6=0;       
                                break;
                        case 0xfd:           //1轮右偏                       

                                motor_big_left();
                                t1++;
                                if(t1==4)
                                {
                                        t1=0;
                                        motor_left();
                                }
                                t0=t2=t3=t4=t5=t6=0;       
                                break;
                        case 0xef:            //4轮左偏                          

                                motor_big_right();
                                t2++;
                                if(t2==4)
                                {
                                        t2=0;
                                        motor_right();
                                }
                                t0=t1=t3=t4=t5=t6=0;       
                                break;
                        case 0xfb:     //2轮右偏出轨
                        case 0xf9:           //1、2轮右偏                                               
                                motor_big_left();
                                t3++;
                                if(t3==4)
                                {
                                        t3=0;
                                        motor_left();
                                }
                                t0=t1=t2=t4=t5=t6=0;       
//                                delay_1ms(10);
                                break;

                        case 0xdf:           //5左偏出轨                          
                        case 0xcf:      //4、5轮左偏
                                motor_big_right();
                                t4++;
                                if(t4==4)
                                {
                                        t4=0;
                                        motor_right();
                                }
                                t0=t1=t2=t3=t5=t6=0;       
//                                delay_1ms(10);
                                break;

                        case 0xfe:           //0最右偏出轨          
                        case 0xfa:
                                motor_big_left();
                                t5++;
                                if(t5==1)
                                {
                                        t5=0;
                                        motor_left();
                                        motor_left();
                                }
                                t0=t1=t2=t3=t4=t6=0;       
//                                delay_1ms(10);
                                break;

                        case 0xbf:     //6最左偏出轨          
                        case 0x9f:          
                                motor_big_right();
                                t6++;
                                if(t6==1)
                                {
                                        t6=0;
                                        motor_right();
                                        motor_right();
                                }
                                t0=t1=t2=t3=t4=t5=0;       
//                                delay_1ms(10);
                                break;       
                       
                        case 0xeb:         //前两传感器压在黑线上
                        case 0xdb:         //后两传感器压在黑线上
                        case 0xbe:         //中间两传感器压在黑线上
                        case 0xac:                 //前四传感器压在黑线上
                        case 0x9a:                //后四传感器压在黑线上
                                motor_back();
                                delay_1ms(200);
                                motor_stop();
                               
//                                delay_1ms(1000);                                                                                                                                         

                        default:
//                                motor_back();
//                                delay_1ms(50);
//                                motor_left();
//                                motor_right();
                                break;
                }  
        }
                       
}

/*void time1() interrupt 3
{

        TH1=-(1000/256);
        TL1=-(1000%256);
        tt1++;
        if(tt1==300)
        {
                motor_back();
        //        delay_1ms(1);
                tt1=0;
        }
} */[/code]
回复

使用道具 举报

ID:912376 发表于 2021-5-1 14:48 | 显示全部楼层

在运行程序时,电机一个正转,一个反转

在运行程序时,电机一个正转,一个反转
  1. #include<reg52.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. uchar temp,signal,tt1,t0,t1,t2,t3,t4,t5,t6;


  5. void delay_1ms(uint d)
  6. {
  7.         uint i;
  8.         while(d--)
  9.                 for(i=0;i<75;i++);
  10. }

  11. void motor_run()                   //电机起动
  12. {
  13.           P1=0x35;
  14.         delay_1ms(200);
  15.         P1=0x53;
  16.         delay_1ms(10);
  17. /*        ENA=1;

  18.         OUT1=0;

  19.         OUT2=1;
  20.         delay_1ms(800);
  21.         OUT2=0;
  22.         delay_1ms(200);
  23.        
  24.         ENB=1;

  25.         OUT4=0;

  26.         OUT3=1;
  27.         delay_1ms(800);
  28.         OUT3=0;
  29.         delay_1ms(200);
  30. */
  31. }
  32. void motor_left()                         //左进
  33. {
  34.         P1=0x30;
  35.         delay_1ms(200);
  36.         P1=0x00;
  37.         delay_1ms(20);
  38. }

  39. void motor_right()                   //右进
  40. {
  41.         P1=0x05;
  42.         delay_1ms(200);
  43.         P1=0x00;
  44.         delay_1ms(20);
  45. }

  46. void motor_big_right()                         //粗右进
  47. {
  48.         P1=0x55;
  49.         delay_1ms(200);
  50.         P1=0x00;
  51.         delay_1ms(20);
  52. }
  53. void motor_big_left()                                 //粗左转
  54. {
  55.         P1=0x33;
  56.         delay_1ms(200);
  57.         P1=0x00;
  58.         delay_1ms(20);
  59. }
  60. void motor_stop()                                        //电机停止
  61. {
  62.          P1=0x00;
  63. }
  64. void motor_back()                                        //电机后退
  65. {
  66.         P1=0x53;
  67. }
  68. void main()
  69. {
  70.         t0=0;
  71.         t1=0;
  72.         t2=0;
  73.         t3=0;
  74.         t4=0;
  75.         t5=0;
  76.         t6=0;

  77.         tt1=0;
  78.         EA=1;
  79.         ET1=1;
  80.         TR1=1;
  81.         TMOD=0x01;
  82.         TH1=-(1000/256);
  83.         TL1=-(1000%256);
  84.         while(1)
  85.         {

  86.                  temp=P2;
  87.                 signal=temp&0xff;                   //得到红外反向信号
  88.                 switch(signal)
  89.                 {
  90.                         case 0xff:           //无偏差
  91.                                 motor_run();
  92.                                 t0++;
  93.                                 if(t0==10)
  94.                                 {
  95.                                         t0=0;
  96.                                         motor_left();
  97.                                         motor_right();
  98.                                 }
  99.                                 t1=t2=t3=t4=t5=t6=0;       
  100.                                 break;
  101.                         case 0xfd:           //1轮右偏                       

  102.                                 motor_big_left();
  103.                                 t1++;
  104.                                 if(t1==4)
  105.                                 {
  106.                                         t1=0;
  107.                                         motor_left();
  108.                                 }
  109.                                 t0=t2=t3=t4=t5=t6=0;       
  110.                                 break;
  111.                         case 0xef:            //4轮左偏                          

  112.                                 motor_big_right();
  113.                                 t2++;
  114.                                 if(t2==4)
  115.                                 {
  116.                                         t2=0;
  117.                                         motor_right();
  118.                                 }
  119.                                 t0=t1=t3=t4=t5=t6=0;       
  120.                                 break;
  121.                         case 0xfb:     //2轮右偏出轨
  122.                         case 0xf9:           //1、2轮右偏                                               
  123.                                 motor_big_left();
  124.                                 t3++;
  125.                                 if(t3==4)
  126.                                 {
  127.                                         t3=0;
  128.                                         motor_left();
  129.                                 }
  130.                                 t0=t1=t2=t4=t5=t6=0;       
  131. //                                delay_1ms(10);
  132.                                 break;

  133.                         case 0xdf:           //5左偏出轨                          
  134.                         case 0xcf:      //4、5轮左偏
  135.                                 motor_big_right();
  136.                                 t4++;
  137.                                 if(t4==4)
  138.                                 {
  139.                                         t4=0;
  140.                                         motor_right();
  141.                                 }
  142.                                 t0=t1=t2=t3=t5=t6=0;       
  143. //                                delay_1ms(10);
  144.                                 break;

  145.                         case 0xfe:           //0最右偏出轨          
  146.                         case 0xfa:
  147.                                 motor_big_left();
  148.                                 t5++;
  149.                                 if(t5==1)
  150.                                 {
  151.                                         t5=0;
  152.                                         motor_left();
  153.                                         motor_left();
  154.                                 }
  155.                                 t0=t1=t2=t3=t4=t6=0;       
  156. //                                delay_1ms(10);
  157.                                 break;

  158.                         case 0xbf:     //6最左偏出轨          
  159.                         case 0x9f:          
  160.                                 motor_big_right();
  161.                                 t6++;
  162.                                 if(t6==1)
  163.                                 {
  164.                                         t6=0;
  165.                                         motor_right();
  166.                                         motor_right();
  167.                                 }
  168.                                 t0=t1=t2=t3=t4=t5=0;       
  169. //                                delay_1ms(10);
  170.                                 break;       
  171.                        
  172.                         case 0xeb:         //前两传感器压在黑线上
  173.                         case 0xdb:         //后两传感器压在黑线上
  174.                         case 0xbe:         //中间两传感器压在黑线上
  175.                         case 0xac:                 //前四传感器压在黑线上
  176.                         case 0x9a:                //后四传感器压在黑线上
  177.                                 motor_back();
  178.                                 delay_1ms(200);
  179.                                 motor_stop();
  180.                                
  181. //                                delay_1ms(1000);                                                                                                                                         

  182.                         default:
  183. //                                motor_back();
  184. //                                delay_1ms(50);
  185. //                                motor_left();
  186. //                                motor_right();
  187.                                 break;
  188.                 }  
  189.         }
  190.                        
  191. }

  192. /*void time1() interrupt 3
  193. {

  194.         TH1=-(1000/256);
  195.         TL1=-(1000%256);
  196.         tt1++;
  197.         if(tt1==300)
  198.         {
  199.                 motor_back();
  200.         //        delay_1ms(1);
  201.                 tt1=0;
  202.         }
  203. } */
复制代码


回复

使用道具 举报

ID:912376 发表于 2021-5-1 16:16 | 显示全部楼层
wc86110 发表于 2021-4-30 17:53
问题是没图程序不好找问题

我已经发了代码和原理图,您抽时间看一下,万分感谢
如果还是不明白,您可以留个邮箱,我把仿真程序和程序可以发给你,
回复

使用道具 举报

ID:96682 发表于 2021-5-1 16:28 来自手机 | 显示全部楼层
用普通的直流电机调换线头就行了
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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