/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : stm32f10x_encoder.c
* Author : IMS Systems Lab
* Date First Issued : 21/11/07
* Description : This file contains the software implementation for the
* encoder unit
********************************************************************************
* History:
* 21/11/07 v1.0
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_encoder.h"
#include "sys.h"
#include "usart.h"
#include "led.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define COUNTER_RESET (u16)0
#define ICx_FILTER (u8) 0 // 6<-> 670nsec
#define TIMx_PRE_EMPTION_PRIORITY 1
#define TIMx_SUB_PRIORITY 0
//#define MAX_COUNT ENCODER_TIM_PERIOD/2
/* Private functions ---------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
volatile s16 Right_hEncoder_Timer_Overflow; //编码器计数值溢出标志
volatile s16 Right_hEncoder_Timer_Overflow_High; //编码器计数值溢出标志高位
volatile s16 Right_hRot_Speed; //当前的速度
volatile s16 Right_hRot_Acceleration; //当前的加速度
s32 Right_CurrentCount = 0; //编码器当前的总计数值
s32 Right_CurrentCount_high = 0; //编码器当前的总计数值高位 计算公式,注意结果可能超过32位的表示范围Left_CurrentCount_high*S32_MAX+Left_CurrentCount
volatile s16 Left_hEncoder_Timer_Overflow;
volatile s16 Left_hEncoder_Timer_Overflow_High;
volatile s16 Left_hRot_Speed;
volatile s16 Left_hRot_Acceleration;
s32 Left_CurrentCount = 0;
s32 Left_CurrentCount_high = 0;
/*******************************************************************************
* Function Name : ENC_Right_Init
* Description : General Purpose Timer x set-up for encoder speed/position
* sensors
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ENC_Right_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; //A7接靠近红色的编码器输入一端
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_Right_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_Right_TIMER, &TIM_TimeBaseStructure);
//TIMx_SMCR 寄存器中的 SMS=011 = TIM_EncoderMode_TI12;
TIM_EncoderInterfaceConfig(ENCODER_Right_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //ICx 不反相输入捕获在上升沿
TIM_ICStructInit(&TIM_ICInitStructure); //TIM_Channel_1 TIM_ICPSC_DIV1 TIM_ICPolarity_Rising
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; //输入滤波 0110:采样频率fSAMPLING=fDTS/4, N=6
TIM_ICInit(ENCODER_Right_TIMER, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_Right_TIMER, TIM_FLAG_Update);
TIM_ITConfig(ENCODER_Right_TIMER, TIM_IT_Update, ENABLE);
//Reset counter
ENCODER_Right_TIMER->CNT = COUNTER_RESET;
TIM_Cmd(ENCODER_Right_TIMER, ENABLE);
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
Right_hEncoder_Timer_Overflow = 0;
Right_hEncoder_Timer_Overflow_High = 0;
Right_hRot_Speed = 0;
Right_ENC_Get_Electrical_Angle(); //第一次不要这个数据!!lastCount
}
/*******************************************************************************
* Function Name : ENC_Right_Init
* Description : General Purpose Timer x set-up for encoder speed/position
* sensors
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void ENC_Left_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
/* Encoder unit connected to TIM2 mode */
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* TIM2 clock source enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
/* Enable GPIOA, clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PA.00,01 as encoder input */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //A7接靠近红色的编码器输入一端
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable the TIM2 Update Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = TIMx_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = TIMx_SUB_PRIORITY+1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Timer configuration in Encoder mode */
TIM_DeInit(ENCODER_Left_TIMER);
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling
TIM_TimeBaseStructure.TIM_Period = (4*ENCODER_PPR)-1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_Left_TIMER, &TIM_TimeBaseStructure);
//TIMx_SMCR 寄存器中的 SMS=011 = TIM_EncoderMode_TI12;
TIM_EncoderInterfaceConfig(ENCODER_Left_TIMER, TIM_EncoderMode_TI12,
TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //ICx 不反相输入捕获在上升沿
TIM_ICStructInit(&TIM_ICInitStructure); //TIM_Channel_1 TIM_ICPSC_DIV1 TIM_ICPolarity_Rising
TIM_ICInitStructure.TIM_ICFilter = ICx_FILTER; //输入滤波 0110:采样频率fSAMPLING=fDTS/4, N=0
TIM_ICInit(ENCODER_Left_TIMER, &TIM_ICInitStructure);
// Clear all pending interrupts
TIM_ClearFlag(ENCODER_Left_TIMER, TIM_FLAG_Update);
TIM_ITConfig(ENCODER_Left_TIMER, TIM_IT_Update, ENABLE);
//Reset counter
ENCODER_Left_TIMER->CNT = COUNTER_RESET;
TIM_Cmd(ENCODER_Left_TIMER, ENABLE);
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
__nop();
Left_hEncoder_Timer_Overflow = 0;
Left_hEncoder_Timer_Overflow_High = 0;
Left_hRot_Speed = 0;
Left_ENC_Get_Electrical_Angle(); //第一次不要这个数据!!lastCount
}
/*******************************************************************************
* Function Name : Right_ENC_Get_Electrical_Angle
* Description : Returns the absolute electrical Rotor angle
* Input : None
* Output : None
* Return : dAngle
//第一次不要这个数据!!lastCount
*******************************************************************************/
s16 Right_ENC_Get_Electrical_Angle(void)
{
static u16 lastCount = 0;
u16 curCount ;
s16 dAngle ;
static s16 Last_Overflow = 0;
curCount = ENCODER_Right_TIMER->CNT;
dAngle = curCount - lastCount;
//ensure |Right_hEncoder_Timer_Overflow - Last_Overflow|<=1
if(Right_hEncoder_Timer_Overflow != Last_Overflow)
{
if ( (ENCODER_Right_TIMER->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down) //反转
{// encoder timer down-counting
dAngle -= ENCODER_TIM_PERIOD;
}
else
{//encoder timer up-counting
dAngle += ENCODER_TIM_PERIOD;
}
}
lastCount = curCount;
Last_Overflow = Right_hEncoder_Timer_Overflow;
return (s16)dAngle;
}
/*******************************************************************************
* Function Name : Left_ENC_Get_Electrical_Angle
* Description : Returns the absolute electrical Rotor angle
* Input : None
* Output : None
* Return : dAngle
//第一次不要这个数据!!lastCount
*******************************************************************************/
s16 Left_ENC_Get_Electrical_Angle(void)
{
static u16 lastCount = 0;
u16 curCount ;
s16 dAngle ;
static s16 Last_Overflow = 0;
curCount = ENCODER_Left_TIMER->CNT;
dAngle = curCount - lastCount;
//ensure |Left_hEncoder_Timer_Overflow - Last_Overflow|<=1
if(Left_hEncoder_Timer_Overflow != Last_Overflow)
{
if ( (ENCODER_Left_TIMER->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down) //反转
{// encoder timer down-counting
dAngle -= ENCODER_TIM_PERIOD;
}
else
{//encoder timer up-counting
dAngle += ENCODER_TIM_PERIOD;
}
}
lastCount = curCount;
Last_Overflow = Left_hEncoder_Timer_Overflow;
return (s16)dAngle;
}
void TIM2_IRQHandler(void)
{
/* Clear the interrupt pending flag */
TIM_ClearFlag(ENCODER_Left_TIMER, TIM_FLAG_Update);
if ((Left_hEncoder_Timer_Overflow != S16_MAX )&&(Left_hEncoder_Timer_Overflow != S16_MIN ))
{
if ( (ENCODER_Left_TIMER->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down) //反转
{// encoder timer down-counting
Left_hEncoder_Timer_Overflow--;
}
else
{//encoder timer up-counting
Left_hEncoder_Timer_Overflow++;
}
}
if(Left_hEncoder_Timer_Overflow == S16_MAX)
{
Left_hEncoder_Timer_Overflow = 0;
Left_hEncoder_Timer_Overflow_High++;
}
if(Left_hEncoder_Timer_Overflow == S16_MIN )
{
Left_hEncoder_Timer_Overflow = 0;
Left_hEncoder_Timer_Overflow_High--;
}
}
void TIM3_IRQHandler(void)
{
/* Clear the interrupt pending flag */
TIM_ClearFlag(ENCODER_Right_TIMER, TIM_FLAG_Update);
if ((Right_hEncoder_Timer_Overflow != S16_MAX )&&(Right_hEncoder_Timer_Overflow != S16_MIN ))
{
if ( (ENCODER_Right_TIMER->CR1 & TIM_CounterMode_Down) == TIM_CounterMode_Down) //反转
{// encoder timer down-counting
Right_hEncoder_Timer_Overflow--;
}
else
{//encoder timer up-counting
Right_hEncoder_Timer_Overflow++;
}
}
if(Right_hEncoder_Timer_Overflow == S16_MAX)
{
Right_hEncoder_Timer_Overflow = 0;
Right_hEncoder_Timer_Overflow_High++;
}
if(Right_hEncoder_Timer_Overflow == S16_MIN )
{
Right_hEncoder_Timer_Overflow = 0;
Right_hEncoder_Timer_Overflow_High--;
}
} |