GeBalanceBot/Reference/MiniBalance_HARDWARE/ENCODER/encoder.c

156 lines
5.7 KiB
C
Raw Normal View History

2024-01-14 19:28:00 +08:00
/***********************************************
2024-01-18 01:55:04 +08:00
WHEELTEC
wheeltec.net
shop114407458.taobao.com
: https://minibalance.aliexpress.com/store/4455017
5.7
2021-04-29
2024-01-14 19:28:00 +08:00
Brand: WHEELTEC
Website: wheeltec.net
2024-01-18 01:55:04 +08:00
Taobao shop: shop114407458.taobao.com
2024-01-14 19:28:00 +08:00
Aliexpress: https://minibalance.aliexpress.com/store/4455017
Version:5.7
2024-01-18 01:55:04 +08:00
Update2021-04-29
2024-01-14 19:28:00 +08:00
All rights reserved
***********************************************/
#include "encoder.h"
#include "stm32f10x_gpio.h"
/**************************************************************************
Function: Initialize TIM2 to encoder interface mode
Input : none
Output : none
2024-01-18 01:55:04 +08:00
TIM2
2024-01-14 19:28:00 +08:00
**************************************************************************/
void Encoder_Init_TIM2(void)
{
2024-01-18 01:55:04 +08:00
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 使能定时器4的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; // 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); // 根据设定参数初始化GPIOB
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; // 设定计数器自动重装值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 选择时钟分频:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; ////TIM向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM2, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); // 使用编码器模式3
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10; // 滤波10
TIM_ICInit(TIM2, &TIM_ICInitStructure);
TIM_ClearFlag(TIM2, TIM_FLAG_Update); // 清除TIM的更新标志位
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
// Reset counter
TIM_SetCounter(TIM2, 0);
TIM_Cmd(TIM2, ENABLE);
2024-01-14 19:28:00 +08:00
}
/**************************************************************************
Function: Initialize TIM4 to encoder interface mode
Input : none
Output : none
2024-01-18 01:55:04 +08:00
TIM4
2024-01-14 19:28:00 +08:00
**************************************************************************/
void Encoder_Init_TIM4(void)
{
2024-01-18 01:55:04 +08:00
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); // 使能定时器4的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // 使能PB端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure); // 根据设定参数初始化GPIOB
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // 预分频器
TIM_TimeBaseStructure.TIM_Period = ENCODER_TIM_PERIOD; // 设定计数器自动重装值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // 选择时钟分频:不分频
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; ////TIM向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); // 使用编码器模式3
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 10;
TIM_ICInit(TIM4, &TIM_ICInitStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update); // 清除TIM的更新标志位
TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
// Reset counter
TIM_SetCounter(TIM4, 0);
TIM_Cmd(TIM4, ENABLE);
2024-01-14 19:28:00 +08:00
}
/**************************************************************************
Function: Read encoder count per unit time
2024-01-18 01:55:04 +08:00
Input : TIMXTimer
2024-01-14 19:28:00 +08:00
Output : none
2024-01-18 01:55:04 +08:00
TIMX
2024-01-14 19:28:00 +08:00
**************************************************************************/
int Read_Encoder(u8 TIMX)
{
2024-01-18 01:55:04 +08:00
int Encoder_TIM;
switch (TIMX)
{
case 2:
Encoder_TIM = (short)TIM2->CNT;
TIM2->CNT = 0;
break;
case 3:
Encoder_TIM = (short)TIM3->CNT;
TIM3->CNT = 0;
break;
case 4:
Encoder_TIM = (short)TIM4->CNT;
TIM4->CNT = 0;
break;
default:
Encoder_TIM = 0;
}
return Encoder_TIM;
2024-01-14 19:28:00 +08:00
}
2024-01-18 01:55:04 +08:00
2024-01-14 19:28:00 +08:00
/**************************************************************************
Function: TIM4 interrupt service function
Input : none
Output : none
2024-01-18 01:55:04 +08:00
TIM4
2024-01-14 19:28:00 +08:00
**************************************************************************/
void TIM4_IRQHandler(void)
2024-01-18 01:55:04 +08:00
{
if (TIM4->SR & 0X0001) // 溢出中断
{
}
TIM4->SR &= ~(1 << 0); // 清除中断标志位
2024-01-14 19:28:00 +08:00
}
/**************************************************************************
Function: TIM2 interrupt service function
Input : none
Output : none
2024-01-18 01:55:04 +08:00
TIM2
2024-01-14 19:28:00 +08:00
**************************************************************************/
void TIM2_IRQHandler(void)
2024-01-18 01:55:04 +08:00
{
if (TIM2->SR & 0X0001) // 溢出中断
{
}
TIM2->SR &= ~(1 << 0); // 清除中断标志位
2024-01-14 19:28:00 +08:00
}