概要
本项目是借鉴b站博主(枫林晚Vx)的代码进行修改,如若需要完整版可以去B站查一查。
本人单片机入门小白,分享项目经验与各位一起学习,若是有不足之处还需各位大佬指正,谢谢
我也是在学的,只是把学习过程做过的项目发出来分享一下
已上传到gitee,连接如下
循迹小车: 2024年电赛循迹小车,用的是红外传感器四路,以及是32板子并不是电赛开发板,仅供学习参考https://gitee.com/stencen/tracking-car.git
本次项目使用的是stm32单片机和两个红外线模块
整体架构流程
基本代码是江协的代码
小车运动部分
首先是驱动小车运动,根据江协的视频和代码我们学会了如何使用PWM驱动电机旋转。在这里我们升级一下,将驱动一个电机升级为四个,但是在PWM内不需要配置四个输出比较模块,只要我们把左边和右边的电机正负线分别连接起来就可以把四个电机看做两个
#include "stm32f10x.h" // Device header
void PWM_Init(void) //初始化PWM
{
//RCC开启时钟 TIM和GPIO外设时钟
//配置时基单元 时钟源选择
//配置输出比较单元
//配置GPIO PWM对应的GPIO口初始化为复用推挽输出的配置
//运行控制
//初始化定时器 —— TIM2 通用定时器
//RCC开启时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE ); //TIM2是APB1总线的外设
//配置GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //开启时钟
GPIO_InitTypeDef GPIO_InitSture;
GPIO_InitSture.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitSture.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_1;
GPIO_InitSture.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitSture);
//选择时基单元的时钟源
TIM_InternalClockConfig(TIM2); //选择内部时钟,TIM2的时基单元由内部时钟驱动
//配置时基单元
TIM_TimeBaseInitTypeDef TIM_TimBaseInitStructure;
TIM_TimBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
//1s
TIM_TimBaseInitStructure.TIM_Period = 100 - 1; //周期ARR 预分频器和计数器都有1个数的偏差 取值 0~65535
TIM_TimBaseInitStructure.TIM_Prescaler = 720 - 1; //预分频器PSC 预分频器和计数器都有1个数的偏差 取值 0~65535
TIM_TimBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TIM_TimBaseInitStructure);
//配置输出比较单元
TIM_OCInitTypeDef TIM_OCInitStructure;
//初始化输出比较单元
TIM_OCStructInit(&TIM_OCInitStructure); //给结构体赋初始值使得所有成员都有一个初始值 以便更改下列成员值
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 50; //CCR的值
TIM_OC3Init(TIM2,&TIM_OCInitStructure); //配置输出比较模块
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
//运行控制使能定时器
TIM_Cmd(TIM2,ENABLE);
}
void PWM_SetCompare3(uint16_t Compare) //PWM_SetCompare1 是用来单独更改通道1 CCR的值的
{
TIM_SetCompare3(TIM2,Compare);
}
void PWM_SetCompare2(uint16_t Compare)
{
TIM_SetCompare2(TIM2, Compare);
}
PWM.h
#ifndef __PWM_H
#define __PWM_H
void PWM_Init(void);
void PWM_SetCompare3(uint16_t Compare);
void PWM_SetCompare2(uint16_t Compare);
#endif
接下来就是电机的运动代码,我们要清楚小车是怎么执行前后左右运动的。总的来说,我们只需要控制左右端即可
Motor.c
#include "stm32f10x.h" // Device header
#include "PWM.h"
void Motor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //开启时钟
GPIO_InitTypeDef GPIO_InitSture;
GPIO_InitSture.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitSture.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitSture.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitSture);
PWM_Init();
}
void Motor_MotorLeftSetSpeed(int16_t Speed) //左侧
{
if (Speed < 0)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
PWM_SetCompare3(Speed);
PWM_SetCompare2(Speed);
}
else if(Speed == 0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
PWM_SetCompare3(Speed);
PWM_SetCompare2(Speed);
}
else
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
PWM_SetCompare3(Speed);
PWM_SetCompare2(Speed);
}
}
void Motor_MotorRightSetSpeed(int16_t Speed) //右侧
{
if (Speed < 0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_6);
GPIO_ResetBits(GPIOA,GPIO_Pin_7);
PWM_SetCompare3(Speed);
PWM_SetCompare2(Speed);
}
else if(Speed == 0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_6);
GPIO_SetBits(GPIOA,GPIO_Pin_7);
PWM_SetCompare3(Speed);
PWM_SetCompare2(Speed);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_6);
GPIO_SetBits(GPIOA,GPIO_Pin_7);
PWM_SetCompare3(Speed);
PWM_SetCompare2(Speed);
}
}
Motor.h
#ifndef __MOTOR_H
#define __MOTOR_H
void Motor_Init(void);
void Motor_MotorLeftSetSpeed(int16_t Speed);
void Motor_MotorRightSetSpeed(int16_t Speed);
#endif
好了,电机代码已经有了接下来就是小车的前进 后退 左转弯 右转弯 。而且左转弯右转弯还分为自转和公转
Car.c
#include "stm32f10x.h" // Device header
#include "Motor.h"
#include "Delay.h"
void Car_Init()
{
Motor_Init();
}
void Go_Ahead()
{
Motor_MotorLeftSetSpeed(70);
Motor_MotorRightSetSpeed(70);
}
void Go_Back()
{
Motor_MotorLeftSetSpeed(-70);
Motor_MotorRightSetSpeed(-70);
}
void Turn_Left()
{
Motor_MotorLeftSetSpeed(0);
Motor_MotorRightSetSpeed(70);
}
void Turn_Right()
{
Motor_MotorLeftSetSpeed(150);
Motor_MotorRightSetSpeed(0);
}
void Self_Left()
{
Motor_MotorLeftSetSpeed(0);
Motor_MotorRightSetSpeed(70);
}
void Self_Right()
{
Motor_MotorLeftSetSpeed(70);
Motor_MotorRightSetSpeed(0);
}
void Car_Stop()
{
Motor_MotorLeftSetSpeed(0);
Motor_MotorRightSetSpeed(0);
}
Car.h
#ifndef __CAR_H
#define __CAR_H
void Car_Init();
void Go_Ahead();
void Go_Back();
void Turn_Left();
void Turn_Right();
void Self_Left();
void Self_Right();
void Car_Stop();
#endif
红外线部分
首先,我们要知道,想要通过红外线循迹我们依靠的是什么来操作的。红外线扫描到黑线我们传递1/0给单片机,而怎么传递就需要另一个代码模块,usart。这里我使用的是江协的代码
Serial.c
#include "stm32f10x.h" // Device header
#include <stdio.h>
#include <stdarg.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; //TX
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; //RX
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 = 1; //指定NVIC线路的抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //指定NVIC线路的响应优先级为1
NVIC_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; //设置结果初值为1
while (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; //定义可变参数列表数据类型的变量arg
va_start(arg, format); //从format开始,接收参数列表到arg变量
vsprintf(String, format, arg); //使用vsprintf打印格式化字符串和参数列表到字符数组中
va_end(arg); //结束变量arg
Serial_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; //置接收标志位变量为1
USART_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
红外线传感器外设部分
track.c
#include "stm32f10x.h" // Device header
void Infrared_Init() //红外线初始化
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_6| GPIO_Pin_7| GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
track.h
#ifndef __INFRARED_H
#define __INFRARED_H
void Infrared_Init();
#endif
小结
好了,基本代码都在这里了,其他的大家自由发挥喽!
有需要全部的可以找我打包