2024-01-20 13:19:09 +08:00
|
|
|
#ifdef __cplusplus
|
2024-01-24 20:44:32 +08:00
|
|
|
extern "C"
|
|
|
|
{
|
|
|
|
#endif
|
2024-01-20 13:19:09 +08:00
|
|
|
|
|
|
|
#include "encoder.h"
|
|
|
|
#include "nvic.h"
|
|
|
|
#include "print.h"
|
2024-01-24 22:08:32 +08:00
|
|
|
|
2024-01-24 20:44:32 +08:00
|
|
|
#define ENCODER_TIM_PERIOD (u16)(60000) // number of pulses per revolution
|
2024-01-20 13:19:09 +08:00
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
void Encoder_InitGPIO(TIM_TypeDef *TIMx, unsigned char GPIO_AF) // Initialize encoder mode, input parameter TIM1 TIM2 TIM3
|
|
|
|
{
|
|
|
|
TIM_ICInitTypeDef TIM_ICInitStructure;
|
|
|
|
GPIO_InitTypeDef GPIO_InitStructure;
|
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
|
if (TIMx == TIM2)
|
2024-01-24 20:44:32 +08:00
|
|
|
{
|
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* enable clock */
|
|
|
|
if (GPIO_AF == 0)
|
|
|
|
{
|
|
|
|
// TIM2 CHI1 CHI2---PA0 PA1;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
2024-01-20 13:19:09 +08:00
|
|
|
}
|
2024-01-24 22:08:32 +08:00
|
|
|
else if (GPIO_AF == 1)
|
2024-01-24 20:44:32 +08:00
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
//---------------------------------------------------------------TIM2 CHI1 CHI2---PA15 PB3;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
|
|
|
|
GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);
|
|
|
|
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); // This is very important!
|
|
|
|
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
2024-01-20 13:19:09 +08:00
|
|
|
}
|
2024-01-24 22:08:32 +08:00
|
|
|
TIM2_NVIC_Configuration(); // enable interrupt
|
|
|
|
}
|
2024-01-20 13:19:09 +08:00
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
else if (TIMx == TIM3)
|
|
|
|
{
|
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA, ENABLE); /* enable clock */
|
|
|
|
if (GPIO_AF == 0)
|
2024-01-24 20:44:32 +08:00
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
// TIM3 CHI1 CHI2---PA6 PA7;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
2024-01-24 20:44:32 +08:00
|
|
|
}
|
2024-01-24 22:08:32 +08:00
|
|
|
else if (GPIO_AF == 1)
|
2024-01-24 20:44:32 +08:00
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
// TIM3 CHI1 CHI2---PB4 PB5;
|
|
|
|
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
2024-01-20 13:19:09 +08:00
|
|
|
}
|
2024-01-24 22:08:32 +08:00
|
|
|
TIM3_NVIC_Configuration(); // enable interrupt
|
2024-01-24 20:44:32 +08:00
|
|
|
}
|
2024-01-20 13:19:09 +08:00
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
else if (TIMx == TIM4)
|
2024-01-24 20:44:32 +08:00
|
|
|
{
|
2024-01-24 22:08:32 +08:00
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); /* enable clock */
|
|
|
|
if (GPIO_AF == 0)
|
|
|
|
{
|
|
|
|
// TIM4 CHI1 CHI2---PB6 PB7;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOB, &GPIO_InitStructure);
|
|
|
|
}
|
|
|
|
else if (GPIO_AF == 1)
|
|
|
|
{
|
|
|
|
// TIM4 CHI1 CHI2---PD12 PD13;
|
|
|
|
GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOD, &GPIO_InitStructure);
|
|
|
|
}
|
|
|
|
TIM4_NVIC_Configuration();
|
2024-01-24 20:44:32 +08:00
|
|
|
}
|
2024-01-24 22:08:32 +08:00
|
|
|
else if (TIMx == TIM5)
|
2024-01-24 20:44:32 +08:00
|
|
|
{
|
2024-01-20 13:19:09 +08:00
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
|
2024-01-20 13:19:09 +08:00
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
if (GPIO_AF == 0)
|
|
|
|
{
|
|
|
|
// TIM5 CHI1 CHI2---PA0 PA1;
|
|
|
|
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
|
|
|
GPIO_StructInit(&GPIO_InitStructure);
|
|
|
|
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
|
|
|
|
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
|
|
|
GPIO_Init(GPIOA, &GPIO_InitStructure);
|
|
|
|
}
|
|
|
|
TIM5_NVIC_Configuration();
|
2024-01-24 20:44:32 +08:00
|
|
|
}
|
2024-01-20 13:19:09 +08:00
|
|
|
|
2024-01-24 22:08:32 +08:00
|
|
|
// 设定TIM_CKD_DIV1和TIM_ICFilter主要起到的是滤除高频信号噪声的作用
|
|
|
|
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
|
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; /* prescler : 72M/72 */
|
|
|
|
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD;
|
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; /* count upwards */
|
|
|
|
TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);
|
|
|
|
|
|
|
|
TIM_EncoderInterfaceConfig(TIMx, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
|
|
|
|
TIM_ICStructInit(&TIM_ICInitStructure);
|
|
|
|
TIM_ICInitStructure.TIM_ICFilter = 10;
|
|
|
|
TIM_ICInit(TIMx, &TIM_ICInitStructure);
|
|
|
|
TIM_Cmd(TIMx, ENABLE);
|
|
|
|
TIMx->CNT = 0x7fff;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Encoder_GetTIM2(void)
|
|
|
|
{
|
|
|
|
float cnt;
|
|
|
|
cnt = (float)((uint16_t)0x7fff) - (float)((uint16_t)(TIM2->CNT)); //! (float) is must
|
|
|
|
TIM2->CNT = 0x7fff;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Encoder_GetTIM3(void)
|
|
|
|
{
|
|
|
|
float cnt;
|
|
|
|
cnt = (float)((uint16_t)30000) - (float)((uint16_t)(TIM3->CNT));
|
|
|
|
TIM3->CNT = 30000;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Encoder_GetTIM4(void)
|
|
|
|
{
|
|
|
|
float cnt;
|
|
|
|
cnt = (float)((uint16_t)30000) - (float)((uint16_t)(TIM4->CNT));
|
|
|
|
TIM4->CNT = 30000;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
float Encoder_GetTIM5(void)
|
|
|
|
{
|
|
|
|
float cnt;
|
|
|
|
cnt = (float)((uint16_t)0x7fff) - (float)((uint16_t)(TIM5->CNT));
|
|
|
|
TIM5->CNT = 0x7fff;
|
|
|
|
return cnt;
|
|
|
|
}
|
|
|
|
|
2024-01-20 13:19:09 +08:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
2024-01-24 20:44:32 +08:00
|
|
|
#endif
|