Question
STM32F3 Timer3 and Timer4 as PWM
Posted on September 22, 2013 at 22:46
Hi,
i use a STM32f3discovery evaluation board and trying to generate 6 PWM signals. For this purpose i have choosen Timers 3 and 4. As base project i use a Demo project. (The project demonstrates a sensors functionality) Initialisation code for timers is following: void Timer_Config(void){ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA , ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Timer4 GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_10); GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_10); GPIO_PinAFConfig(GPIOA, GPIO_PinSource13, GPIO_AF_10); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_TimeBaseInitStructure.TIM_Prescaler = 71; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 999; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStructure); //PWM Config on Timer4 CH3 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_OC3Init(TIM4, &TIM_OCInitStructure); //PWM Config on Timer4 CH2 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_OC2Init(TIM4, &TIM_OCInitStructure); //PWM Config on Timer4 CH1 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(TIM4, &TIM_OCInitStructure); TIM_Cmd(TIM4, ENABLE); //Timer3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_6 | GPIO_Pin_7 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_2); //Timer3 Config TIM_TimeBaseInitStructure.TIM_Prescaler = 71; TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStructure.TIM_Period = 999; TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV4; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure); //PWM Config on Timer3 CH3 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_Low; TIM_OC3Init(TIM3, &TIM_OCInitStructure); //PWM Config on Timer3 CH2 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_Low; TIM_OC2Init(TIM3, &TIM_OCInitStructure); //PWM Config on Timer3 CH1 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_Low; TIM_OC1Init(TIM3, &TIM_OCInitStructure); TIM_Cmd(TIM3, ENABLE); } But if I call this funktion the board stops working. Pin_to_periphery allocation description was taken from DM00058181.pdf dokument form ST site. Could someone help me make it work? P.S. I use a USART1 too, it uses pins PA9 and PA10. #pwm #stm32f3 #timers