PPM信号程序 ppm输出不对-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 5549|回复: 0

PPM信号程序 ppm输出不对

[复制链接]
发表于 2013-7-10 14:28 | 显示全部楼层 |阅读模式
程序如下:

  1. #define PPMCH                 8

  2. #define LED 13
  3. #define PPMOUT 8

  4. unsigned int ppm1[PPMCH+1] = {
  5.   0, 1500, 1500, 1500, 1900, 1500, 1500, 1500, 1500};
  6. unsigned int ppm2[PPMCH+1] = {
  7.   0, 1500, 1500, 1500, 1900, 1500, 1500, 1500, 1500};
  8. unsigned int ppmout[PPMCH+1] = {
  9.   0, 1500, 1500, 1500, 1900, 1500, 1500, 1500, 1500};


  10. void PPM_ISR0(void)
  11. {
  12.   volatile static char ch;
  13.   static unsigned lastmicros;  
  14.   unsigned mic = micros();
  15.   if(digitalRead(2) == HIGH)
  16.   {
  17.     if(mic - lastmicros>3000)
  18.     {
  19.       ch = 0;
  20.     }
  21.     ppm1[ch] = mic - lastmicros;
  22.     lastmicros = mic;
  23.     ch = (++ch)%(PPMCH+1);
  24.   }
  25. }
  26. void PPM_ISR1(void)
  27. {
  28.   volatile static char ch;
  29.   static unsigned lastmicros;  
  30.   unsigned mic = micros();
  31.   if(digitalRead(3) == HIGH)
  32.   {
  33.     if(mic - lastmicros>3000)
  34.     {
  35.       ch = 0;
  36.     }
  37.     ppm2[ch] = mic - lastmicros;
  38.     lastmicros = mic;
  39.     ch = (++ch)%(PPMCH+1);
  40.   }
  41. }
  42. void setup(void)
  43. {
  44.   Serial.begin(57600);
  45.   pinMode(2, INPUT_PULLUP);
  46.   pinMode(3, INPUT_PULLUP);
  47.   pinMode(PPMOUT, OUTPUT);
  48.   digitalWrite(PPMOUT, HIGH);
  49.   attachInterrupt(0, PPM_ISR0, CHANGE);
  50.   attachInterrupt(1, PPM_ISR1, CHANGE);
  51. }
  52. void loop(void)
  53. {
  54.   unsigned int timecount = 0;
  55.   for(int i=0; i<=PPMCH; i++)
  56.   {
  57.    
  58.     if(i < 6)
  59.     {
  60.       if(ppm1[i] > 900 && ppm1[i] < 2100)
  61.         ppmout[i] = (ppm1[i] + ppmout[i])/2;
  62.     }
  63.    
  64.    
  65. else
  66.     {
  67.       if(ppm2[i] > 900 && ppm2[i] < 2100)
  68.         ppmout[i] = (ppm2[i] + ppmout[i])/2;
  69.     }
  70.    
  71.     ppmout[i] = 1900;
  72.   }
  73.   timecount = 0;
  74.   for(int i=1; i<=PPMCH; i++)
  75.   {
  76.    
  77.     digitalWrite(PPMOUT, LOW);
  78.     delayMicroseconds(300);
  79.     digitalWrite(PPMOUT, HIGH);
  80.     delayMicroseconds((ppmout[i]/3)*2);
  81.     timecount += ppmout[i];
  82.     //*/
  83.   }
  84.   
  85.   digitalWrite(PPMOUT, LOW);
  86.   delayMicroseconds(300);
  87.   digitalWrite(PPMOUT, HIGH);
  88.   delay((23000-timecount)/1000);

  89. }

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

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-29 05:57 , Processed in 0.074763 second(s), 16 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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