Home > other >  Could you tell me why the stm32 Tim encoder mode CNT readings have been zero
Could you tell me why the stm32 Tim encoder mode CNT readings have been zero

Time:09-30

The code
 
# include "test. H"
/* TIM2 initialized to encoder interface */
Void Encoder_Init_TIM2 (void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2, ENABLE);//can make the clock timer 4
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOA, ENABLE);//can make PA port clock

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;//port configuration
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;//floated input
GPIO_Init (GPIOA, & amp; GPIO_InitStructure);//according to set parameters initialization GPIOA

TIM_TimeBaseStructInit (& amp; TIM_TimeBaseStructure);
TIM_TimeBaseStructure. TIM_Prescaler=0 x0;//the preassigned frequency,
TIM_TimeBaseStructure. TIM_Period=65535;//set the counter automatically reinstall value
TIM_TimeBaseStructure. TIM_ClockDivision=TIM_CKD_DIV1;//select the clock frequency division: regardless of frequency
TIM_TimeBaseStructure. TIM_CounterMode=TIM_CounterMode_Up;//TIM counting up
TIM_TimeBaseInit (TIM2, & amp; TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig (TIM2, TIM_EncoderMode_TI12 TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//use the encoder model 3
TIM_ICStructInit (& amp; TIM_ICInitStructure);
TIM_ICInitStructure. TIM_ICFilter=10;
TIM_ICInit (TIM2, & amp; TIM_ICInitStructure);
TIM_ClearFlag (TIM2, TIM_FLAG_Update);//remove TIM updated flags
TIM_ITConfig (TIM2, TIM_IT_Update, ENABLE);
//Reset counter
TIM_SetCounter (TIM2, 0);
TIM_Cmd (TIM2, ENABLE);
}

/* TIM4 initialized to encoder interface */
Void Encoder_Init_TIM4 (void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE);//can make the clock timer 4
RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, ENABLE);//can make PB port clock

GPIO_InitStructure. GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7;//port configuration
GPIO_InitStructure. GPIO_Mode=GPIO_Mode_IN_FLOATING;//floated input
GPIO_Init (GPIOB, & amp; GPIO_InitStructure);//according to set parameters initialization GPIOB

TIM_TimeBaseStructInit (& amp; TIM_TimeBaseStructure);
TIM_TimeBaseStructure. TIM_Prescaler=0 x0;//the preassigned frequency,
TIM_TimeBaseStructure. TIM_Period=65535;//set the counter automatically reinstall value
TIM_TimeBaseStructure. TIM_ClockDivision=TIM_CKD_DIV1;//select the clock frequency division: regardless of frequency
TIM_TimeBaseStructure. TIM_CounterMode=TIM_CounterMode_Up;//TIM counting up
TIM_TimeBaseInit (TIM4, & amp; TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig (TIM4, TIM_EncoderMode_TI12 TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//use the encoder model 3
TIM_ICStructInit (& amp; TIM_ICInitStructure);
TIM_ICInitStructure. TIM_ICFilter=10;
TIM_ICInit (TIM4, & amp; TIM_ICInitStructure);
TIM_ClearFlag (TIM4, TIM_FLAG_Update);//remove TIM updated flags
TIM_ITConfig (TIM4, TIM_IT_Update, ENABLE);
//Reset counter
TIM_SetCounter (TIM4, 0);
TIM_Cmd (TIM4, ENABLE);
}

Timer/* unit time counting encoder input output speed value */
Int Read_Encoder (u8 TIMX)
{
Int Encoder_TIM;
The switch (TIMX)
{
Case 2: Encoder_TIM=(short) TIM2 - & gt; CNT. TIM2 - & gt; CNT=0; break;
Case 3: Encoder_TIM=(short) TIM3 - & gt; CNT. TIM3 - & gt; CNT=0; break;
Case 4: Encoder_TIM=(short) TIM4 - & gt; CNT. TIM4 - & gt; CNT=0; break;
Default: Encoder_TIM=0;
}
Return Encoder_TIM;
}

Void TIM4_IRQHandler (void)
{
If (TIM4 - & gt; SR& Overflow interrupt 0 x0001)//
{
}
TIM4 - & gt; SR&=~ (1 & lt; <0);//remove the interrupt flag bit
}

Void TIM2_IRQHandler (void)
{
If (TIM2 - & gt; SR& Overflow interrupt 0 x0001)//
{
}
TIM2 - & gt; SR&=~ (1 & lt; <0);//remove the interrupt flag bit
}
  • Related