2019-06-26 07:46 PM
Hi, I use STM32F103VBT.
Can anyone provide an example of how to use TIM1 Encoder interface mode(ch1, ch2) and PWM generation mode(ch3, ch4) at the same time?
PWM operation is stopped when encoder interface mode on.
TIM_EncoderInterfaceConfig(TIM1,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
my source code is below:
void TIMER1_Initialize(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
// TIM1 clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 200-1; // MAX 0xFFFF
TIM_TimeBaseStructure.TIM_Prescaler = 10-1; //1khz
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
//TIM_EncoderInterfaceConfig(TIM1,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter=0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel=TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter=0;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
// PWM1 Mode configuration: Channel3
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 20;
TIM_OC3Init(TIM1, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
// PWM1 Mode configuration: Channel4
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 1;
TIM_OC4Init(TIM1, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_CtrlPWMOutputs(TIM1, ENABLE);
TIM_ARRPreloadConfig(TIM1, ENABLE);
// TIM1 enable counter
TIM_Cmd(TIM1, ENABLE);
// Enable the CC Interrupt Request
TIM_ClearFlag(TIM1, TIM_FLAG_Update);
TIM_ITConfig(TIM1, TIM_IT_CC1, ENABLE);
}
2019-06-26 11:35 PM
> Can anyone provide an example of how to use TIM1 Encoder interface mode(ch1, ch2) and PWM generation mode(ch3, ch4) at the same time?
You can't.
There is only one counter in the timer. It either counts the encoder pulses, or it runs out of the APB clock, can't do both at the same time.
JW