/************配置编码器模式**********、
void Encoder_Init_TIM2(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_AFIODeInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable , ENABLE); //??JTAG??,?PB3,PB4????IO???
GPIO_PinRemapConfig(GPIO_FullRemap_TIM2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //????
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING; //????
GPIO_Init(GPIOA, &GPIO_InitStructure); //?????????GPIOA
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //????
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 = ENCODER_TIM_PERIOD; //??????????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//??????:???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;////TIM????
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM2, &TIM_ICInitStructure);//??TIM_ICInitStruct???????????TIM
TIM_ClearFlag(TIM2, TIM_FLAG_Update);//??TIM??????
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //?????????TIM??
TIM_SetCounter(TIM2,0);
TIM2 -> CNT=0;
TIM_Cmd(TIM2, ENABLE); //??????TIMx??
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //¼ì2éÖ¸¶¨μÄTIMÖD¶Ï·¢éúóë·ñ:TIM ÖD¶ÏÔ′
{
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //Çå3yTIMxμÄÖD¶Ï′y′|àíλ:TIM ÖD¶ÏÔ′
}
、
/*************用于定时输出计数值***************/
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //????
TIM_TimeBaseStructure.TIM_Period = arr; //????????
TIM_TimeBaseStructure.TIM_Prescaler =psc; //????
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //??????:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM??????
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //??????????TIMx
TIM_ITConfig( TIM3,TIM_IT_Update | TIM_IT_Trigger, ENABLE );//??????TIM??
//??????
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3??
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //?????0?
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //????3?
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?????
NVIC_Init(&NVIC_InitStructure); //??NVIC_InitStruct???????????NVIC???
TIM_Cmd(TIM3, ENABLE); //??TIMx??
}
void TIM3_IRQHandler(void) //TIM3??
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //?????TIM??????:TIM ???
{
bmq_nmb=(short)TIM2 -> CNT;
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //??TIMx???????:TIM ???
}
}
最后在显示bmq_nmb的值
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
啥意思? 咋开中断
一周热门 更多>