cancel
Showing results for 
Search instead for 
Did you mean: 

STM32f207 PWM output

eugenia
Associate II
Posted on July 15, 2016 at 16:44

Hi, 

I am trying to generate a PWM output on port PE9. I have set up the Timer configuration, the GPIO configuration and the interrupts. Somehow when I add the code to set up the PWM mode, then I completely lose my signal. Without the code for configuring the PWM mode, I get a PWM signal but with a very small duty cycle which I would like to regulate. I have no idea what is going on.

static void ledamdrv_TIMInit(void)

{

    TIM_TimeBaseInitTypeDef TIM_Base; // structure with timer base settings

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    uint16_t PrescalerValue;

    GPIO_InitTypeDef GPIO_InitStructure;

    // Enable clocks

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

    /* Compute the prescaler value */

    PrescalerValue = (uint16_t) (ROUNDDIV(SystemAPB2TimerClock, LEDAMDRV_TIMER_FREQ_HZ) - 1);

    // Setup Timer, PWM generation with CL modulation

    TIM_DeInit( TIM1 );

    TIM_TimeBaseStructInit(&TIM_Base);

    TIM_Base.TIM_Prescaler = 0;

    TIM_Base.TIM_ClockDivision = TIM_CKD_DIV1;

    TIM_Base.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_Base.TIM_Period = LEDAMDRV_TIMER_FREQ_HZ / LEDMADDRV_ISR_FREQ_HZ;

    TIM_TimeBaseInit( TIM1, &TIM_Base );

/* Prescaler configuration */

     TIM_PrescalerConfig(TIM1, PrescalerValue, TIM_PSCReloadMode_Immediate);

    /* Enable TIM1 Preload register on ARR */

     // TIM_ARRPreloadConfig(TIM1, ENABLE);

    // /* PWM1 Mode configuration: Channel1 */

        // TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;

        // TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Disable;  // Don't need pin output, just interrup

       //  TIM_OCInitStructure.TIM_Pulse = ( (LEDAMDRV_TIMER_FREQ_HZ / LEDMADDRV_ISR_FREQ_HZ) / 4);//50% duty cycle

       //  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

       //  TIM_OC1Init(TIM1, &TIM_OCInitStructure);

         TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);

    /* TIM1 enable counter */

    TIM_Cmd(TIM1, ENABLE);

   

    /* TIM Interrupt enable */

    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);

    TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE); //added this 14-7-2016

   

    

  

}

As soon as I uncomment the commented code above, I completely lose the PWM signal. Any ideas on how to solve this will be very appreciated. 

Thanks, 

Eugenia

#pwm
3 REPLIES 3
Posted on July 15, 2016 at 17:24

TIM1 is an Advanced Timer, and has a significantly more complex initialization structure which you are not filling out. The PWM Output must also be enabled.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
eugenia
Associate II
Posted on July 18, 2016 at 09:33

Hi Clive,

I decided to use the mode of  timer1 as a GPIO output instead of GPIO_AF because the Alternate Function was not working. Using mode GPIO output I now have a software implementation of PWM. I followed this example from a previous solution posted on this forum... Now when I try to configure the PWM output then it stops working completely.. I don't know what else to look at..

Posted on July 18, 2016 at 20:26

Ok, here are some basic posting tips.

If you want to refer to other threads please cite them directly, not indirectly posting to them. [DEAD LINK /public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/STM32f207%20remapping%20pins%20problem&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx&currentviews=1478]Here Don't reopen old threads, even if you think that your problem is similar, or you want immediate attention to your issue. I will get to things in an order, manner, and rate that suits me. Post complete code examples, that have defines, configurations and will at least compile. Stuff that lack basic completeness is going to be ignored or fall to the bottom of the heap.

// STM32 PWM (TIM1 CH1 PE.09) STM32F2 - sourcer32@gmail.com
#include ''stm32f2xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOE clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_TIM1);
}
/**************************************************************************************/
void TIM1_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t Period;
Period = 1000000 / 20000; // 20 KHz for 1MHz prescaled
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = (SystemCoreClock / 1000000) - 1; // Get clock to 1 MHz on STM32F2/F4
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Enable TIM1 Preload register on ARR */
TIM_ARRPreloadConfig(TIM1, ENABLE);
/* TIM PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
/* Output Compare PWM1 Mode configuration: Channel1 PA.08 */
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
/* TIM1 enable counter */
TIM_Cmd(TIM1, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM1_Configuration();
while(1); // Don't want to exit
}
/**************************************************************************************/
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**************************************************************************************/

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..