第一次发博客,以及第一次坐智能小车,可能有很多瑕疵和不足。
一、项目时间:2022.11.19~11.22
二、实现效果:小车进行循迹行驶
三、使用模块:
- STM32F103RCT6核心板 * 1
- L298N电机驱动模块 * 2
- TCRT5000L五路红外循迹传感器模块 * 1
- DC3V-6V黄色直流减速电机-TT * 4
- 锂电池组电源 12V * 2
- OLED屏幕-四针 * 1
- LM2596降压模块(新款DC-DC可调稳压电源模块)*1
- DC - DC 12V装3.3v 5v 12v 电源模块
四、基础使用方式:
- OLED显示循迹模块反馈状态【例如:10001】
- 电机驱动1【采用左右轮倒转实现拐弯】
- 电机驱动2【1块控制左轮,一块控制右轮】
- 循迹模式【转角力度不同】
- 降压模块【提供稳定3.3v供电】
五、代码:
1、OLED屏(就写一些用的到的函数)
/**
* @brief OLED显示数字(十进制,正数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~4294967295
* @param Length 要显示数字的长度,范围:1~10
* @retval 无
*/
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
2、 电机控制
(1)PWM
#include "stm32f10x.h" // Device header
#include "PWM.h"
void PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO配置结构体定义
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //TIM配置结构体定义
TIM_OCInitTypeDef TIM_OCInitStructure;//
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3, ENABLE); //使能定时器时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟
//配置IO口模式
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_7;//配置输出引脚
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设定引脚速率
GPIO_Init(GPIOA,&GPIO_InitStructure); //根据结构体参数初始化GPIOA
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; //配置GPIO端口
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设定引脚速率
GPIO_Init(GPIOB,&GPIO_InitStructure); //根据结构体参数初始化GPIOB
TIM_DeInit(TIM3); //初始化TIM3
TIM_DeInit(TIM2); //初始化TIM2
//配置时基单元 ————