cancel
Showing results for 
Search instead for 
Did you mean: 

STM32 PWM output blocked ?

hanser
Associate II
Posted on January 27, 2011 at 15:17

STM32 PWM output blocked ?

#pwm-systick-dma-nvic #tim4 #pwm #pwm #stm32f #tim4 #stm32f103 #outputs
13 REPLIES 13
mehmet.karakaya
Associate III
Posted on May 17, 2011 at 14:23

hello ,where is the NVIC for TIM2 interrupt ?

second ) does the Zustand_pwm have a value < pwm period ?
bstumm
Associate II
Posted on May 17, 2011 at 14:23

Trying to implement stepper driver in my code and having trouble with the use of Systick.

   /* Disable SysTick Counter */

   //SysTick->CTRL &= 0xFFFFFFFE;

If I disable the systick counter then my RTOS hangs. If I do not disable and reset the systick then I cannot update the PWM frequency more than 1 time (after this further attempts are ignored until it eventually freezes up).

Can anyone point me in a direction that might work to update the frequency of PWM on the fly without the use of SysTick?

Posted on May 17, 2011 at 14:23

With the limited information provided, I'm going to guess that you have failed to clear one or more pending interrupts properly. The results of which will be a cyclic crap storm of interrupts as your routines just tail-chain endlessly, and NO foreground or low priority code will execute. Or that it has Hard Faulted into a terminal loop.

Get a debugger hooked up, and determine what's wrong.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bstumm
Associate II
Posted on May 17, 2011 at 14:23

I had meant to add that I am using AN2820 Stepper Motor Example code which uses DMA and TIM2 to vary the frequency of the PWM. I'm not sure how to eliminate the SysTick requirement for this example code. The only AN2820 code I've changed is commenting out the SysTick reset as shown in my original post.

As an alternative, perhaps I could set the TIM pin to PWM output but next question is how do you then vary the frequency of the pulses? I read somewhere that you have to disable the PWM then update the speed and re-enable but I cannot seem to find information on how to do that now.

Gawie
Associate II
Posted on May 17, 2011 at 14:23

Hi bap68

I am not sure why your PWM output is not working, but I have it working on my board using TIM3 ch1 - not controlling motors though. So the best I can do for you now is to show you how I did it - may be that would help. Below you will find some code extracts of how I did it.

Good luck.

Cheers,

Gawie

Init PWM output mode:

    // Init PWM output mode 

    // The external signal is connected to TIM3 CH1 pin REMAPPED to (PC.06), 

    GPIO_InitTypeDef GPIO_InitStructure;

    NVIC_InitTypeDef NVIC_InitStructure;

    TIM_OCInitTypeDef TIM_OCInitStruct;

    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

    

    TIM_DeInit(TIM3);

    TIM_OCStructInit(&TIM_OCInitStruct);

    TIM_OC1Init(TIM3, &TIM_OCInitStruct);

    

    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);

    // Full Remap TIM3 to PC6-9

    GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);   

    // TIM3 clock enable        

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);  

    // GPIOC clock enable       

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);      

    

    // Enable the TIM3 global Interrupt

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;

    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStructure);                              

    // Configure TIM3 channel 1 pin (PC.6) as output

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   

    GPIO_Init(GPIOC, &GPIO_InitStructure);                       

    // TIM3 disable counter 

    TIM_Cmd(TIM3, DISABLE);  

    // Disable the Update Interrupt Request                                    

    TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);                 

Start PWM output:

    // Start PWM Output

    TIM_OCInitTypeDef TIM_OCInitStructure;

    TIM_ARRPreloadConfig(TIM3, DISABLE);

    TIM3->ARR = 0;

    // Set Output Compare Chan1 on TIM3

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

    TIM_OCInitStructure.TIM_Pulse = 0;

    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);                            

    // Enable preload of period (ARR)

    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

    // Enable preload of pulse (CCR1)                   

    TIM_ARRPreloadConfig(TIM3, ENABLE);                                 

    // Start Period width that will be loaded on next Update Event

    TIM3->ARR = u16Period;    

    // Start Pulse width that will be loaded on next Update Event

    TIM3->CCR1 = u16Pulse;                                              

    // Clear any pending interrupts before when enable it

    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);

    // Enable the Update Interrupt Request                         

    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

    // Force an Update event to start sequence                          

    TIM_GenerateEvent(TIM3, TIM_EventSource_Update);                    

    // TIM enable counter

    TIM_Cmd(TIM3, ENABLE);                                               

Interrupt Service Routine:

    if(TIM_GetITStatus(TIM3, TIM_IT_Update) == SET)

    {

        // Clear interrupt bit

        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);                  

        // New Period width that will be loaded on next Update Event

        TIM3->ARR = u16Period;                                       

        // New Pulse width that will be loaded on next Update Event

        TIM3->CCR1 = u16Pulse;                                       

    }

You will need some condition to stop PWM output - will be unique to you

    // Disable the Update Interrupt Request 

    TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);               

hanser
Associate II
Posted on May 17, 2011 at 14:23

NVIC is normal:

//**Motoransteuerung Nadelabhebung**********************************************

    // Enable the TIM2 global Interrupt - Motoransteuerung Nadelabhebung

  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

I tried an easier Code too, but it didn't help.

Does anyone already tried to output a pwm on the TIM4 ?

I mean perhaps he can't do that ?

I looked too for Remap, but it didn't help...

Posted on May 17, 2011 at 14:23

Does anyone already tried to output a pwm on the TIM4 ?

I'm using TIM4 CH1 (PB.06) in PWM to generate frequency and control duty cycle.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hanser
Associate II
Posted on May 17, 2011 at 14:23

Thanks to all and specially to de_vos.gawie and clive1 for their helpful threads.

I'll try it.

Does everyone know how to close the topic ?

I didn't found any menu...

Sorry, I'm beginner on this forum.

Bap68

hanser
Associate II
Posted on May 17, 2011 at 14:23

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6Zl&d=%2Fa%2F0X0000000br3%2FZm2i2ENxYTiMXihUT1UmdupIXYLna8pCm.yoHuaE8Sg&asPdf=false