GeBalanceBot/4.WHEELTEC B570 平衡小车源码(库函数精.../MiniBalance_HARDWARE/ENCODER/encoder.c

156 lines
5.7 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/***********************************************
公司:轮趣科技(东莞)有限公司
品牌WHEELTEC
官网wheeltec.net
淘宝店铺shop114407458.taobao.com
速卖通: https://minibalance.aliexpress.com/store/4455017
版本5.7
修改时间2021-04-29
Brand: WHEELTEC
Website: wheeltec.net
Taobao shop: shop114407458.taobao.com
Aliexpress: https://minibalance.aliexpress.com/store/4455017
Version:5.7
Update2021-04-29
All rights reserved
***********************************************/
#include "encoder.h"
#include "stm32f10x_gpio.h"
/**************************************************************************
Function: Initialize TIM2 to encoder interface mode
Input : none
Output : none
函数功能把TIM2初始化为编码器接口模式
入口参数:无
返回 值:无
**************************************************************************/
void Encoder_Init_TIM2(void)
{
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);
}
/**************************************************************************
Function: Initialize TIM4 to encoder interface mode
Input : none
Output : none
函数功能把TIM4初始化为编码器接口模式
入口参数:无
返回 值:无
**************************************************************************/
void Encoder_Init_TIM4(void)
{
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);
}
/**************************************************************************
Function: Read encoder count per unit time
Input : TIMXTimer
Output : none
函数功能:单位时间读取编码器计数
入口参数TIMX定时器
返回 值:速度值
**************************************************************************/
int Read_Encoder(u8 TIMX)
{
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;
}
/**************************************************************************
Function: TIM4 interrupt service function
Input : none
Output : none
函数功能TIM4中断服务函数
入口参数:无
返回 值:无
**************************************************************************/
void TIM4_IRQHandler(void)
{
if (TIM4->SR & 0X0001) // 溢出中断
{
}
TIM4->SR &= ~(1 << 0); // 清除中断标志位
}
/**************************************************************************
Function: TIM2 interrupt service function
Input : none
Output : none
函数功能TIM2中断服务函数
入口参数:无
返回 值:无
**************************************************************************/
void TIM2_IRQHandler(void)
{
if (TIM2->SR & 0X0001) // 溢出中断
{
}
TIM2->SR &= ~(1 << 0); // 清除中断标志位
}