项目技术要求
PWM波形的学习
参考文章stm32 TIM输出比较(PWM驱动LED呼吸灯&&PWM驱动舵机&&PWM驱动直流电机)_ttl pwm 驱动激光头区别-CSDN博客
舵机的学习
参考文章
stm32 TIM输出比较(PWM驱动LED呼吸灯&&PWM驱动舵机&&PWM驱动直流电机)_ttl pwm 驱动激光头区别-CSDN博客
蓝牙的学习
参考文章
stm32 USART串口(串口发送&串口发送+接收)_stm32串口实现接收和发送-CSDN博客
项目材料
降压模块+2节14500 3.7v电池+芯片拓展版+HC-06蓝牙模块+stm32f103c8t6+8个舵机+3D打印机器人骨架
项目结构图
技术问题
PWM初始化
(1)是否可以同时开启多个TIM时钟
1.独立配置:你可以对多个定时器(如 TIM2、TIM3、TIM4 等)分别调用 TIM_InternalClockConfig()
函数,例如:
TIM_InternalClockConfig(TIM2);
TIM_InternalClockConfig(TIM3);
2.相互独立:每个定时器都是独立工作的,因此可以同时开启多个定时器的内部时钟配置。每个定时器的状态和配置不会互相干扰。
3.资源限制:需要注意的是,虽然可以同时开启多个定时器,但要确保你的 MCU 有足够的资源(如定时器数量、计数器和中断处理能力等)。
4.使用场景:在某些应用场景中,你可能会用到多个定时器来实现不同的功能,比如定时器1用于PWM输出,定时器2用于延时,定时器3用于事件计数等。
5.总结:可以同时开启多个定时器的内部时钟配置,只需确保每个定时器都被正确初始化和配置即可。
(2)TIM使能问题
使用定时器(TIM)时,确保其正确使能是非常重要的。确保正确配置和使能 TIM 是保证 STM32 定时器正常工作的关键。
(3)捕获通道的正确开启
由于本项目需要开启所有的捕获通道,要保证所有的通道的开启
输入捕获通道CCR的正确的写入
(4)四足机器人步态
代码书写
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "Servo.h"
#include "Serial.h"
uint8_t RxData;
uint8_t Middle;
float Angle;
const uint16_t RunAdvance[6][8]={{110,150,100,40,130,40,50,50},{45,100,100,40,130,40,50,50},{130,150,160,40,110,40,70,50},
{60,150,140,40,70,40,100,50},{60,150,140,40,120,80,100,50},{80,150,170,40,80,0,50,50}};
const uint8_t StartStates[]={60,145,140,30,100,40,70,60};
const uint16_t HelloStates[2][8]={{60,60,140,20,130,30,70,40},{60,130,140,20,130,30,70,40}};
const uint16_t HapplyStates[2][8]={{60,90,140,80,100,80,70,10},{110,90,90,80,70,80,100,10}};
int main(void)
{ Servo_Init_Right();Servo_Init_Left();Serial_Init(); while (1){if (Serial_GetRxFlag() == 1) //检查串口接收数据的标志位{RxData = Serial_GetRxData(); //获取串口接收的数据Serial_SendByte(RxData);if(RxData==0x34){for(int i=0;i<6;i++){Servo_SetAngle1_Left(RunAdvance[i][0]); Servo_SetAngle2_Left(RunAdvance[i][1]);Servo_SetAngle3_Left(RunAdvance[i][2]);Servo_SetAngle4_Left(RunAdvance[i][3]);Servo_SetAngle1_Right(RunAdvance[i][4]);Servo_SetAngle2_Right(RunAdvance[i][5]);Servo_SetAngle3_Right(RunAdvance[i][6]);Servo_SetAngle4_Right(RunAdvance[i][7]);Delay_ms(250);}}if(RxData==0x35){Servo_SetAngle1_Left(StartStates[0]); Servo_SetAngle2_Left(StartStates[1]);Servo_SetAngle3_Left(StartStates[2]);Servo_SetAngle4_Left(StartStates[3]);Servo_SetAngle1_Right(StartStates[4]);Servo_SetAngle2_Right(StartStates[5]);Servo_SetAngle3_Right(StartStates[6]);Servo_SetAngle4_Right(StartStates[7]);}if(RxData==0x36){for(int i=0;i<2;i++){Servo_SetAngle1_Left(HelloStates[i][0]); Servo_SetAngle2_Left(HelloStates[i][1]);Servo_SetAngle3_Left(HelloStates[i][2]);Servo_SetAngle4_Left(HelloStates[i][3]);Servo_SetAngle1_Right(HelloStates[i][4]);Servo_SetAngle2_Right(HelloStates[i][5]);Servo_SetAngle3_Right(HelloStates[i][6]);Servo_SetAngle4_Right(HelloStates[i][7]);Delay_ms(250);}}if(RxData==0x37){for(int i=0;i<2;i++){Servo_SetAngle1_Left(HapplyStates[i][0]); Servo_SetAngle2_Left(HapplyStates[i][1]);Servo_SetAngle3_Left(HapplyStates[i][2]);Servo_SetAngle4_Left(HapplyStates[i][3]);Servo_SetAngle1_Right(HapplyStates[i][4]);Servo_SetAngle2_Right(HapplyStates[i][5]);Servo_SetAngle3_Right(HapplyStates[i][6]);Servo_SetAngle4_Right(HapplyStates[i][7]);Delay_ms(250);}}}}
}
PWM
PWM.c
#include "stm32f10x.h" // Device headervoid PWM_Init_Left(void)
{/*开启时钟*/RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //开启TIM2的时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟/*GPIO初始化*/GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 |GPIO_Pin_0|GPIO_Pin_2 |GPIO_Pin_3;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA1引脚初始化为复用推挽输出 //受外设控制的引脚,均需要配置为复用模式/*配置时钟源*/TIM_InternalClockConfig(TIM2); //选择TIM2为内部时钟,若不调用此函数,TIM默认也为内部时钟/*时基单元初始化*/TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //定义结构体变量TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频,选择不分频,此参数用于配置滤波器时钟,不影响时基单元功能TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器模式,选择向上计数TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //计数周期,即ARR的值TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //预分频器,即PSC的值TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器,高级定时器才会用到TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure); //将结构体变量交给TIM_TimeBaseInit,配置TIM2的时基单元TIM_OCInitTypeDef TIM_OCInitStructure; //定义结构体变量TIM_OCStructInit(&TIM_OCInitStructure); //结构体初始化,若结构体没有完整赋值//则最好执行此函数,给结构体所有成员都赋一个默认值//避免结构体初值不确定的问题TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //输出比较模式,选择PWM模式1TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性,选择为高,若选择极性为低,则输出高低电平取反TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出使能TIM_OCInitStructure.TIM_Pulse = 0; //初始的CCR值TIM_OC1Init(TIM2, &TIM_OCInitStructure);TIM_OC2Init(TIM2, &TIM_OCInitStructure);TIM_OC3Init(TIM2, &TIM_OCInitStructure);TIM_OC4Init(TIM2, &TIM_OCInitStructure);TIM_Cmd(TIM2, ENABLE);}void PWM_Init_Right(void)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);GPIO_InitTypeDef GPIO_InitStruct;GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 |GPIO_Pin_1;GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOB, &GPIO_InitStruct);TIM_InternalClockConfig(TIM3);TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //定义结构体变量TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; //时钟分频,选择不分频,此参数用于配置滤波器时钟,不影响时基单元功能TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器模式,选择向上计数TIM_TimeBaseInitStructure.TIM_Period = 20000 - 1; //计数周期,即ARR的值TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1; //预分频器,即PSC的值TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0; //重复计数器,高级定时器才会用到TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);TIM_OCInitTypeDef TIM_OCInitStructure; TIM_OCStructInit(&TIM_OCInitStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //输出比较模式,选择PWM模式1TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性,选择为高,若选择极性为低,则输出高低电平取反TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //输出使能TIM_OCInitStructure.TIM_Pulse = 0; //初始的CCR值TIM_OC1Init(TIM3, &TIM_OCInitStructure);TIM_OC2Init(TIM3, &TIM_OCInitStructure);TIM_OC3Init(TIM3, &TIM_OCInitStructure);TIM_OC4Init(TIM3, &TIM_OCInitStructure);TIM_Cmd(TIM3, ENABLE);}void PWM_SetCompare2_Left(uint16_t Compare)
{TIM_SetCompare2(TIM2, Compare); //设置CCR2的值
}
void PWM_SetCompare1_Left(uint16_t Compare)
{TIM_SetCompare1(TIM2, Compare); //设置CCR1的值
}
void PWM_SetCompare3_Left(uint16_t Compare)
{TIM_SetCompare3(TIM2, Compare); //设置CCR1的值
}
void PWM_SetCompare4_Left(uint16_t Compare)
{TIM_SetCompare4(TIM2, Compare); //设置CCR1的值
}void PWM_SetCompare2_Right(uint16_t Compare)
{TIM_SetCompare2(TIM3, Compare); //设置CCR2的值
}
void PWM_SetCompare1_Right(uint16_t Compare)
{TIM_SetCompare1(TIM3, Compare); //设置CCR1的值
}
void PWM_SetCompare3_Right(uint16_t Compare)
{TIM_SetCompare3(TIM3, Compare); //设置CCR1的值
}
void PWM_SetCompare4_Right(uint16_t Compare)
{TIM_SetCompare4(TIM3, Compare); //设置CCR1的值
}
PWM.h
#ifndef __PWM_H
#define __PWM_Hvoid PWM_Init_Left(void);
void PWM_Init_Right(void);
void PWM_SetCompare2_Left(uint16_t Compare);
void PWM_SetCompare1_Left(uint16_t Compare);
void PWM_SetCompare3_Left(uint16_t Compare);
void PWM_SetCompare4_Left(uint16_t Compare);void PWM_SetCompare2_Right(uint16_t Compare);
void PWM_SetCompare1_Right(uint16_t Compare);
void PWM_SetCompare3_Right(uint16_t Compare);
void PWM_SetCompare4_Right(uint16_t Compare);#endif
Servo
Servo.h
#ifndef __SERVO_H
#define __SERVO_Hvoid Servo_Init_Left(void);
void Servo_Init_Right(void);
void Servo_SetAngle1_Left(float Angle);
void Servo_SetAngle2_Left(float Angle);
void Servo_SetAngle3_Left(float Angle);
void Servo_SetAngle4_Left(float Angle);void Servo_SetAngle1_Right(float Angle);
void Servo_SetAngle2_Right(float Angle);
void Servo_SetAngle3_Right(float Angle);
void Servo_SetAngle4_Right(float Angle);#endif
Servo.c
#include "stm32f10x.h" // Device header
#include "PWM.h"/*** 函 数:舵机初始化* 参 数:无* 返 回 值:无*/
void Servo_Init_Left(void)
{PWM_Init_Left(); //初始化舵机的底层PWM}
void Servo_Init_Right(void)
{PWM_Init_Right(); //初始化舵机的底层PWM}void Servo_SetAngle2_Left(float Angle)
{PWM_SetCompare2_Left(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}void Servo_SetAngle1_Left(float Angle)
{PWM_SetCompare1_Left(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}
void Servo_SetAngle3_Left(float Angle)
{PWM_SetCompare3_Left(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}
void Servo_SetAngle4_Left(float Angle)
{PWM_SetCompare4_Left(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}void Servo_SetAngle2_Right(float Angle)
{PWM_SetCompare2_Right(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}void Servo_SetAngle1_Right(float Angle)
{PWM_SetCompare1_Right(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}
void Servo_SetAngle3_Right(float Angle)
{PWM_SetCompare3_Right(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}
void Servo_SetAngle4_Right(float Angle)
{PWM_SetCompare4_Right(Angle / 180 * 2000 + 500); //设置占空比//将角度线性变换,对应到舵机要求的占空比范围上
}
Serial
Serial.c
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.h>
#include "Timer.h"uint8_t Serial_RxData; //定义串口接收的数据变量
uint8_t Serial_RxFlag; //定义串口接收的标志位变量
/*** 函 数:串口初始化* 参 数:无* 返 回 值:无*/
void Serial_Init(void)
{/*开启时钟*/RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //开启USART1的时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟/*GPIO初始化*/GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA9引脚初始化为复用推挽输出GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA10引脚初始化为上拉输入/*USART初始化*/USART_InitTypeDef USART_InitStructure; //定义结构体变量USART_InitStructure.USART_BaudRate = 9600; //波特率USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制,不需要USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //模式,发送模式和接收模式均选择USART_InitStructure.USART_Parity = USART_Parity_No; //奇偶校验,不需要USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位,选择1位USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长,选择8位USART_Init(USART1, &USART_InitStructure); //将结构体变量交给USART_Init,配置USART1/*中断输出配置*/USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启串口接收数据的中断/*NVIC中断分组*/NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //配置NVIC为分组2/*NVIC配置*/NVIC_InitTypeDef NVIC_InitStructure; //定义结构体变量NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //选择配置NVIC的USART1线NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //指定NVIC线路使能NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //指定NVIC线路的抢占优先级为1NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //指定NVIC线路的响应优先级为1NVIC_Init(&NVIC_InitStructure); //将结构体变量交给NVIC_Init,配置NVIC外设/*USART使能*/USART_Cmd(USART1, ENABLE); //使能USART1,串口开始运行
}/*** 函 数:串口发送一个字节* 参 数:Byte 要发送的一个字节* 返 回 值:无*/
void Serial_SendByte(uint8_t Byte)
{USART_SendData(USART1, Byte); //将字节数据写入数据寄存器,写入后USART自动生成时序波形while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //等待发送完成/*下次写入数据寄存器会自动清除发送完成标志位,故此循环后,无需清除标志位*/
}/*** 函 数:串口发送一个数组* 参 数:Array 要发送数组的首地址* 参 数:Length 要发送数组的长度* 返 回 值:无*/
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{uint16_t i;for (i = 0; i < Length; i ++) //遍历数组{Serial_SendByte(Array[i]); //依次调用Serial_SendByte发送每个字节数据}
}/*** 函 数:串口发送一个字符串* 参 数:String 要发送字符串的首地址* 返 回 值:无*/
void Serial_SendString(char *String)
{uint8_t i;for (i = 0; String[i] != '\0'; i ++)//遍历字符数组(字符串),遇到字符串结束标志位后停止{Serial_SendByte(String[i]); //依次调用Serial_SendByte发送每个字节数据}
}/*** 函 数:次方函数(内部使用)* 返 回 值:返回值等于X的Y次方*/
uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{uint32_t Result = 1; //设置结果初值为1while (Y --) //执行Y次{Result *= X; //将X累乘到结果}return Result;
}/*** 函 数:串口发送数字* 参 数:Number 要发送的数字,范围:0~4294967295* 参 数:Length 要发送数字的长度,范围:0~10* 返 回 值:无*/
void Serial_SendNumber(uint32_t Number, uint8_t Length)
{uint8_t i;for (i = 0; i < Length; i ++) //根据数字长度遍历数字的每一位{Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0'); //依次调用Serial_SendByte发送每位数字}
}/*** 函 数:使用printf需要重定向的底层函数* 参 数:保持原始格式即可,无需变动* 返 回 值:保持原始格式即可,无需变动*/
int fputc(int ch, FILE *f)
{Serial_SendByte(ch); //将printf的底层重定向到自己的发送字节函数return ch;
}/*** 函 数:自己封装的prinf函数* 参 数:format 格式化字符串* 参 数:... 可变的参数列表* 返 回 值:无*/
void Serial_Printf(char *format, ...)
{char String[100]; //定义字符数组va_list arg; //定义可变参数列表数据类型的变量argva_start(arg, format); //从format开始,接收参数列表到arg变量vsprintf(String, format, arg); //使用vsprintf打印格式化字符串和参数列表到字符数组中va_end(arg); //结束变量argSerial_SendString(String); //串口发送字符数组(字符串)
}/*** 函 数:获取串口接收标志位* 参 数:无* 返 回 值:串口接收标志位,范围:0~1,接收到数据后,标志位置1,读取后标志位自动清零*/
uint8_t Serial_GetRxFlag(void)
{if (Serial_RxFlag == 1) //如果标志位为1{Serial_RxFlag = 0;return 1; //则返回1,并自动清零标志位}return 0; //如果标志位为0,则返回0
}/*** 函 数:获取串口接收的数据* 参 数:无* 返 回 值:接收的数据,范围:0~255*/
uint8_t Serial_GetRxData(void)
{return Serial_RxData; //返回接收的数据变量
}/*** 函 数:USART1中断函数* 参 数:无* 返 回 值:无* 注意事项:此函数为中断函数,无需调用,中断触发后自动执行* 函数名为预留的指定名称,可以从启动文件复制* 请确保函数名正确,不能有任何差异,否则中断函数将不能进入*/
void USART1_IRQHandler(void)
{if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //判断是否是USART1的接收事件触发的中断{Serial_RxData = USART_ReceiveData(USART1); //读取数据寄存器,存放在接收的数据变量Serial_RxFlag = 1; //置接收标志位变量为1USART_ClearITPendingBit(USART1, USART_IT_RXNE); //清除USART1的RXNE标志位//读取数据寄存器会自动清除此标志位//如果已经读取了数据寄存器,也可以不执行此代码}
}
Serial.h
#ifndef __SERIAL_H
#define __SERIAL_H#include <stdio.h>void Serial_Init(void);
void Serial_SendByte(uint8_t Byte);
void Serial_SendArray(uint8_t *Array, uint16_t Length);
void Serial_SendString(char *String);
void Serial_SendNumber(uint32_t Number, uint8_t Length);
void Serial_Printf(char *format, ...);uint8_t Serial_GetRxFlag(void);
uint8_t Serial_GetRxData(void);#endif
项目不足
没有写掉头逻辑,四足机器人走路存在一点问题,有时间就改进。