极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11670|回复: 3

求教:用ArduinoMega2560完成11路PWM采集和4路PWM输出

[复制链接]
发表于 2014-11-21 22:06:36 | 显示全部楼层 |阅读模式
尊敬的各位大侠、专家、高手:
我打算采用ArduinoMega2560板子采集11路PWM输入,然后用USB口通过串口协议发送给手机解算后,再发回Arduino,由arduino生成4路PWM波,驱动小车的四个轮子旋转。
现在已经搞了一个初步的代码,其中有5路采用查询方式采集,6路采用外部中断采集。然而,问题是当手机从串口发回数据时会影响Arduino的PWM波采集值,大约有10%的概率采集到的PWM波会突然变小一些,导致采集的误差发生,波动较大。
请问各位尊敬的大侠、专家、高手改如何解决?不胜感激。





  1. int ppm1 = 2;  
  2. int ppm2 = 3;
  3. int ppm3 = 18;  
  4. int ppm4 = 19;
  5. int ppm5 = 20;
  6. int ppm6 = 21;
  7. int pwmInput1 = 0;
  8.   int pwmInput2 = 0;
  9.   int pwmInput3 = 0;
  10.   int pwmInput4 = 0;
  11.   int pwmInput5 = 0;
  12. unsigned long rc1_PulseStartTicks,rc2_PulseStartTicks,rc3_PulseStartTicks,rc4_PulseStartTicks,rc5_PulseStartTicks,rc6_PulseStartTicks;        
  13. volatile int rc1_val, rc2_val,rc3_val, rc4_val,rc5_val,rc6_val;  
  14. float startTime = 0;
  15. void setup() {
  16. Serial.begin(115200);
  17.       

  18.         ////PPM inputs from RC receiver
  19.         pinMode(ppm1, INPUT);
  20.        pinMode(ppm2, INPUT);
  21.          pinMode(ppm3, INPUT);
  22.         pinMode(ppm4, INPUT);
  23.          pinMode(ppm5, INPUT);


  24.         // 电平变化即触发中断
  25.         attachInterrupt(0, rc1, CHANGE);   
  26.         attachInterrupt(1, rc2, CHANGE);
  27.     attachInterrupt(5, rc3, CHANGE);   
  28.        attachInterrupt(4, rc4, CHANGE);   
  29.      attachInterrupt(3, rc5, CHANGE);  
  30.     attachInterrupt(2, rc6, CHANGE);

  31. pinMode(31,INPUT);
  32.    pinMode(33,INPUT);
  33.     pinMode(35,INPUT);
  34.     pinMode(37,INPUT);
  35.     pinMode(39,INPUT);
  36. startTime = millis();
  37. }

  38. void rc1()
  39. {
  40.         // did the pin change to high or low?
  41.         if (digitalRead(ppm1 ) == HIGH)
  42.                 rc1_PulseStartTicks = micros();    // store the current micros() value
  43.         else
  44.                 rc1_val = micros() - rc1_PulseStartTicks;
  45.                 map(rc1_val,900,2100,0,255);
  46. }

  47. void rc2()
  48. {
  49.         // did the pin change to high or low?
  50.         if (digitalRead( ppm2 ) == HIGH)
  51.                 rc2_PulseStartTicks = micros();   
  52.         else
  53.                 rc2_val = micros() - rc2_PulseStartTicks;
  54.                 map(rc2_val,900,2100,0,255);
  55. }

  56. void rc3()
  57. {
  58.         // did the pin change to high or low?
  59.         if (digitalRead( ppm3 ) == HIGH)
  60.                 rc3_PulseStartTicks = micros();   
  61.         else
  62.                 rc3_val = micros() - rc3_PulseStartTicks;
  63.                 map(rc3_val,900,2100,0,255);
  64. }

  65. void rc4()
  66. {
  67.         // did the pin change to high or low?
  68.         if (digitalRead( ppm4 ) == HIGH)
  69.                 rc4_PulseStartTicks = micros();   
  70.         else
  71.                 rc4_val = micros() - rc4_PulseStartTicks;
  72.                map(rc4_val,900,2100,0,255);  
  73. }

  74. void rc5()
  75. {
  76.         // did the pin change to high or low?
  77.         if (digitalRead( ppm5 ) == HIGH)
  78.                 rc5_PulseStartTicks = micros();   
  79.         else
  80.                 rc5_val = micros() - rc5_PulseStartTicks;
  81.                 map(rc5_val,900,2100,0,255);
  82. }

  83. void rc6()
  84. {
  85.         // did the pin change to high or low?
  86.         if (digitalRead( ppm6 ) == HIGH)
  87.                 rc6_PulseStartTicks = micros();   
  88.         else
  89.                 rc6_val = micros() - rc6_PulseStartTicks;
  90.                 map(rc6_val,900,2100,0,255);
  91. }

  92. unsigned long time = 0;
  93. int loopCounter = 1;
  94. void loop() {
  95. int checkSum = 0;
  96. delayMicroseconds(17500);
  97.   pwmInput1 = pulseIn(31, HIGH);
  98.   delayMicroseconds(500);
  99.   pwmInput2 = pulseIn(33,HIGH);
  100.   delayMicroseconds(500);
  101.   pwmInput3 = pulseIn(35,HIGH);
  102.   delayMicroseconds(500);
  103.   pwmInput4 = pulseIn(37, HIGH);
  104.   delayMicroseconds(500);
  105.   pwmInput5 = pulseIn(39, HIGH);
  106.   delayMicroseconds(500);
  107.   pwmInput1 = map(pwmInput1,900,2100,0,255);     
  108.   pwmInput2 = map(pwmInput2,900,2100,0,255);      
  109.   pwmInput3 = map(pwmInput3,900,2100,0,255);      
  110.   pwmInput4 = map(pwmInput4,900,2100,0,255);      
  111.   pwmInput5 = map(pwmInput5,900,2100,0,255);
  112. checkSum = pwmInput1+pwmInput2+pwmInput3+pwmInput4+pwmInput5+rc1_val+rc2_val+rc3_val+rc4_val+rc5_val+rc6_val;
  113. checkSum = checkSum%256;

  114.   
  115. //        //Send to Console
  116. //        Serial.print("No.  ");
  117. //        Serial.print(loopCounter);
  118. //        Serial.print('\t');
  119. //        Serial.print("Time  ");
  120. //        Serial.print((millis()-startTime)/1000);
  121. //       Serial.print('\t');
  122. //        Serial.print(pwmInput1);  
  123. //        Serial.print('\t');  
  124. //         Serial.print(pwmInput2);  
  125. //        Serial.print('\t');  
  126. //         Serial.print(pwmInput3);  
  127. //        Serial.print('\t');  
  128. //         Serial.print(pwmInput4);  
  129. //        Serial.print('\t');  
  130. //         Serial.print(pwmInput5);  
  131. //        Serial.print('\t');  
  132. //        Serial.print(rc1_val);  
  133. //        Serial.print('\t');  
  134. //        Serial.print(rc2_val);
  135. //         Serial.print('\t');  
  136. //        Serial.print(rc3_val);
  137. //         Serial.print('\t');  
  138. //        Serial.print(rc4_val);
  139. //         Serial.print('\t');  
  140. //        Serial.print(rc5_val);
  141. //        Serial.print('\t');
  142. //        Serial.print(rc6_val);
  143. //        Serial.print('\n');
  144. //        loopCounter ++;
  145.         
  146.         //Send to Android
  147.         Serial.write(0xFE);
  148.         Serial.write(0xFE);
  149.         Serial.write(0xFE);
  150.         Serial.write((byte)loopCounter);
  151.         Serial.write((byte)pwmInput1);  
  152.          Serial.write((byte)pwmInput2);  
  153.          Serial.write((byte)pwmInput3);  
  154.          Serial.write((byte)pwmInput4);   
  155.          Serial.write((byte)pwmInput5);   
  156.         Serial.write((byte)rc1_val);   
  157.         Serial.write((byte)rc2_val);   
  158.         Serial.write((byte)rc3_val);  
  159.         Serial.write((byte)rc4_val);  
  160.         Serial.write((byte)rc5_val);
  161.         Serial.write((byte)rc6_val);
  162.         Serial.write((byte)checkSum);
  163.         Serial.write(0xFE);
  164.         Serial.write(0xFE);
  165.         Serial.write(0xFE);
  166.         Serial.write((byte)loopCounter);
  167.         Serial.write((byte)pwmInput1);  
  168.          Serial.write((byte)pwmInput2);  
  169.          Serial.write((byte)pwmInput3);  
  170.          Serial.write((byte)pwmInput4);   
  171.          Serial.write((byte)pwmInput5);   
  172.         Serial.write((byte)rc1_val);   
  173.         Serial.write((byte)rc2_val);   
  174.         Serial.write((byte)rc3_val);  
  175.         Serial.write((byte)rc4_val);  
  176.         Serial.write((byte)rc5_val);
  177.         Serial.write((byte)rc6_val);
  178.         Serial.write((byte)checkSum);
  179.         Serial.write(0xFE);
  180.         Serial.write(0xFE);
  181.         Serial.write(0xFE);
  182.         Serial.write((byte)loopCounter);
  183.         Serial.write((byte)pwmInput1);  
  184.          Serial.write((byte)pwmInput2);  
  185.          Serial.write((byte)pwmInput3);  
  186.          Serial.write((byte)pwmInput4);   
  187.          Serial.write((byte)pwmInput5);   
  188.         Serial.write((byte)rc1_val);   
  189.         Serial.write((byte)rc2_val);   
  190.         Serial.write((byte)rc3_val);  
  191.         Serial.write((byte)rc4_val);  
  192.         Serial.write((byte)rc5_val);
  193.         Serial.write((byte)rc6_val);
  194.         Serial.write((byte)checkSum);
  195.         loopCounter++;
  196.         if (loopCounter==255)
  197.         loopCounter = 0;

  198.    
  199. }
复制代码
回复

使用道具 举报

发表于 2014-11-22 08:19:17 | 显示全部楼层
不懂帮顶,
回复 支持 反对

使用道具 举报

发表于 2014-11-22 11:14:48 | 显示全部楼层
代码好长,有点看不懂
回复 支持 反对

使用道具 举报

发表于 2014-11-22 14:20:35 来自手机 | 显示全部楼层
接收 PWM 的方法有問題:
1) interrupt 之間互相影響下,時間不準確
2) isr 中不可以用時間的函數,i.e. micro()

我之前出過一個帖,研究如何讀取 pwm 值的,有興趣可看看。
回复 支持 反对

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-16 04:23 , Processed in 0.064657 second(s), 21 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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