如题,送给自己做警醒。
如果关于正交编码器,平台stm32f103推荐参考代码:
http://www.openedv.com/posts/list/44775.htm
注意:
如果stm32f2xx系列上面的网址的代码要做适当修改。
以下说重点:stm32f2xx系列,在调试中对GPIO的初始化,切记选择GPIO_Mode_AF模式,即端口复用。
随后,要做端口映射处理,即调用:GPIO_PinAFConfig()函数。
这2个方式不选择,timer是不会有反应的
本人是stm32f207,花了4天惨痛代价。
以下贴代码:
/* Private define ------------------------------------------------------------*/
#define ENCODER_TIMER TIM3 // Encoder unit connected to TIM3
#define ENCODER_PPR (u16)(1000) // number of pulses per revolution
#define SPEED_BUFFER_SIZE 8
#define COUNTER_RESET (u16)10
#define ICx_FILTER (u8) 6 // 6<-> 670nsec
#define TIMx_PRE_EMPTION_PRIORITY 0x0F
#define TIMx_SUB_PRIORITY 0x0F
*******************************************************************************
* Function Name : ENC_Init
* Description : General Purpose Timer x set-up for encoder speed/position
* sensors
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Encoder_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Encoder unit connected to TIM3, 4X mode */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM3 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/* Enable GPIOC, clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
//GPIO_Init
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PC.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = INTA_PIN | INTB_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //这个地方注意
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* Connect TIM3 pins to AF2 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3); //这个地方注意
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3); //这个地方注意
/* Enable the TIM3 Update Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = 0x0F;//(4*ENCODER_PPR)-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_TIMER, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(ENCODER_TIMER, TIM_EncoderMode_TI1,
TIM_ICPolarity_Falling, TIM_ICPolarity_Falling);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; //--?--
TIM_ICInit(ENCODER_TIMER, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
TIM_ITConfig(ENCODER_TIMER, TIM_IT_Update, ENABLE);
//Reset counter
//ENCODER_TIMER->CNT = COUNTER_RESET;
ENC_Clear_Speed_Buffer();
TIM_Cmd(ENCODER_TIMER, ENABLE);
}
/*******************************************************************************
* Function Name : TIM3_IRQHandler
* Description : This function handles TIMx Update interrupt request.
Encoder unit connected to TIM3
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void TIM3_IRQHandler(void)
{
/* Clear the interrupt pending flag */
TIM_ClearFlag(ENCODER_TIMER, TIM_FLAG_Update);
}