标题:
找stm32f1系列的编码器测速例程 求分享
[打印本页]
作者:
小卡少
时间:
2020-5-25 09:42
标题:
找stm32f1系列的编码器测速例程 求分享
找stm32f1系列的编码器测速例程
用的是那种带编码器的减速电机
作者:
Nomi
时间:
2020-5-25 09:42
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_tim.h"
// 定义引脚
#define ENCODER_A_PIN GPIO_Pin_0
#define ENCODER_B_PIN GPIO_Pin_1
#define ENCODER_TIM TIM2
#define ENCODER_TIM_CH TIM_Channel_1
#define ENCODER_TIM_IRQn TIM2_IRQn
volatile uint32_t encoder_count = 0; // 编码器计数值
volatile uint16_t encoder_speed = 0; // 编码器速度
void Encoder_Init(void);
void TIM2_IRQHandler(void);
int main(void)
{
uint8_t dir = 1; // 电机转动方向,1为正转,0为反转
float speed = 0; // 电机速度
Encoder_Init(); // 初始化编码器
TIM_Cmd(ENCODER_TIM, ENABLE); // 使能定时器
TIM_ITConfig(ENCODER_TIM, TIM_IT_Update, ENABLE); // 使能更新中断
NVIC_EnableIRQ(ENCODER_TIM_IRQn); // 使能定时器中断
while (1)
{
// 根据方向计算速度
if (dir)
{
speed = (float)encoder_count * 60 / (float)(4 * TIM_GetCounter(ENCODER_TIM));
}
else
{
speed = (float)encoder_count * 60 / (float)(4 * TIM_GetCounter(ENCODER_TIM)) * -1;
}
}
}
void Encoder_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); // 使能GPIOA和AFIO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // 使能TIM2时钟
// 配置GPIOA.0和GPIOA.1为输入模式,浮空输入,上拉电阻关闭
GPIO_InitStructure.GPIO_Pin = ENCODER_A_PIN | ENCODER_B_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置定时器2为编码器模式,计数器向上计数,自动重装载值为0xFFFF,预分频值为72M/72=1,溢出时产生中断,时钟源为内部时钟源
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(ENCODER_TIM, &TIM_TimeBaseStructure);
// 配置定时器2通道1为复用功能模式,选择输入捕获通道作为输入源,上升沿触发捕获,不滤波,预分频值为72M/72=1,计数器向上计数,溢出时产生中断,时钟源为内部时钟源,禁用PWM输出和死区控制,允许更新中断和复位中断,使能主从JK触发器和边沿对齐模式,使能高级控制寄存器的复位功能和SMS位清零功能,使能预分频器的复位和SMS位清零功能,使能定时器2的复位和SMS位清零功能,使能定时器2的中断和使能全局中断
}
作者:
WangMC
时间:
2023-6-4 10:49
void EncoderT3_Init(void)
{
//编码器1
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//编码器A相B相初始化
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//定时器3初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1; //ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);
//定时器3编码器模式初始化
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_Cmd(TIM3, ENABLE);
}
仅供参考
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1