Guest User

Arduino PPM RC TX

a guest
Jan 9th, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.95 KB | None | 0 0
  1. //this programm will put out a PPM signal
  2.  
  3. //////////////////////CONFIGURATION///////////////////////////////
  4. #define chanel_number 6  //set the number of chanels
  5. #define default_servo_value 1500  //set the default servo value
  6. #define PPM_FrLen 22500  //set the PPM frame length in microseconds (1ms = 1000µs)
  7. #define PPM_PulseLen 300  //set the pulse length
  8. #define onState 1  //set polarity of the pulses: 1 is positive, 0 is negative
  9. #define sigPin 10  //set PPM signal output pin on the arduino
  10. //////////////////////////////////////////////////////////////////
  11.  
  12.  
  13. /*this array holds the servo values for the ppm signal
  14.  change theese values in your code (usually servo values move between 1000 and 2000)*/
  15. int ppm[chanel_number];
  16.  
  17. void setup()
  18. {
  19.   //initiallize default ppm values
  20.   for(int i=0; i<chanel_number; i++)
  21.   {
  22.     ppm[i]= default_servo_value;
  23.   }
  24.  
  25.   pinMode(sigPin, OUTPUT);
  26.   digitalWrite(sigPin, !onState);  //set the PPM signal pin to the default state (off)
  27.  
  28.   cli();
  29.   TCCR1A = 0; // set entire TCCR1 register to 0
  30.   TCCR1B = 0;
  31.  
  32.   OCR1A = 100;  // compare match register, change this
  33.   TCCR1B |= (1 << WGM12);  // turn on CTC mode
  34.   TCCR1B |= (1 << CS11);  // 8 prescaler: 0,5 microseconds at 16mhz
  35.   TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
  36.   sei();
  37.  
  38.   pinMode(A0, INPUT);
  39.   pinMode(A1, INPUT);
  40.   pinMode(A2, INPUT);
  41.   pinMode(A3, INPUT);
  42.   pinMode(A4, INPUT);
  43.   pinMode(A5, INPUT);
  44.   //Serial.begin(9600);
  45. }
  46.  
  47. void loop()
  48. {
  49.   //THROTTLE
  50.   int throttle = analogRead(A1);
  51.   //Serial.println(throttle);
  52.   throttle = map(throttle, 90, 965, 1050, 1950);
  53.   ppm[0] = throttle;
  54.  
  55.   //YAW
  56.   int yaw = analogRead(A0);
  57.   //Serial.println(yaw);
  58.   yaw = map(yaw, 60, 945, 1050, 1950);
  59.   ppm[3] = yaw;
  60.  
  61.   //PICTH
  62.   int pitch = analogRead(A2);
  63.   //Serial.println(pitch);
  64.   pitch = map(pitch, 90, 946, 1050, 1950);
  65.   ppm[2] = pitch;
  66.  
  67.   //ROLL
  68.   int roll = analogRead(A3);
  69.   //Serial.println(roll);
  70.   roll = map(roll, 110, 987, 1050, 1950);
  71.   ppm[1 ] = roll;
  72.  
  73.   //CH1
  74.   int ch1 = digitalRead(A4);
  75.   ppm[4] = (ch1 == HIGH) ? 1850 : 1100;
  76.  
  77.   //CH2
  78.   int ch2 = digitalRead(A5);
  79.   ppm[5] = (ch2 == HIGH) ? 1850 : 1100;
  80.  
  81.   //delay(10);
  82. }
  83.  
  84. ISR(TIMER1_COMPA_vect)
  85. {  //leave this alone
  86.   static boolean state = true;
  87.  
  88.   TCNT1 = 0;
  89.  
  90.   if(state) {  //start pulse
  91.     digitalWrite(sigPin, onState);
  92.     OCR1A = PPM_PulseLen * 2;
  93.     state = false;
  94.   }
  95.   else{  //end pulse and calculate when to start the next pulse
  96.     static byte cur_chan_numb;
  97.     static unsigned int calc_rest;
  98.  
  99.     digitalWrite(sigPin, !onState);
  100.     state = true;
  101.  
  102.     if(cur_chan_numb >= chanel_number){
  103.       cur_chan_numb = 0;
  104.       calc_rest = calc_rest + PPM_PulseLen;//
  105.       OCR1A = (PPM_FrLen - calc_rest) * 2;
  106.       calc_rest = 0;
  107.     }
  108.     else{
  109.       OCR1A = (ppm[cur_chan_numb] - PPM_PulseLen) * 2;
  110.       calc_rest = calc_rest + ppm[cur_chan_numb];
  111.       cur_chan_numb++;
  112.     }    
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment