How to genaraite ppm in stm32f1

Post here first, or if you can't find a relevant section!
Post Reply
aml@stm32
Posts: 3
Joined: Mon Mar 23, 2020 8:25 pm

How to genaraite ppm in stm32f1

Post by aml@stm32 »

can any one help me to write a simple code to genaraite ppm signal on stm32 i had only found codes for atmega and esp null for stm
i am geting trouble at porting it (dont have good idia about timer and counter)

ag123
Posts: 298
Joined: Thu Dec 19, 2019 5:30 am
Answers: 1

Re: How to genaraite ppm in stm32f1

Post by ag123 »


aml@stm32
Posts: 3
Joined: Mon Mar 23, 2020 8:25 pm

Re: How to genaraite ppm in stm32f1

Post by aml@stm32 »

thanks , but that project convert ppm to serial i need the invarce

esp code for ppm /// it works

Code: Select all

#define CPU_MHZ 80
#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 5 //set PPM signal output pin on the arduino
#define DEBUGPIN 4
volatile unsigned long next;
volatile unsigned int ppm_running=1;
int ppm[CHANNEL_NUMBER];


   void inline ppmISR(void){
  static boolean state = true;
  if (state) {  //start pulse
    digitalWrite(sigPin, onState);
    next = next + (PULSE_LENGTH * CPU_MHZ);
    state = false;
    alivecount++;
  }
  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) * CPU_MHZ);
      calc_rest = 0;
      digitalWrite(DEBUGPIN, !digitalRead(DEBUGPIN));
    }
    else{
      next = next + ((ppm[cur_chan_numb] - PULSE_LENGTH) * CPU_MHZ);
      calc_rest = calc_rest + ppm[cur_chan_numb];
      cur_chan_numb++;
    }
  }
  timer0_write(next);
}

void setup() {
   pinMode(sigPin,OUTPUT);
   digitalWrite(sigPin, !onState);
   noInterrupts();
   ppm_running=0;
   timer0_isr_init();
   timer0_attachInterrupt(ppmISR);
   next=ESP.getCycleCount()+1000;
   timer0_write(next);
   interrupts();
}void loop() {}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//ported code for stm32 ppm signal genaraits but incorrect :?: dont know why
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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 * CPU_MHZ);
    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) * CPU_MHZ);
      calc_rest = 0;

    }
    else{
      next = next + ((ppm[cur_chan_numb] - PULSE_LENGTH) * CPU_MHZ);
      calc_rest = calc_rest + ppm[cur_chan_numb];
      cur_chan_numb++;
    }
  }
  timer.setCount(next);
}



void setup() {
 pinMode(PC13,OUTPUT);
  pinMode(PB12,OUTPUT);
  timer.pause();
  timer.setChannel1Mode(TIMER_OUTPUT_COMPARE);
  timer.setCompare(TIMER_CH1, 1);
  timer.attachCompare1Interrupt(ppmISR);
  timer.refresh();
  next=timer.getCount()+1000;
  timer.setCount(next);
  ppm[0]=1000;
        ppm[1]=1444;
        ppm[2]=1444;
        ppm[3]=1444;
        ppm[4]=1000;
        ppm[5]=1000;
        ppm[6]=1000;
        ppm[7]=1000;

  timer.resume();

}
void loop() {
 digitalWrite(PC13,HIGH);
 delay(500);
 digitalWrite(PC13,LOW);
 delay(500);

}

aml@stm32
Posts: 3
Joined: Mon Mar 23, 2020 8:25 pm

Re: How to genaraite ppm in stm32f1

Post by aml@stm32 »

working ported code for stm32f1 (the blue pill) :mrgreen:

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);     
}

Post Reply

Return to “General discussion”