stm蓝牙小车程序代码
时间: 2023-06-23 15:09:29 浏览: 97
下面是一个简单的 STM32F103C8T6 蓝牙小车程序代码,使用 HC-05 蓝牙模块和 L298N 驱动模块控制两个电机。
代码中使用了定时器 TIM2 作为 PWM 信号的产生器,同时使用 USART1 与 HC-05 蓝牙模块通信。通过蓝牙发送的数据来控制小车的运动,具体控制方式见代码注释。
```c
#include "stm32f10x.h"
#define USART_RX_BUF_SIZE 128
volatile uint8_t USART_RX_BUF[USART_RX_BUF_SIZE];
volatile uint8_t USART_RX_STA = 0;
void USART1_Init(uint32_t baudrate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 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);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
void USART1_IRQHandler(void)
{
uint8_t res;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
res = USART_ReceiveData(USART1);
if ((USART_RX_STA & 0x80) == 0)
{
if (USART_RX_STA & 0x40)
{
if (res != 0x0a)
{
USART_RX_STA = 0;
}
else
{
USART_RX_STA |= 0x80;
}
}
else
{
if (res == 0x0d)
{
USART_RX_STA |= 0x40;
}
else
{
USART_RX_BUF[USART_RX_STA & 0x3f] = res;
USART_RX_STA++;
if (USART_RX_STA > (USART_RX_BUF_SIZE - 1))
{
USART_RX_STA = 0;
}
}
}
}
}
}
void TIM2_PWM_Init(uint32_t freq, uint16_t duty)
{
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_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / freq) - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = duty;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE);
}
void Motor_Control(int32_t motor1, int32_t motor2)
{
if (motor1 > 0)
{
TIM_SetCompare2(TIM2, motor1);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
GPIO_SetBits(GPIOB, GPIO_Pin_0);
}
else if (motor1 < 0)
{
TIM_SetCompare2(TIM2, -motor1);
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_SetBits(GPIOB, GPIO_Pin_1);
}
else
{
TIM_SetCompare2(TIM2, 0);
GPIO_ResetBits(GPIOB, GPIO_Pin_0);
GPIO_ResetBits(GPIOB, GPIO_Pin_1);
}
if (motor2 > 0)
{
TIM_SetCompare3(TIM2, motor2);
GPIO_ResetBits(GPIOB, GPIO_Pin_4);
GPIO_SetBits(GPIOB, GPIO_Pin_3);
}
else if (motor2 < 0)
{
TIM_SetCompare3(TIM2, -motor2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_SetBits(GPIOB, GPIO_Pin_4);
}
else
{
TIM_SetCompare3(TIM2, 0);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_ResetBits(GPIOB, GPIO_Pin_4);
}
}
int main(void)
{
SystemInit();
USART1_Init(9600);
TIM2_PWM_Init(20000, 0);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
while (1)
{
if ((USART_RX_STA & 0x80) == 0x80)
{
uint8_t cmd = USART_RX_BUF[0];
if (cmd == 'F')
{
Motor_Control(1000, 1000);
}
else if (cmd == 'B')
{
Motor_Control(-1000, -1000);
}
else if (cmd == 'L')
{
Motor_Control(-1000, 1000);
}
else if (cmd == 'R')
{
Motor_Control(1000, -1000);
}
else if (cmd == 'S')
{
Motor_Control(0, 0);
}
USART_RX_STA = 0;
}
}
}
```
注意事项:
1. 代码中使用的是 STM32F103C8T6 开发板,如果使用其他型号的开发板需要进行相应的修改。
2. HC-05 蓝牙模块需要先进行 AT 指令配置,具体方法可以参考模块的说明书。此处默认模块已经配置好,波特率为 9600。
3. 代码中使用的是 L298N 驱动模块,如果使用其他型号的驱动模块需要进行相应的修改。
4. 代码中 PWM 信号的频率为 20 kHz,占空比范围为 0~1000,可以根据实际需要进行修改。
阅读全文