AnsweredAssumed Answered

Multiple Frequency PWM sequencer

Question asked by aston.bob on Jan 18, 2016
Latest reply on Jan 19, 2016 by aston.bob
Hello,
I'd like to create a sequencer on my STM32F100RB (@8MHz) to control 8 independants PWM channels. I configured a timer to generate a 10us interrupt wich call the update function.

//timer 10µs   
void Init_TIMER(void)
    {
        TIM2->PSC = 7;                  // Set prescaler to 24 000 (PSC + 1)
        TIM2->ARR = 10;             // Auto reload value 1000
        TIM2->DIER = TIM_DIER_UIE;  // Enable update interrupt (timer level)
        TIM2->CR1 = TIM_CR1_CEN;    // Enable timer
 
        NVIC_EnableIRQ(TIM2_IRQn);  // Enable interrupt from TIM2 (NVIC level)     
    }

Here my interrupt function :

void TIM2_IRQHandler(void)
    {
        if(TIM2->SR & TIM_SR_UIF)           // if UIF flag is set
            {
                 
            if (currentPeriod[0] > 0){
                currentTick[0]++;
            if (currentTick[0] >= currentPeriod[0]){
                    move(0,STEP_0,DIR_0);
                    currentTick[0]=0;
                }
            }  
                 
            if (currentPeriod[1] > 0){
                currentTick[1]++;
            if (currentTick[1] >= currentPeriod[1]){
                    move(1,STEP_1,DIR_1);
                    currentTick[1]=0;
                }
            }  
 
[6 more blocks]
         
            TIM2->SR &= ~TIM_SR_UIF;            // clear UIF flag
             
            }
}

and my move function :

void move (int mot_id, uint16_t pin, uint16_t direction_pin) {
 
    //Switch directions if end has been reached
  if (currentPosition[pin] >= MAX_POSITION) {
    currentState[direction_pin] = 1;
        GPIO_SetBits(GPIOC,direction_pin);
  }
  else if (currentPosition[pin] <= 0) {
    currentState[direction_pin] = 0;
    GPIO_ResetBits(GPIOC,direction_pin);
  }
 
  //Update currentPosition
  if (currentState[direction_pin] == 1){
    currentPosition[pin]--;
  }
  else {
    currentPosition[pin]++;
  }
 
  //Pulse the control pin
    if (currentState[pin]== 0){
     GPIO_ResetBits(GPIOC,pin);
    }
 else{
     GPIO_SetBits(GPIOC,pin);
 }
      
  currentState[pin] = ~currentState[pin];
         
}

When I try this code with one single output I generate a signal with the right frequency but if i had more output(blocks) the frequency decrease.
Have you ever experienced such a problem ?

Outcomes