基于STM32f103C8T6的12V双直流编码电机调速控制,编码器为双通道,电机驱动采用L298n驱动,调速比1:5,将两个电机转速以十进制数的形式反馈给串口助手,请生成基于keil5的代码并给出硬件接线方法
时间: 2024-06-11 12:06:15 浏览: 114
STM32,L298N驱动模块驱动直流电机调速
以下是基于STM32f103C8T6的12V双直流编码电机调速控制的代码和硬件接线方法:
代码:
```
#include "stm32f10x.h"
#define ENA GPIO_Pin_0
#define IN1 GPIO_Pin_1
#define IN2 GPIO_Pin_2
#define ENB GPIO_Pin_3
#define IN3 GPIO_Pin_4
#define IN4 GPIO_Pin_5
#define Encoder_Left TIM2
#define Encoder_Right TIM3
uint32_t count_Left = 0;
uint32_t count_Right = 0;
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = ENA | IN1 | IN2 | ENB | IN3 | IN4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA, ENA | IN1 | IN2 | ENB | IN3 | IN4);
}
void TIM_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE);
// Encoder_Left
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_Left, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(Encoder_Left, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(Encoder_Left, &TIM_ICInitStructure);
TIM_ClearFlag(Encoder_Left, TIM_FLAG_Update);
TIM_ITConfig(Encoder_Left, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(Encoder_Left, ENABLE);
// Encoder_Right
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_Right, &TIM_TimeBaseStructure);
TIM_EncoderInterfaceConfig(Encoder_Right, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);
TIM_ICStructInit(&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_ICFilter = 6;
TIM_ICInit(Encoder_Right, &TIM_ICInitStructure);
TIM_ClearFlag(Encoder_Right, TIM_FLAG_Update);
TIM_ITConfig(Encoder_Right, TIM_IT_Update, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(Encoder_Right, ENABLE);
}
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void send_data(uint16_t data)
{
uint8_t temp[4];
temp[0] = (uint8_t)(data >> 8);
temp[1] = (uint8_t)data;
temp[2] = '\r';
temp[3] = '\n';
for (int i = 0; i < 4; i++) {
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
USART_SendData(USART1, temp[i]);
}
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(Encoder_Left, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(Encoder_Left, TIM_IT_Update);
count_Left = TIM_GetCounter(Encoder_Left);
TIM_SetCounter(Encoder_Left, 0);
}
}
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(Encoder_Right, TIM_IT_Update) != RESET) {
TIM_ClearITPendingBit(Encoder_Right, TIM_IT_Update);
count_Right = TIM_GetCounter(Encoder_Right);
TIM_SetCounter(Encoder_Right, 0);
}
}
void Motor_Control(int left_speed, int right_speed)
{
if (left_speed > 0) {
GPIO_SetBits(GPIOA, IN1);
GPIO_ResetBits(GPIOA, IN2);
TIM_SetCompare1(TIM1, left_speed);
} else if (left_speed < 0) {
GPIO_SetBits(GPIOA, IN2);
GPIO_ResetBits(GPIOA, IN1);
TIM_SetCompare1(TIM1, -left_speed);
} else {
GPIO_ResetBits(GPIOA, IN1 | IN2);
TIM_SetCompare1(TIM1, 0);
}
if (right_speed > 0) {
GPIO_SetBits(GPIOA, IN3);
GPIO_ResetBits(GPIOA, IN4);
TIM_SetCompare2(TIM1, right_speed);
} else if (right_speed < 0) {
GPIO_SetBits(GPIOA, IN4);
GPIO_ResetBits(GPIOA, IN3);
TIM_SetCompare2(TIM1, -right_speed);
} else {
GPIO_ResetBits(GPIOA, IN3 | IN4);
TIM_SetCompare2(TIM1, 0);
}
}
int main(void)
{
GPIO_Config();
TIM_Config();
USART_Config();
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &TIM_OCInitStructure);
TIM_OC2Init(TIM1, &TIM_OCInitStructure);
TIM_Cmd(TIM1, ENABLE);
while (1) {
int left_speed = (count_Left - 32768) * 5;
int right_speed = (count_Right - 32768) * 5;
Motor_Control(left_speed, right_speed);
send_data(count_Left);
send_data(count_Right);
delay_ms(10);
}
}
```
硬件接线方法:
将编码器的A、B两个信号线分别连接到STM32的PA0、PA1和PA6、PA7上,将电机驱动模块L298n的ENA、IN1、IN2、ENB、IN3、IN4分别连接到STM32的PA0、PA1、PA2、PA3、PA4、PA5上,将电机的两个输出端分别连接到L298n的OUT1和OUT2、OUT3和OUT4上。
注:此处只给出了主要的接线方法,具体细节还需要根据实际情况进行调整,如电源的接线等。
阅读全文