新手入门
- 积分
- 17
- 金钱
- 17
- 注册时间
- 2016-12-5
- 在线时间
- 1 小时
|
1金钱
void ENC_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Encoder unit connected to TIM3, 4X mode */
GPIO_InitTypeDef GPIO_InitStructure; //IO¿úÅäÖýá11ìå
NVIC_InitTypeDef NVIC_InitStructure; //ÖD¶ÏÅäÖýá11ìå
/* TIM3 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
////////////////////////////////////////////////////////////
// /* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //PA6¡¢7IO¿úÅäÖÃ
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO¿úÄ£ê½ÅäÖÃ
GPIO_Init(GPIOA, &GPIO_InitStructure);
/////////////////////////////////////////////////////////////
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9; //PA6¡¢7IO¿úÅäÖÃ
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //IO¿úÄ£ê½ÅäÖÃ
// GPIO_Init(GPIOC, &GPIO_InitStructure);
// GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
/* Enable the TIM3 Update Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3ÖD¶Ï
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =0; //ÇàÕ¼óÅÏ輶 //1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Ïìó|óÅÏ輶
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1ÄüÖD¶Ï
NVIC_Init(&NVIC_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_TIMER); //¸′λ TIM3
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
//¼ÆêyÂö3åμÄ·ÖÆμ
TIM_TimeBaseStructure.TIM_Prescaler = 0; //IM_PrescaleréèÖÃáËóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ¡£ËüμÄè¡Öμ±ØDëÔú0x0000oí0xFFFFÖ®¼ä¡£
TIM_TimeBaseStructure.TIM_Period =65535; //¼Æêyμ½òç3ö TIM_PeriodéèÖÃáËÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ¡£ËüμÄè¡Öμ±ØDëÔú0x0000oí0xFFFFÖ®¼ä¡£
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //TIM_ClockDivisionéèÖÃáËê±Öó·Ö¸î
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI12, //tim3 ±àÂëÆ÷Ä£ê½3 ±àÂëÆ÷Ëı¶Æμ
TIM_ICPolarity_BothEdge, TIM_ICPolarity_BothEdge);//??????
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter =6; //êäèë±è½ÏÂË2¨Æ÷ ¸Ã2Îêyè¡ÖμÔú0x0oí0xFÖ®¼ä
TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);// Clear all pending interrupts
TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE); //ê1ÄüTIM ÖD¶ÏÔ′
//Reset counter
TIM3->CNT = 0;
// ENC_Clear_Speed_Buffer();
TIM_Cmd(ENCODER_TIMER, ENABLE);
}
//void TIM3_IRQHandler(void)
//{
// if (TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
// {
// TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
//
// }
//}
串口打印 TIM3->CNT 只有0和1??不是应该累加么
|
|