极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11086|回复: 2

PWM调速与MsTimer2冲突 导致电驱板两个控制输出端 只有一个可用

[复制链接]
发表于 2015-9-9 17:26:52 | 显示全部楼层 |阅读模式
PWM调速与MsTimer2冲突 导致电驱板两个控制输出端 只有一个可用
调速程序是

  1. int INA = 8;      //电机A正反转控制端
  2. int PWMA = 9;     //电机A调速端
  3. int INB = 10;      //电机B正反转控制端
  4. int PWMB = 11;  
  5. char getstr;

  6. void advance(int v)
  7. {
  8.   digitalWrite(INA,LOW);         //HIGH为前进,LOW为后退
  9.   digitalWrite(INB,HIGH);
  10.   analogWrite(PWMA,v);      //PWM调速
  11.   analogWrite(PWMB,v);
  12. }
  13. void bluetooth() {
  14.   getstr=Serial.read();  //读取串口数据
  15.   switch(getstr) {
  16.   case 'a':        
  17.   Serial.println("go forward!");
  18.   advance(250);break;
  19.   default:break;
  20.   }
  21. }
  22. void setup() {
  23.   pinMode(INA,OUTPUT);
  24.   pinMode(INB,OUTPUT);
  25.   pinMode(PWMB,OUTPUT);
  26.   pinMode(PWMA,OUTPUT);
  27.   
  28.   Serial.begin(9600);
  29. }

  30. void loop() {
  31.   bluetooth();
  32. }
复制代码
可以正常运行
后来加入MsTimer2就出现有一个驱动输出口不能控制,后来排查,在setup 中加入MsTimer2::start();就会出现问题
  1. #include <MsTimer2.h>
  2. #define  Value 1.0995574

  3. int INA=10;   
  4. int PWMA=11;     
  5. int INB=8;   
  6. int PWMB=9;     //电机控制端口
  7. int EchoPin = 4;  
  8. int TrigPin = 5;  //超声波端口
  9. float distance;
  10. char getstr;

  11. int out1=2;       //光码盘端口
  12. long c1=0;        //光码盘初始计数
  13. float r_velocity=0;//速度;

  14. void advance(int spe)   
  15.     {digitalWrite(INB,LOW);
  16.      digitalWrite(INA,HIGH);
  17.      analogWrite(PWMA,spe);
  18.      analogWrite(PWMB,spe);
  19.     }
  20. void stopp()      
  21.     {
  22.      digitalWrite(INB,HIGH);
  23.      analogWrite(PWMB,0);
  24.      digitalWrite(INA,HIGH);
  25.      analogWrite(PWMA,0);
  26.     }
  27. void read_csb()                  //超声波数据读取
  28.     {
  29.       digitalWrite(TrigPin, LOW);  
  30.       delayMicroseconds(2);
  31.       digitalWrite(TrigPin, HIGH);  
  32.       delayMicroseconds(10);           
  33.       digitalWrite(TrigPin, LOW);           
  34.       distance = pulseIn(EchoPin, HIGH)/58;  //单位换算为厘米           
  35.       Serial.print("UTdistance is ");
  36.       Serial.println(distance);     //窗口输出测距值
  37.     }  
  38. void bluetooth()
  39. {
  40.    getstr = Serial.read();  //读取串口数据
  41.    switch(getstr) {
  42.   case 'a':        
  43.     Serial.println("go forward!");
  44.     advance(250);        
  45.     break;
  46.   }
  47. }


  48. float velocity(int n)
  49. {
  50.   float vel =Value*n;
  51.   return vel;
  52. }
  53. void flash()
  54. {
  55.   int r;
  56.   r=c1;
  57.   r_velocity = velocity(r);
  58.   Serial.print("Motorspeed is ");
  59.   Serial.print(r_velocity);
  60.   Serial.println("cm/s");
  61.   c1 = 0;               
  62. }
  63. void COUNT()
  64. {
  65.   c1++;          //光码盘累加计数
  66. }
  67. void loop()
  68. {  
  69.    read_csb();
  70.    delay(1000);          //控制显示数据间隔,可以注释掉
  71.    if (distance<15 and distance>0)   //前方距离小于15cm
  72.     {      
  73.       stopp();                          //停止  
  74.     }
  75.    else
  76.     {
  77.       bluetooth();  
  78.     }
  79. }
  80. void setup()
  81. {   
  82.   pinMode(INA,OUTPUT);
  83.   pinMode(INB,OUTPUT);
  84.   pinMode(PWMB,OUTPUT);
  85.   pinMode(PWMA,OUTPUT);
  86.   
  87.   pinMode(EchoPin, INPUT);   
  88.   pinMode(TrigPin, OUTPUT);  
  89.   
  90.   attachInterrupt(0,COUNT,FALLING);
  91.   
  92.   Serial.begin(9600);
  93.   MsTimer2::set(1000, flash);        // 中断设置函数,每 1s 进入一次中断
  94.   MsTimer2::start();
  95.   
  96. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2015-9-9 17:30:20 | 显示全部楼层
我觉得既然有程序可以正常运行,应该就是电机驱动板没有问题,可是加入MsTimer2后,就出现问题,应该是程序的问题 所以希望大家帮忙纠正下错误
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-9-9 17:38:43 | 显示全部楼层
问题解决,使用了MsTimer2.h 后pin 11 和 pin 3 就不能再用做 PWM 输出了 更换的端口后解决
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 23:20 , Processed in 0.033905 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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