Generating PPM signal using bluepill and hardware timer library

Post Reply
Artur
Posts: 2
Joined: Sat Jun 19, 2021 9:10 am

Generating PPM signal using bluepill and hardware timer library

Post by Artur »

Hey,
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);     
}
viewtopic.php?p=1802#p1802 - source

Can somebody help me?
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Generating PPM signal using bluepill and hardware timer library

Post by ag123 »

there are some faqs which may be relevant
viewtopic.php?f=2&t=3
viewtopic.php?f=2&t=301

i read a little into RC stuff here
https://www.dialog-semiconductor.com/si ... lation.pdf

for RC stuff, if one have flexibility with both the transmitter and receiver, using things like nrf24l01 or cc2500 radios are probably easier.
those can transmit and receive data as bytes rather than having to convert to pwm, ppm etc.
Post Reply

Return to “STM32F1 based boards”