最近做一个项目,主控芯片用STM32RBT6,要用到光栅尺,本来带一个控制器的,通过控制器的232可以读取光栅尺的数据,但这个控制器太大,设备中放不下,于是,考虑自己做一个,网上看到很多有用CPLD的方案,后来无意间发现stm32的定时器可以配置成编码器,甚喜
高兴之余,突然发现stm32的定时器是16位的,我的光栅尺的计数会超过65535,于是在21ic论坛上和几位高手请教,最终确定的方案
工作过程是配置TIM3为正交编码器模式,并定一个10ms的中断,每10ms读取一次计数值,10ms的前提是在10ms内计数器不溢出(这个思想要感谢21ic的lxyppc)
以下是部分代码:(这些代码修改于ST官方的例程,但我的工程用的是V3的固件库,他们的例程貌似是0.3的,所以有些地方有改动)
下面是初始化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);
}
以下为获取一次计数值,此算法来自lxyppc,可以规避超过16位的情况,具体细节见http://bbs.21ic.com/viewthread.php?tid=110623的讨论
s16 ENC_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;
}
以下为系统滴答的初始化和中断函数
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();
}
}
以上代码已通过测试,固件库版本为:V3.1.2
ST官方例程和中文说明文档:http://www.stmicroelectronics.co ... oder_AN%28CH%29.pdf
http://images.stmicroelectronics ... 32F10xxx_Encoder_AN(CH).zip
转载
|