cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F100xx TIM4 PWM woes

eaverill
Associate II
Posted on January 04, 2013 at 16:58

Ok, I'm confused.. according to the docs I should be able to program TIM4 as a PWM with unremaped output on PB9.  However, the Timer API call TIM_CtrlPWMOutputs() has an assert which indicates that TIM4 isn't supported for PWM output.

Does anyone know if the API is incorrect, or if I'm stuck with TIM1/TIM8/TIM15/TIM16/TIM17 as the only permissible PWM generators?

All info appreciated!

Ed Averill

#timer #pwm #tim4
3 REPLIES 3
Posted on January 04, 2013 at 17:16

It's more of a matter than TIM4 isn't special, and doesn't need to use TIM_CtrlPWMOutputs() like the advanced timers (1 & 8 ?).

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on January 04, 2013 at 17:21

// STM32 VLDiscovery 1HZ External clock demo TIM4 CH4 PB9 PWM - sourcer32@gmail.com
#include ''stm32F10x.h''
#include ''STM32vldiscovery.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
// clock for GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// clock for TIM4
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// PB.09 TIM4_CH4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/**************************************************************************************/
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
// Set up TIM4_CH4 to be a 1 Hz time base (1PPS)
TIM_TimeBaseStructure.TIM_Prescaler = 24000 - 1; // 24 MHz / 24000 = 1 KHz
TIM_TimeBaseStructure.TIM_Period = 1000 - 1; // 1 KHz / 1000 = 1 Hz
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
// Channel 4 configuration = PB.09 TIM4_CH4
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = (TIM_TimeBaseStructure.TIM_Period + 1) / 2; // 50% Duty
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
// turning on TIM4 and PWM outputs
TIM_Cmd(TIM4, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM4_Configuration(); // 1Hz Output
while(1); 
}
/**************************************************************************************/

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
eaverill
Associate II
Posted on January 04, 2013 at 17:35

@all: Thanks!  We had been using TIM1 (which needs the PWM output call) but the hardware guys stole PB0 for something ELSE, so I had to scramble to fix Someone Elses Code(tm) and am not that familiar with the STM32 family.

Going to run off and use the posted example code to see if I can get this thing working (or if we have other hardware issues, iugh).  Thanks Clive, you rock!  Have a great weekend!

Ed Averill