cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F TIM8 PWM

michaelbrinker9
Associate II
Posted on September 26, 2012 at 18:20

Hello!

I'm trying to get an STM32F407 with the LQFP176 Pinout to output in OC PWM1 mode.  My code is as follows:

  /* set up GPIO as timer output GPIOI 5, TIM 8 CH 1*/

    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI,ENABLE);

    GPIOInitS.GPIO_Pin = GPIO_PIN_174;

    GPIOInitS.GPIO_Mode = GPIO_Mode_AF;

    GPIOInitS.GPIO_Speed = GPIO_Speed_100MHz;

    GPIOInitS.GPIO_OType = GPIO_OType_PP;

    GPIOInitS.GPIO_PuPd = GPIO_PuPd_UP ;

    GPIO_Init(GPIOI, &GPIOInitS); 

    GPIO_PinAFConfig(GPIOI, GPIO_PinSource5,GPIO_AF_TIM8);

    

     /* set up timer hardware for PWM generation */

     RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM8, ENABLE);

    /* initialize timer to default values */

    TIM_TimeBaseStructInit(&TIMbaseS);

    TIMbaseS.TIM_Period = 0xffff;

    TIM_TimeBaseInit(TIM8, &TIMbaseS);

    /* configure output compare block */

    TIM_OCStructInit(&TIMOCinitS);

    TIMOCinitS.TIM_OCMode = TIM_OCMode_PWM1;

    TIMOCinitS.TIM_OutputState = TIM_OutputState_Enable;

    TIMOCinitS.TIM_Pulse = 3; 

    TIMOCinitS.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC1Init(TIM8, &TIMOCinitS);

    TIM_OC1PreloadConfig(TIM8,TIM_OCPreload_Enable);

    TIM_ARRPreloadConfig(TIM8, ENABLE);

    TIM_CtrlPWMOutputs(TIM8,ENABLE); /* TIM1/8 need extra command */

 

    TIM_Cmd(TIM8, ENABLE);

----------------------------

Unfortunately, I'm checking the output with an oscilloscope, and not getting anything out at all. What might I be doing wrong here?
5 REPLIES 5
Posted on September 26, 2012 at 18:32

Don't use RCC_APB2PeriphResetCmd() to enable the clock.

GPIO_PIN_174 is set to what?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
michaelbrinker9
Associate II
Posted on September 26, 2012 at 20:03

GPIO_PIN_174 is defined in gpio_cfg.h as:

GPIO(GPIO_PIN_174,       GPIOI, 5, OUTPUT,ACTIVE_HIGH,0)/* pin174. */

changed the line to :

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);

But still nothing.
Posted on September 26, 2012 at 20:19

GPIOInitS.GPIO_Pin = GPIO_Pin_5; // A bit vector (1 << 5)

This is for TIM1, the STM32F4-Discovery doesn't get me PI.5

// STM32 PWM 42 MHz (TIM1 CH1 PA.08) STM32F4 Discovery - sourcer32@gmail.com
#include ''stm32f4_discovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* GPIOA clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
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_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Connect TIM1 pins to AF */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
}
/**************************************************************************************/
void TIM1_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t Period;
Period = (SystemCoreClock / 42000000);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0; // Dump 1X clock into timer
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
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_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* 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..
Posted on September 26, 2012 at 20:39

For PI.5, a quick blind example

// STM32 PWM 21 MHz (TIM8 CH1 PI.05) STM32F4 - sourcer32@gmail.com
#include ''stm32f4xx.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* --------------------------- System Clocks Configuration -----------------*/
/* TIM8 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
/* GPIOI clock enable */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*-------------------------- GPIO Configuration ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
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_100MHz;
GPIO_Init(GPIOI, &GPIO_InitStructure);
/* Connect TIM8 pins to AF */
GPIO_PinAFConfig(GPIOI, GPIO_PinSource5, GPIO_AF_TIM8);
}
/**************************************************************************************/
void TIM8_Configuration(void)
{
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
uint16_t Period;
Period = (SystemCoreClock / 21000000);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0; // Dump 1X clock into timer
TIM_TimeBaseStructure.TIM_Period = Period - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
/* Enable TIM8 Preload register on ARR */
TIM_ARRPreloadConfig(TIM8, ENABLE);
/* TIM PWM1 Mode configuration */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Period / 2; // 50%
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
/* Output Compare PWM1 Mode configuration: Channel1 PI.05 */
TIM_OC1Init(TIM8, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);
/* TIM8 Main Output Enable */
TIM_CtrlPWMOutputs(TIM8, ENABLE);
/* TIM8 enable counter */
TIM_Cmd(TIM8, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM8_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..
michaelbrinker9
Associate II
Posted on September 28, 2012 at 20:55

It works now, thank you!

Problem was with the GPIO_PIN_174 line, I was defining it elsewhere as a regular GPIO pin, and then here as a timer Output Compare pin.