I building custom rc transmitter. I would want to add support for standart rc transmitter modules. For that I need to generate 8 channel PPM signal.
I tried porting this code from Arduino IDE to PlatformIO but with no luck.
Code: Select all
#include <Arduino.h>
#define CPU_MHZ 72
#define CHANNEL_NUMBER 8 //set the number of chanels
#define CHANNEL_DEFAULT_VALUE 1100 //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 0 //set polarity of the pulses: 1 is positive, 0 is negative
#define sigPin PB12 //set PPM signal output pin on the arduino
volatile unsigned long next;
volatile unsigned int ppm_running=1;
int ppm[CHANNEL_NUMBER];
int alivecount ;
HardwareTimer timer(2);
void inline ppmISR(void){
static boolean state = true;
if (state) { //start pulse
digitalWrite(sigPin, onState);
next = next + PULSE_LENGTH ;
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;
next = next + (FRAME_LENGTH - calc_rest) ;
calc_rest = 0;
}
else{
next = next + (ppm[cur_chan_numb] - PULSE_LENGTH) ;
calc_rest = calc_rest + ppm[cur_chan_numb];
cur_chan_numb++;
}
}
timer.setCompare(TIMER_CH1, next);
}
void setup() {
pinMode(PC13,OUTPUT);
pinMode(PB13,OUTPUT);
pinMode(PB12,OUTPUT);
timer.pause() ;
timer.setPrescaleFactor(72);
timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
next=timer.getCount()+10;
timer.setCompare(TIMER_CH1,next);
timer.attachCompare1Interrupt(ppmISR);
timer.refresh();
ppm[0]=1000;
ppm[1]=1444;
ppm[2]=1444;
ppm[3]=1444;
ppm[4]=1444;
ppm[5]=1000;
ppm[6]=1000;
ppm[7]=1000;
timer.resume();
}
void loop() {
digitalWrite(PC13,LOW);
delay(500);
digitalWrite(PC13,HIGH);
delay(600);
}
Can somebody help me?