void Encoder_Init_TIM4(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);//?????2???
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//??PA PB????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //???? PA15
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????
GPIO_Init(GPIOB, &GPIO_InitStructure); //?????????GPIOA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; //???? PB3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //????
GPIO_Init(GPIOB, &GPIO_InitStructure); //?????????GPIOB
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // ????
TIM_TimeBaseStructure.TIM_Period = 65535; //??????????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//??????:???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//??????
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //??????2
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);//???????3
TIM_ICStructInit(&TIM_ICInitStructure); //?TIM_ICInitStruct ?????????????
TIM_ICInitStructure.TIM_ICFilter = 10; //???????
TIM_ICInit(TIM4, &TIM_ICInitStructure);//?? TIM_ICInitStruct ???????? TIMx
TIM_ClearFlag(TIM4, TIM_FLAG_Update);//??TIM??????
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);//???????
TIM_SetCounter(TIM4,0);
TIM_Cmd(TIM4, ENABLE); //?????2
}
int Read_Encoder4(void)
{
int encoder_num;
encoder_num = (int)((int16_t)(TIM4->CNT)); // ????????????
//TIM_SetCounter(TIM3, 0);
return encoder_num;
}
【STM32F1标准库】编码器代码
最新推荐文章于 2024-07-06 11:19:23 发布