stm32蓝牙小车hal库PWM控制电机代码
时间: 2023-06-26 09:08:06 浏览: 168
以下是基于HAL库的STM32蓝牙小车PWM控制电机代码,代码中使用了TIM3作为PWM的计数器,使用了TIM3_CH1和TIM3_CH2作为PWM输出引脚,同时使用了UART2作为蓝牙通信口:
```c
#include "main.h"
#include "stm32f1xx_hal.h"
/* 定义PWM周期 */
#define PWM_PERIOD 999
/* 定义电机控制命令 */
#define CMD_FORWARD 'F'
#define CMD_BACKWARD 'B'
#define CMD_LEFT 'L'
#define CMD_RIGHT 'R'
#define CMD_STOP 'S'
/* 定义电机PWM占空比 */
#define PWM_DUTY_MAX 800
#define PWM_DUTY_MIN 200
/* 定义电机PWM输出引脚 */
#define MOTOR_PWM1_PIN GPIO_PIN_6
#define MOTOR_PWM2_PIN GPIO_PIN_7
#define MOTOR_PWM_PORT GPIOA
/* 定义蓝牙通信口 */
#define BT_UART huart2
/* 定义全局变量 */
UART_HandleTypeDef BT_UART;
TIM_HandleTypeDef htim3;
/* 函数声明 */
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM3_Init(void);
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
int main(void)
{
HAL_Init();
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TIM3_Init();
/* 启动PWM计数器 */
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
uint8_t rx_data;
uint16_t pwm_duty1 = 0;
uint16_t pwm_duty2 = 0;
while (1)
{
/* 接收蓝牙数据 */
if (HAL_UART_Receive(&BT_UART, &rx_data, 1, 100) == HAL_OK)
{
switch (rx_data)
{
case CMD_FORWARD:
pwm_duty1 = PWM_DUTY_MAX;
pwm_duty2 = PWM_DUTY_MAX;
break;
case CMD_BACKWARD:
pwm_duty1 = PWM_DUTY_MIN;
pwm_duty2 = PWM_DUTY_MIN;
break;
case CMD_LEFT:
pwm_duty1 = PWM_DUTY_MAX;
pwm_duty2 = PWM_DUTY_MIN;
break;
case CMD_RIGHT:
pwm_duty1 = PWM_DUTY_MIN;
pwm_duty2 = PWM_DUTY_MAX;
break;
case CMD_STOP:
pwm_duty1 = 0;
pwm_duty2 = 0;
break;
default:
break;
}
/* 更新PWM占空比 */
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, pwm_duty1);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_2, pwm_duty2);
}
}
}
/* TIM3 init function */
static void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = PWM_PERIOD;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim3);
}
/* USART2 init function */
static void MX_USART2_UART_Init(void)
{
BT_UART.Instance = USART2;
BT_UART.Init.BaudRate = 9600;
BT_UART.Init.WordLength = UART_WORDLENGTH_8B;
BT_UART.Init.StopBits = UART_STOPBITS_1;
BT_UART.Init.Parity = UART_PARITY_NONE;
BT_UART.Init.Mode = UART_MODE_RX;
BT_UART.Init.HwFlowCtl = UART_HWCONTROL_NONE;
BT_UART.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&BT_UART) != HAL_OK)
{
Error_Handler();
}
}
/* GPIO init function */
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
__HAL_RCC_GPIOA_CLK_ENABLE();
/* 配置PWM输出引脚 */
GPIO_InitStruct.Pin = MOTOR_PWM1_PIN | MOTOR_PWM2_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(MOTOR_PWM_PORT, &GPIO_InitStruct);
/* 配置蓝牙通信口 */
GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
__HAL_AFIO_REMAP_USART2_ENABLE();
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if (htim->Instance == TIM3)
{
/* TIM3 clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM3 GPIO Configuration
PA6 ------> TIM3_CH1
PA7 ------> TIM3_CH2
*/
GPIO_InitStruct.Pin = MOTOR_PWM1_PIN | MOTOR_PWM2_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(MOTOR_PWM_PORT, &GPIO_InitStruct);
}
}
void Error_Handler(void)
{
while (1)
{
/* 错误处理函数 */
}
}
```
请注意,此代码仅供参考,可能需要根据具体的硬件和应用场景进行修改。
阅读全文