程序如下:
- #define PPMCH 8
- #define LED 13
- #define PPMOUT 8
- unsigned int ppm1[PPMCH+1] = {
- 0, 1500, 1500, 1500, 1900, 1500, 1500, 1500, 1500};
- unsigned int ppm2[PPMCH+1] = {
- 0, 1500, 1500, 1500, 1900, 1500, 1500, 1500, 1500};
- unsigned int ppmout[PPMCH+1] = {
- 0, 1500, 1500, 1500, 1900, 1500, 1500, 1500, 1500};
- void PPM_ISR0(void)
- {
- volatile static char ch;
- static unsigned lastmicros;
- unsigned mic = micros();
- if(digitalRead(2) == HIGH)
- {
- if(mic - lastmicros>3000)
- {
- ch = 0;
- }
- ppm1[ch] = mic - lastmicros;
- lastmicros = mic;
- ch = (++ch)%(PPMCH+1);
- }
- }
- void PPM_ISR1(void)
- {
- volatile static char ch;
- static unsigned lastmicros;
- unsigned mic = micros();
- if(digitalRead(3) == HIGH)
- {
- if(mic - lastmicros>3000)
- {
- ch = 0;
- }
- ppm2[ch] = mic - lastmicros;
- lastmicros = mic;
- ch = (++ch)%(PPMCH+1);
- }
- }
- void setup(void)
- {
- Serial.begin(57600);
- pinMode(2, INPUT_PULLUP);
- pinMode(3, INPUT_PULLUP);
- pinMode(PPMOUT, OUTPUT);
- digitalWrite(PPMOUT, HIGH);
- attachInterrupt(0, PPM_ISR0, CHANGE);
- attachInterrupt(1, PPM_ISR1, CHANGE);
- }
- void loop(void)
- {
- unsigned int timecount = 0;
- for(int i=0; i<=PPMCH; i++)
- {
-
- if(i < 6)
- {
- if(ppm1[i] > 900 && ppm1[i] < 2100)
- ppmout[i] = (ppm1[i] + ppmout[i])/2;
- }
-
-
- else
- {
- if(ppm2[i] > 900 && ppm2[i] < 2100)
- ppmout[i] = (ppm2[i] + ppmout[i])/2;
- }
-
- ppmout[i] = 1900;
- }
- timecount = 0;
- for(int i=1; i<=PPMCH; i++)
- {
-
- digitalWrite(PPMOUT, LOW);
- delayMicroseconds(300);
- digitalWrite(PPMOUT, HIGH);
- delayMicroseconds((ppmout[i]/3)*2);
- timecount += ppmout[i];
- //*/
- }
-
- digitalWrite(PPMOUT, LOW);
- delayMicroseconds(300);
- digitalWrite(PPMOUT, HIGH);
- delay((23000-timecount)/1000);
- }
复制代码 |