stm32通过串口控制电机程序
时间: 2023-08-01 09:15:10 浏览: 133
电机控制及串口通讯
5星 · 资源好评率100%
下面是一个简单的示例代码,通过串口控制电机的转速:
```c
#include "stm32f10x.h"
#include <stdio.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALER ((SystemCoreClock/2)/USART_BAUDRATE)
void USART1_Init(void);
void USART1_SendChar(char ch);
void USART1_SendString(const char *str);
char USART1_ReceiveChar(void);
void PWM_Init(void);
void Set_DutyCycle(uint16_t dutyCycle);
int main(void)
{
USART1_Init();
PWM_Init();
uint16_t dutyCycle = 0;
char buffer[10];
while(1)
{
char command = USART1_ReceiveChar();
if(command == 'f') // Forward
{
dutyCycle += 10;
if(dutyCycle > 1000) dutyCycle = 1000;
Set_DutyCycle(dutyCycle);
sprintf(buffer, "%d", dutyCycle);
USART1_SendString("Duty cycle: ");
USART1_SendString(buffer);
USART1_SendString("\r\n");
}
else if(command == 's') // Stop
{
dutyCycle = 0;
Set_DutyCycle(dutyCycle);
USART1_SendString("Stop\r\n");
}
else if(command == 'b') // Backward
{
dutyCycle -= 10;
if(dutyCycle < -1000) dutyCycle = -1000;
Set_DutyCycle(-dutyCycle);
sprintf(buffer, "%d", -dutyCycle);
USART1_SendString("Duty cycle: ");
USART1_SendString(buffer);
USART1_SendString("\r\n");
}
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
// Configure USART1 Tx (PA9) as alternate function push-pull
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);
// Configure USART1 Rx (PA10) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// Configure USART1
USART_InitStructure.USART_BaudRate = USART_BAUDRATE;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
// Enable USART1
USART_Cmd(USART1, ENABLE);
}
void USART1_SendChar(char ch)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
}
void USART1_SendString(const char *str)
{
while(*str)
{
USART1_SendChar(*str++);
}
}
char USART1_ReceiveChar(void)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return USART_ReceiveData(USART1);
}
void PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
// Configure PB0 as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure TIM3
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// Configure TIM3 channel 3 as PWM output
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_OC3Init(TIM3, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
// Enable TIM3
TIM_Cmd(TIM3, ENABLE);
}
void Set_DutyCycle(uint16_t dutyCycle)
{
TIM_SetCompare3(TIM3, dutyCycle);
}
```
在这个示例代码中,我们通过USART1接收串口指令,根据指令来控制电机的转速。我们使用TIM3的PWM输出来控制电机,通过改变占空比来改变电机的转速。例如,当接收到指令 'f' 时,我们增加占空比10,以增加电机的转速;当接收到指令 's' 时,我们将占空比设置为0,以停止电机;当接收到指令 'b' 时,我们减少占空比10,以反向电机转动。
阅读全文