In the beginning of this year I’ve written a short tutorial how to read PWM signals from RC radio with Arduino. While it is can be useful when building own RC equipment, it does not help much when one has to deal with PPM (CPPM) signal. Let’s be honest, PPM is much more useful than PWM: all RC channels are sent over single wire. On one side, it simplifies electrical design. On the other, it makes software part more “complicated”, since there is a need to encode multiple PWM channels into single PPM line in transmitter, and then decode PPM signal into multiple PWMs in receiver. And there are very little “ready and working out of the box” solutions in Arduino world.
In this short article I will show how to generate PPM (CPPM) signal using solution prepared few years ago by David Hasko. Originally it was posted of Google Code. But Google Code is not closed and who knows for how long it still will be available. So, let’s not let the knowledge got lost.
/* | |
* PPM generator originally written by David Hasko | |
* on https://code.google.com/p/generate-ppm-signal/ | |
*/ | |
//////////////////////CONFIGURATION/////////////////////////////// | |
#define CHANNEL_NUMBER 12 //set the number of chanels | |
#define CHANNEL_DEFAULT_VALUE 1500 //set the default servo value | |
#define FRAME_LENGTH 22500 //set the PPM frame length in microseconds (1ms = 1000µs) | |
#define PULSE_LENGTH 300 //set the pulse length | |
#define onState 1 //set polarity of the pulses: 1 is positive, 0 is negative | |
#define sigPin 10 //set PPM signal output pin on the arduino | |
/*this array holds the servo values for the ppm signal | |
change theese values in your code (usually servo values move between 1000 and 2000)*/ | |
int ppm[CHANNEL_NUMBER]; | |
void setup(){ | |
//initiallize default ppm values | |
for(int i=0; i<CHANNEL_NUMBER; i++){ | |
ppm[i]= CHANNEL_DEFAULT_VALUE; | |
} | |
pinMode(sigPin, OUTPUT); | |
digitalWrite(sigPin, !onState); //set the PPM signal pin to the default state (off) | |
cli(); | |
TCCR1A = 0; // set entire TCCR1 register to 0 | |
TCCR1B = 0; | |
OCR1A = 100; // compare match register, change this | |
TCCR1B |= (1 << WGM12); // turn on CTC mode | |
TCCR1B |= (1 << CS11); // 8 prescaler: 0,5 microseconds at 16mhz | |
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt | |
sei(); | |
} | |
void loop(){ | |
/* | |
Here modify ppm array and set any channel to value between 1000 and 2000. | |
Timer running in the background will take care of the rest and automatically | |
generate PPM signal on output pin using values in ppm array | |
*/ | |
} | |
ISR(TIMER1_COMPA_vect){ //leave this alone | |
static boolean state = true; | |
TCNT1 = 0; | |
if (state) { //start pulse | |
digitalWrite(sigPin, onState); | |
OCR1A = PULSE_LENGTH * 2; | |
state = false; | |
} else{ //end pulse and calculate when to start the next pulse | |
static byte cur_chan_numb; | |
static unsigned int calc_rest; | |
digitalWrite(sigPin, !onState); | |
state = true; | |
if(cur_chan_numb >= CHANNEL_NUMBER){ | |
cur_chan_numb = 0; | |
calc_rest = calc_rest + PULSE_LENGTH;// | |
OCR1A = (FRAME_LENGTH - calc_rest) * 2; | |
calc_rest = 0; | |
} | |
else{ | |
OCR1A = (ppm[cur_chan_numb] - PULSE_LENGTH) * 2; | |
calc_rest = calc_rest + ppm[cur_chan_numb]; | |
cur_chan_numb++; | |
} | |
} | |
} |
Code is relatively simple, and almost all work is done inside ISR(TIMER1_COMPA_vect)
that is executed in the background by timer . Everything user has to do, is to put desired values to ppm
array inside loop
function. This code can generate both positive and negative signal. It can be easily ported to almost any project, as long as TIME1 is free to use.
Slightly more advanced example is available on GitHub.
Leave a Reply