stm32f103c8t6 蓝牙控制舵机代码
时间: 2023-09-23 18:11:01 浏览: 118
以下是一个简单的基于STM32F103C8T6和蓝牙模块的舵机控制代码示例。该代码使用USART1与蓝牙模块通信,并使用PWM输出控制舵机。
```c
#include "stm32f10x.h"
#define PWM_Period 20000 // PWM周期为20ms
#define Servo_Max 2500 // 舵机最大角度对应的PWM占空比
#define Servo_Min 500 // 舵机最小角度对应的PWM占空比
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
// USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// USART1 初始化设置
USART_InitStructure.USART_BaudRate = 9600;
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);
USART_Cmd(USART1, ENABLE);
}
void TIM3_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);
// PWM输出引脚 PB5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// TIM3初始化设置
TIM_TimeBaseStructure.TIM_Period = PWM_Period - 1;
TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
// PWM模式设置
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = Servo_Min; // 初始位置为最小角度
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_Cmd(TIM3, ENABLE);
}
void USART_SendByte(USART_TypeDef* USARTx, uint8_t byte)
{
USART_SendData(USARTx, byte);
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
}
void ServoControl(uint16_t angle)
{
uint16_t pwm_val = PWM_Period / 20 * (angle + 90) / 180 + Servo_Min;
if (pwm_val > Servo_Max) pwm_val = Servo_Max;
TIM_SetCompare1(TIM3, pwm_val);
}
int main(void)
{
USART1_Init();
TIM3_PWM_Init();
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
uint8_t byte = USART_ReceiveData(USART1);
if (byte == 'w') ServoControl(0); // 向前
else if (byte == 's') ServoControl(180); // 向后
else if (byte == 'a') ServoControl(-90); // 向左
else if (byte == 'd') ServoControl(90); // 向右
else if (byte == 'q') ServoControl(45); // 向右上
else if (byte == 'e') ServoControl(-45); // 向左上
else if (byte == 'z') ServoControl(135); // 向右下
else if (byte == 'c') ServoControl(-135); // 向左下
}
}
}
```
在本示例中,我们使用PA9和PA10设置USART1,并使用TIM3 CH1在PB5上输出PWM信号以控制舵机。在`main`函数中,我们使用`USART_GetFlagStatus`检查是否接收到数据,并使用`USART_ReceiveData`读取接收缓冲区中的数据。然后,我们根据接收到的数据调用`ServoControl`函数以控制舵机的角度。
`ServoControl`函数将舵机角度转换为PWM占空比,并将其设置为TIM3 CH1的比较值。在本示例中,我们将舵机的最小角度设置为-90度,最大角度设置为90度,但实际上可能会因舵机型号而异。您需要根据您的舵机和应用程序进行调整。
阅读全文