stm32f103c6t6用蓝牙控制舵机的完整代码
时间: 2023-10-13 13:05:50 浏览: 158
以下是使用stm32f103c6t6和蓝牙控制舵机的完整代码:
```c
#include "stm32f10x.h"
#include "stdio.h"
#include "string.h"
#include "math.h"
#define PI 3.14159265358979323846
void USART1_Init(void);
void GPIO_Configuration(void);
void TIM2_Configuration(void);
void TIM3_Configuration(void);
void Delay_us(int nCount);
void Delay_ms(int nCount);
void servo_control(float angle);
float angle;
char RxBuffer[20];
volatile uint16_t Pulse[4] = {1500, 1500, 1500, 1500};
int main(void)
{
GPIO_Configuration();
USART1_Init();
TIM2_Configuration();
TIM3_Configuration();
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
static uint8_t i = 0;
char ch = USART_ReceiveData(USART1);
if (ch == '\r' || ch == '\n')
{
RxBuffer[i] = '\0';
i = 0;
angle = atof(RxBuffer);
servo_control(angle);
}
else
{
RxBuffer[i++] = ch;
if (i > 19)
{
i = 0;
}
}
}
Delay_ms(10);
}
}
void servo_control(float angle)
{
uint16_t pulse_width = (uint16_t)(500.0 + angle * 10.0);
if (pulse_width < 500)
{
pulse_width = 500;
}
else if (pulse_width > 2500)
{
pulse_width = 2500;
}
Pulse[0] = pulse_width;
TIM_SetCompare1(TIM2, Pulse[0]);
}
void USART1_Init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, 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 = 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 GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void TIM2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 20000;
TIM_TimeBaseStructure.TIM_Prescaler = 71;
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 = Pulse[0];
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 9999;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
void TIM3_IRQHandler(void)
{
static uint16_t i = 0;
static uint16_t j = 0;
static uint16_t counter = 0;
static uint16_t channel = 0;
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
if (counter == 0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
TIM_SetCompare1(TIM2, Pulse[channel]);
}
else if (counter == 1)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
}
if (++channel >= 4)
{
channel = 0;
}
if (++i >= 50)
{
Pulse[1] = 1500 + (int)(500 * sin(2 * PI * j / 2000));
Pulse[2] = 1500 + (int)(500 * sin(2 * PI * j / 2000 + PI / 2));
Pulse[3] = 1500 + (int)(500 * sin(2 * PI * j / 2000 + PI));
j++;
i = 0;
}
if (++counter >= 2)
{
counter = 0;
}
}
}
void Delay_us(int nCount)
{
int i;
for (i = 0; i < nCount; i++)
{
asm("nop");
}
}
void Delay_ms(int nCount)
{
int i, j;
for (i = 0; i < nCount; i++)
{
for (j = 0; j < 1000; j++)
{
asm("nop");
}
}
}
```
注意,这是一个简单的代码示例,仅用于演示如何使用stm32f103c6t6和蓝牙控制舵机。您需要根据自己的具体情况进行修改和优化。
阅读全文