STM32學(xué)習(xí)筆記之正交編碼器接口
高興之余,突然發(fā)現(xiàn)stm32的定時(shí)器是16位的,我的光柵尺的計(jì)數(shù)會(huì)超過65535,于是在21ic論壇上和幾位高手請(qǐng)教,最終確定的方案
工作過程是配置TIM3為正交編碼器模式,并定一個(gè)10ms的中斷,每10ms讀取一次計(jì)數(shù)值,10ms的前提是在10ms內(nèi)計(jì)數(shù)器不溢出(這個(gè)思想要感謝21ic的lxyppc)
以下是部分代碼:(這些代碼修改于ST官方的例程,但我的工程用的是V3的固件庫,他們的例程貌似是0.3的,所以有些地方有改動(dòng))
下面是初始化TIM3為正交編碼器模式
void ENC_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 GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA.06,07 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 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 = ENCODER_TIM_PERIOD;
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_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
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
TIM2->CNT = COUNTER_RESET;
// ENC_Clear_Speed_Buffer();
TIM_Cmd(ENCODER_TIMER, ENABLE);
}
以下為獲取一次計(jì)數(shù)值,此算法來自lxyppc,可以規(guī)避超過16位的情況,具體細(xì)節(jié)見http://bbs.21ic.com/viewthread.php?tid=110623的討論
s16ENC_Get_Electrical_Angle(void)
{
static u16 lastCount = 0;
u16 curCount = ENCODER_TIMER->CNT;
s32 dAngle = curCount - lastCount;
if(dAngle >= MAX_COUNT){
dAngle -= ENCODER_TIM_PERIOD;
}else if(dAngle < -MAX_COUNT){
dAngle += ENCODER_TIM_PERIOD;
}
lastCount = curCount;
return (s16)dAngle;
}
以下為系統(tǒng)滴答的初始化和中斷函數(shù)
void TB_Init(void)
{
/* Setup SysTick Timer for 10 msec interrupts */
if (SysTick_Config(SystemFrequency / 100))
{
/* Capture error */
while (1);
}
}
void SysTick_Handler(void)
{
/*if (hTimebase_display_500us != 0)
{
hTimebase_display_500us --;
}
*/
if (hSpeedMeas_Timebase_500us !=0)
{
hSpeedMeas_Timebase_500us--;
}
else
{
hSpeedMeas_Timebase_500us = SPEED_SAMPLING_TIME;
CurrentCount += ENC_Get_Electrical_Angle();
//ENC_Calc_Average_Speed must be called ONLY every SPEED_MEAS_TIMEBASE ms
//ENC_Calc_Average_Speed();
}
}
以上代碼已通過測(cè)試,固件庫版本為:V3.1.2
ST官方例程和中文說明文檔:http://www.stmicroelectronics.com.cn/stonline/mcu/images/STM32F10xxx_Encoder_AN%28CH%29.pdf
http://images.stmicroelectronics.com.cn/stonline/mcu/images/STM32F10xxx_Encoder_AN(CH).zip
最后抱怨一句,st為啥不把定時(shí)器做成32位的呢,能增加1分錢成本嗎?
評(píng)論