Page 1 of 1

How to decode and encode PPM signal with Arduino?

Posted: Sat Feb 04, 2017 3:43 am
by Jay
Hello all,

I am looking for Arduino code that can decode PPM signal from a radio receiver, and encode PPM signal to a flight controller. Does anyone know where to find the code? Thank you.

Jay

Re: How to decode and encode PPM signal with Arduino?

Posted: Wed Apr 12, 2017 4:21 pm
by Cesco
output:
-------------------------------------

uint8_t servo[6] = { 128, 128, 128, 128, 128, 128 }; // servo pulse in usec

uint8_t ppmCounter = 0;
uint16_t ppmTotal = 0;

void initOutput()
{
TCCR1A = (1 << WGM11);
TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
ICR1 = 40000; // just initial value, will be constantly updated
OCR1A = 1000;
TIMSK1 |= (1 << TOIE1) | (1 << OCIE1A);
pinMode(PPMSUM_OUT_PIN, OUTPUT);
}

ISR(TIMER1_OVF_vect)
{
uint16_t ppmOut;
if (ppmCounter >= RC_CHANNELS)
{
ICR1 = 40000 - ppmTotal; // 20ms total frame
ppmCounter = 0;
ppmTotal = 0;
PPM_Port_LOW;
}
else
{
ppmOut = 1976 + 8*(uint16_t)servo[ppmCounter];
ppmTotal += ppmOut;
ICR1 = ppmOut;
PPM_Port_LOW;
ppmCounter++;
}
}

ISR(TIMER1_COMPA_vect)
{
PPM_Port_HIGH;
}

-------------------------------------
input:
-------------------------------------

volatile uint8_t rcValue[RC_CHANNELS];
uint8_t rcData[RC_CHANNELS];
volatile uint8_t gotir = 0;

volatile uint16_t last = 0;
volatile uint8_t chan = 0;

void rxInt()
{
uint16_t now,diff;
now = TCNT1;
diff = now - last;
last = now;
if (diff>6000) chan = 0; // Sync gap
else if (chan < RC_CHANNELS)
{
if (1800<diff && diff<4200)
{
if (diff <= 2000) rcValue[chan] = 0;
else if (diff >= 4000) rcValue[chan] = 255;
else rcValue[chan] = (diff - 1976) >> 3;
chan++;
}
else chan = 20;
}
else if (chan == RC_CHANNELS)
{
gotir = 1;
chan++;
}
}

void InitPPMin()
{
byte chan;
// Init PPM SUM RX
attachInterrupt(PPM_IN, rxInt, RISING);
TCCR1A = 0;
TCCR1B = (1 << CS11);
for (chan = 0; chan < RC_CHANNELS; chan++) rcValue[chan] = 128;
}

boolean getRC()
{
if (gotir > 0)
{
gotir = 0;
Green_LED_ON
tx_buf[0] = rcValue[0];
tx_buf[1] = rcValue[1];
tx_buf[2] = rcValue[2];
tx_buf[3] = rcValue[3];
tx_buf[4] = rcValue[4];
tx_buf[5] = rcValue[5];
return true;
}
else return false;
}