*****使用大V http://openedv.com/?90525 @北名茗水的配置。******本来想艾特他的,但是我不会hhhh
void TIM4_Init(u16 arr,u16 psc){ TIM4_Handler.Instance=TIM4; TIM4_Handler.Init.Prescaler=psc; //分频系数 TIM4_Handler.Init.CounterMode=TIM_COUNTERMODE_UP; //向上计数器 TIM4_Handler.Init.Period=arr; //自动装载值 TIM4_Handler.Init.ClockDivision=TIM_CLOCKDIVISION_DIV1;//时钟分频因子 TIM4_Encoder_Handler.EncoderMode=TIM_ENCODERMODE_TI12; TIM4_Encoder_Handler.IC1Filter=0; TIM4_Encoder_Handler.IC1Polarity=TIM_ICPOLARITY_RISING; TIM4_Encoder_Handler.IC1Prescaler=TIM_ICPSC_DIV1; TIM4_Encoder_Handler.IC1Selection=TIM_ICSELECTION_DIRECTTI; TIM4_Encoder_Handler.IC2Filter=0; TIM4_Encoder_Handler.IC2Polarity=TIM_ICPOLARITY_RISING; TIM4_Encoder_Handler.IC2Selection=TIM_ICSELECTION_DIRECTTI; TIM4_Encoder_Handler.IC2Prescaler=TIM_ICPSC_DIV1; HAL_TIM_Encoder_Init(&TIM4_Handler,&TIM4_Encoder_Handler); HAL_TIM_Encoder_Start(&TIM4_Handler,TIM_CHANNEL_ALL); HAL_TIM_Encoder_Start_IT(&TIM4_Handler,TIM_CHANNEL_ALL);//开启中断 TIM4->CNT=1000; __HAL_TIM_ENABLE_IT(&TIM4_Handler,TIM_IT_UPDATE); //使能更新中断 __HAL_TIM_ENABLE(&TIM4_Handler);}在这里我们配置编码器模式,开启时钟和溢出中断
//回调函数,定时器中断服务函数调用
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim==(&TIM4_Handler))
{
if ( (TIM4->CR1&0x0010) == 0 ) //向上计数溢出 {
Interupt_Num++ ;
}
else //向下计数溢出
{
Interupt_Num-- ;
}
}
} |