最近做一个项目,主控芯片用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的评论
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;
}
以下为体系滴答的初始化和中止函数
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.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为啥不把定时器做成32位的呢,能添加1分钱本钱吗?