2013-07-30 07:10 AM
I am trying to generate a pwm signal from timer1, Pin_E12. I found some helpful posts about stm32f4xx but i could not manage to apply it to stm32f107. Can anyone help me about this subject?
2013-07-30 08:16 AM
PE12 is the negated output, is that what you were looking for?
// STM32F107 PWM1 TIM1_CH3N(PE12) TIM1_CH3(PE13) - sourcer32@gmail.com
#include ''stm32f10x.h''
/**************************************************************************************/
void RCC_Configuration(void)
{
/* TIM1, GPIOE and AFIO clocks enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
}
/**************************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOE Configuration: Channel 3/3N as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOE, &GPIO_InitStructure);
/* TIM1 Full remapping pins (to PE pin bank) */
GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE);
}
/**************************************************************************************/
void TIM1_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 1MHz timebase from 72 MHz bus
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 10000 - 1; // 100 Hz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
/* Channel 3 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = 2500; // 25/75 for PE13, 75/25 for PE12
TIM_OC3Init(TIM1, &TIM_OCInitStructure); // Channel 3
/* TIM1 counter enable */
TIM_Cmd(TIM1, ENABLE);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
/**************************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
TIM1_Configuration();
while (1); /* Infinite loop */
}
2013-08-01 10:04 AM
Thank you very much for your help.
2013-12-17 06:21 AM
Thanks Clive, very helpful snippet.
Is it possible to create 3 PWM signals at the same time?Thanks in advance2013-12-17 06:36 AM
Yes, four are also possible, all the channels on the same timer must share the same frequency. I've posted a couple of 4-Channel servo examples for various STM32 parts.