本项目算是接触32来开发的第一个小项目了,虽然前期用51写过一个循迹小车,以为直接转到32会比较简单,结果还是花了大几天才把小车的参数完全调完,以此来记录下自己的学习历程(注:循迹算法并未加入PID算法)
所需原件及其模块:
1.stm32c8t6最小系统板:
2.红外循迹模块(可以只用一个五路循迹模块,不需要单个的循迹模块,使用单个循迹模块是因为需要检测起始线(比赛题目要求))
五路循迹模块:
单个循迹模块:
3.车模底板:
4.L298N电机驱动模块(不能直接用单片机给电机供电,需要用一个驱动模块):
5.TT马达(直流电机):
6.面包板(便于插线,单片机的接口不够用的时候使用面包板可以很好的解决这个问题):
7.0.96寸OLED屏:
具体介绍每个原件及模块的用法:
1.红外循迹模块:检测黑线的原理是向外发射红外线,当没有遇到黑线的时候,返回回来,之后返回高电平,当遇到黑线的时候,红外被吸收,返回高电平,读取相应IO口的电平就可以知道小车此时的大致位置;
2.L298N电机驱动模块:分别是5V,12V,GND,可以通过外加电源给驱动模块供电,IN1,IN2,IN3,IN4,EN1,EN2,通过给IN口不同的电平,可以达到控制电机正反转,EN口为使能口,可以通过给EN口不同的PWM波,来达到控制小车速度,模块有四个输入接口,分别连接四个TT马达,驱动小车运动;PWM波:通过定时器来控制占空比,从而达到宏观上控速的现象;
源码程序:
wheel.c:
cpp
#include "wheel.h"
void wheel_Init(){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = IN1 | IN2 | IN3 | IN4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void Car_Go(){
//左轮向前
GPIO_SetBits(GPIOA, IN1);
GPIO_ResetBits(GPIOA, IN2);
//右轮向前
GPIO_SetBits(GPIOA, IN3);
GPIO_ResetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(100);
//右轮速度
PWM_SetCompare3(100);
}
void Stop(){
//左轮速度
PWM_SetCompare2(0);
//右轮速度
PWM_SetCompare3(0);
}
void Car_GoLeft(){
//左轮向前
GPIO_SetBits(GPIOA, IN1);
GPIO_ResetBits(GPIOA, IN2);
//右轮向前
GPIO_SetBits(GPIOA, IN3);
GPIO_ResetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(50);
//右轮速度
PWM_SetCompare3(55);
}
void Car_GoRight(){
//左轮向前
GPIO_SetBits(GPIOA, IN1);
GPIO_ResetBits(GPIOA, IN2);
//右轮向前
GPIO_SetBits(GPIOA, IN3);
GPIO_ResetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(55);
//右轮速度
PWM_SetCompare3(50);
}
void TurnLeft_Slow(){
//左轮向后
GPIO_ResetBits(GPIOA, IN1);
GPIO_SetBits(GPIOA, IN2);
//右轮向前
GPIO_SetBits(GPIOA, IN3);
GPIO_ResetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(75);
//右轮速度
PWM_SetCompare3(75);
}
void TurnRight_Slow(){
//左轮向前
GPIO_SetBits(GPIOA, IN1);
GPIO_ResetBits(GPIOA, IN2);
//右轮向后
GPIO_ResetBits(GPIOA, IN3);
GPIO_SetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(75);
//右轮速度
PWM_SetCompare3(75);
}
void TurnLeft_Fast(){
//左轮向后
GPIO_ResetBits(GPIOA, IN1);
GPIO_SetBits(GPIOA, IN2);
//右轮向前
GPIO_SetBits(GPIOA, IN3);
GPIO_ResetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(70);
//右轮速度
PWM_SetCompare3(70);
}
void TurnRight_Fast(){
//左轮向前
GPIO_SetBits(GPIOA, IN1);
GPIO_ResetBits(GPIOA, IN2);
//右轮向后
GPIO_ResetBits(GPIOA, IN3);
GPIO_SetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(70);
//右轮速度
PWM_SetCompare3(70);
}
void Angle_Left(){
//左轮向后
GPIO_ResetBits(GPIOA, IN1);
GPIO_SetBits(GPIOA, IN2);
//右轮向前
GPIO_SetBits(GPIOA, IN3);
GPIO_ResetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(90);
//右轮速度
PWM_SetCompare3(90);
}
void Angle_Right(){
//左轮向前
GPIO_SetBits(GPIOA, IN1);
GPIO_ResetBits(GPIOA, IN2);
//右轮向后
GPIO_ResetBits(GPIOA, IN3);
GPIO_SetBits(GPIOA, IN4);
//左轮速度
PWM_SetCompare2(90);
//右轮速度
PWM_SetCompare3(90);
}
wheel.h:
cpp
#ifndef __WHEEL_H
#define __WHEEL_H
#include "stm32f10x.h"
#include "PWM.h"
/* IN1 PA5
IN2 PA6
IN3 PA3
IN4 PA4
*/
#define IN1 GPIO_Pin_5
#define IN2 GPIO_Pin_6
#define IN3 GPIO_Pin_3
#define IN4 GPIO_Pin_4
void wheel_Init();//初始化车轮
void Car_Go();//向前
void Stop();//停车
void Car_GoLeft();//当小车车身略偏右的时候,右轮速度略大于左轮,可以使小车回到正轨
void Car_GoRight();//当小车车身略偏左的时候,右轮速度略大于左轮,可以使小车回到正轨
void TurnLeft_Slow();//慢左转
void TurnRight_Slow();//慢右转
void TurnLeft_Fast();//快左转
void TurnRight_Fast();//快右转
void Angle_Left();//向左转直角弯
void Angle_Right();//向右转直角弯
#endif
PWM.c:
cpp
#include "PWM.h"
//采用定时器TIM2的通道2,通道3输出PWM波,分别控制左右轮速度
void PWM_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 150 - 1; //ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
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 = 0; //CCR
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
TIM_Cmd(TIM2, ENABLE);
}
//左轮调速
void PWM_SetCompare2(uint16_t Compare)
{
TIM_SetCompare2(TIM2, Compare);
}
//右轮调速
void PWM_SetCompare3(uint16_t Compare)
{
TIM_SetCompare3(TIM2, Compare);
}
PWM.h:
cpp
#ifndef __PWM_H
#define __PWM_H
#include "stm32f10x.h"
/*
EN1 PA2(右轮调速)
EN2 PA1(左轮调速)
*/
void PWM_Init();
void PWM_SetCompare2(uint16_t Compare);
void PWM_SetCompare3(uint16_t Compare);
#endif
**Trace.c:**讲解一下循迹代码,就是当检测到某一个具体的状态时,需要小车做出相应的反应来维护小车始终在黑线上,比如,假设此时的状态为(!DET1 && DET2 && DET3 && DET4 && DET5)即红外循迹模块检测到电平为01111,则此时黑线在小车左边,需要快速将小车左转,则我们可以将小车的左轮向后转,右轮向前转,就可以很好的转向了,此外当小车速度过快的时候,可能刚检测到黑线的时候,由于小车没反应过来,车身已经冲出赛道了,我们可以另外用一个算法来维护,假设我们冲出赛道的时候是向左转,则一旦冲出赛道(即红外循迹模块电平为11111)此时可以持续小车左转的状态,直到小车自行转到赛道上,可以继续沿着赛道循迹,当然此方法只是没学PID算法的一种下策,并不能很流畅的解决所有赛道
cpp
#include "Trace.h"
//Flag标志位,用于存放小车偏离轨道时的上一个状态,Flag为一则上次为左转,Flag为2则上次为右转
unsigned int Flag=0;
//StopFlag标志位判断小车是否为第一次经过起始线
unsigned int StopFlag=0;
void Trace_Init(){
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15 | GPIO_Pin_12 | GPIO_Pin_7;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
//0为黑线
void Trace(){
if(DET1 && DET2 && !DET3 && DET4 && DET5) {Car_Go();Flag=0;}//11011
if(DET1 && !DET2 && !DET3 && !DET4 && DET5) {Car_Go();Flag=0;}//10001
if(DET1 && !DET2 && DET3 && DET4 && DET5) {Car_GoLeft();Flag=1;}//10111
if(DET1 && !DET2 && !DET3 && DET4 && DET5) {Car_GoLeft();Flag=1;}//10011
if(!DET1 && DET2 && DET3 && DET4 && DET5) {TurnLeft_Fast();Flag=1;}//01111
if(!DET1 && !DET2 && DET3 && DET4 && DET5) {Angle_Left();Flag=1;}//00111
if(!DET1 && !DET2 && !DET3 && DET4 && DET5) {Angle_Left();Flag=1;}//00011
if(!DET1 && !DET2 && !DET3 && !DET4 && DET5) {Angle_Left();Flag=1;}//00001
if(DET1 && DET2 && DET3 && !DET4 && DET5) {Car_GoRight();Flag=2;}//11101
if(DET1 && DET2 && !DET3 && !DET4 && DET5) {Car_GoRight();Flag=2;}//11001
if(DET1 && DET2 && DET3 && DET4 && !DET5) {TurnRight_Fast();Flag=2;}//11110
if(DET1 && DET2 && DET3 && !DET4 && !DET5) {Angle_Right();Flag=2;}//11100
if(DET1 && DET2 && !DET3 && !DET4 && !DET5) {Angle_Right();Flag=2;}//11000
if(DET1 && !DET2 && !DET3 && !DET4 && !DET5) {Angle_Right();Flag=2;}//10000
if(!DET1 && !DET2 && !DET3 && !DET4 && !DET5) {Car_Go();Flag=0;}//00000
if(DET1 && DET2 && DET3 && DET4 && DET5){
if(!Flag){Car_Go();Flag=0;}
if(Flag==1){TurnLeft_Fast();Flag=1;}
if(Flag==2){TurnRight_Fast();Flag=2;}
}
}
//判断小车是否为第一次经过起始线,当第二次经过时停止
int Car_State(){
if(DETLeft && DETRight){
while(DETLeft && DETRight){}
if(!StopFlag){
StopFlag=1;
return 0;
}
else
return 1;
}
return 0;
}
Trace.h:
cpp
#ifndef __TRACE_H
#define __TRACE_H
#include "stm32f10x.h"
#include "wheel.h"
#include "PWM.h"
//从左到右红外循迹模块依次
#define DETLeft GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_12)
#define DETRight GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_7)
#define DET1 GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15)
#define DET2 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_4)
#define DET3 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_5)
#define DET4 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6)
#define DET5 GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7)
void Trace_Init();
void Trace();
int Car_State();
#endif
OLED.c:
cpp
#include "stm32f10x.h"
#include "OLED_Font.h"
/*引脚配置*/
#define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
#define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
/*引脚初始化*/
void OLED_I2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C开始
* @param 无
* @retval 无
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1);
OLED_W_SCL(1);
OLED_W_SDA(0);
OLED_W_SCL(0);
}
/**
* @brief I2C停止
* @param 无
* @retval 无
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C发送一个字节
* @param Byte 要发送的一个字节
* @retval 无
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
OLED_W_SDA(Byte & (0x80 >> i));
OLED_W_SCL(1);
OLED_W_SCL(0);
}
OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
OLED_W_SCL(0);
}
/**
* @brief OLED写命令
* @param Command 要写入的命令
* @retval 无
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //从机地址
OLED_I2C_SendByte(0x00); //写命令
OLED_I2C_SendByte(Command);
OLED_I2C_Stop();
}
/**
* @brief OLED写数据
* @param Data 要写入的数据
* @retval 无
*/
void OLED_WriteData(uint8_t Data)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //从机地址
OLED_I2C_SendByte(0x40); //写数据
OLED_I2C_SendByte(Data);
OLED_I2C_Stop();
}
/**
* @brief OLED设置光标位置
* @param Y 以左上角为原点,向下方向的坐标,范围:0~7
* @param X 以左上角为原点,向右方向的坐标,范围:0~127
* @retval 无
*/
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
OLED_WriteCommand(0xB0 | Y); //设置Y位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
}
/**
* @brief OLED清屏
* @param 无
* @retval 无
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j++)
{
OLED_SetCursor(j, 0);
for(i = 0; i < 128; i++)
{
OLED_WriteData(0x00);
}
}
}
/**
* @brief OLED显示一个字符
* @param Line 行位置,范围:1~4
* @param Column 列位置,范围:1~16
* @param Char 要显示的一个字符,范围:ASCII可见字符
* @retval 无
*/
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{
uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容
}
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容
}
}
void OLED_ShowChinese(uint8_t Line, uint8_t Column, uint8_t num)
{
uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_chinese[num][i]); //显示上半部分内容
}
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_chinese[num][i + 8]); //显示下半部分内容
}
}
void OLDE_ShowChinese2(uint8_t Line, uint8_t Column, uint8_t num)
{
OLED_ShowChinese(Line,Column,num);
OLED_ShowChinese(Line,Column+1,num+1);
}
/**
* @brief OLED显示字符串
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串,范围:ASCII可见字符
* @retval 无
*/
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++)
{
OLED_ShowChar(Line, Column + i, String[i]);
}
}
/**
* @brief OLED次方函数
* @retval 返回值等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y--)
{
Result *= X;
}
return Result;
}
/**
* @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');
}
}
/**
* @brief OLED显示数字(十进制,带符号数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-2147483648~2147483647
* @param Length 要显示数字的长度,范围:1~10
* @retval 无
*/
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0)
{
OLED_ShowChar(Line, Column, '+');
Number1 = Number;
}
else
{
OLED_ShowChar(Line, Column, '-');
Number1 = -Number;
}
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief OLED显示数字(十六进制,正数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFFFFFF
* @param Length 要显示数字的长度,范围:1~8
* @retval 无
*/
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++)
{
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10)
{
OLED_ShowChar(Line, Column + i, SingleNumber + '0');
}
else
{
OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
}
}
}
/**
* @brief OLED显示数字(二进制,正数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~1111 1111 1111 1111
* @param Length 要显示数字的长度,范围:1~16
* @retval 无
*/
void OLED_ShowBinNum(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(2, Length - i - 1) % 2 + '0');
}
}
/**
* @brief OLED初始化
* @param 无
* @retval 无
*/
void OLED_Init(void)
{
uint32_t i, j;
for (i = 0; i < 1000; i++) //上电延时
{
for (j = 0; j < 1000; j++);
}
OLED_I2C_Init(); //端口初始化
OLED_WriteCommand(0xAE); //关闭显示
OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8); //设置多路复用率
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3); //设置显示偏移
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40); //设置显示开始行
OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置
OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置
OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //设置对比度控制
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9); //设置预充电周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
OLED_WriteCommand(0xA6); //设置正常/倒转显示
OLED_WriteCommand(0x8D); //设置充电泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //开启显示
OLED_Clear(); //OLED清屏
}
OLED.h:
cpp
#ifndef __OLED_H
#define __OLED_H
void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowChinese(uint8_t Line, uint8_t Column, uint8_t num);
void OLDE_ShowChinese2(uint8_t Line, uint8_t Column, uint8_t num);
#endif
OLED_Font.h(字模库) (该字库为取模软件生成,若有想展示的字可以自行取模):
cpp
#ifndef __OLED_FONT_H
#define __OLED_FONT_H
const uint8_t OLED_chinese[][16]={
//注意第二行和第三行要调转一下位置
0x20,0x20,0x24,0x24,0x24,0xA4,0x24,0x24,
0x40,0xC0,0x7E,0x40,0x40,0x3F,0x22,0x22,
0x20,0xFF,0x20,0x22,0x2C,0x20,0x20,0x00,
0x20,0x03,0x0C,0x10,0x20,0x40,0xF8,0x00,/*"武",0*/
0x10,0x60,0x02,0x0C,0xC0,0x02,0x1E,0xE2,
0x04,0x04,0x7C,0x03,0x80,0x80,0x40,0x20,
0x02,0x02,0x02,0xE2,0x1E,0x00,0x00,0x00,
0x13,0x0C,0x13,0x20,0x40,0x80,0x80,0x00,/*"汉",2*/
0x00,0x04,0x04,0x04,0x04,0x04,0x04,0xFC,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,
0x04,0x04,0x04,0x04,0x04,0x04,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,/*"工",4*/
0x24,0x24,0xA4,0xFE,0x23,0x22,0x00,0x3E,
0x08,0x06,0x01,0xFF,0x01,0x06,0x40,0x49,
0x22,0x22,0x22,0x22,0x22,0x3E,0x00,0x00,
0x49,0x49,0x7F,0x49,0x49,0x49,0x41,0x00,/*"程",6*/
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xFF,
0x80,0x80,0x40,0x20,0x10,0x0C,0x03,0x00,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,
0x03,0x0C,0x10,0x20,0x40,0x80,0x80,0x00,/*"大",8*/
0x40,0x30,0x11,0x96,0x90,0x90,0x91,0x96,
0x04,0x04,0x04,0x04,0x04,0x44,0x84,0x7E,
0x90,0x90,0x98,0x14,0x13,0x50,0x30,0x00,
0x06,0x05,0x04,0x04,0x04,0x04,0x04,0x00,/*"学",10*/
0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,
0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,
0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00,
0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00,/*"电",12*/
0x20,0x10,0x4C,0x47,0x54,0x54,0x54,0x54,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x54,0x54,0x54,0xD4,0x04,0x04,0x00,0x00,
0x00,0x00,0x00,0x0F,0x30,0x40,0xF0,0x00,/*"气",14*/
0x00,0x80,0x60,0xF8,0x07,0x00,0x04,0x24,
0x01,0x00,0x00,0xFF,0x00,0x00,0x00,0xF9,
0x24,0x25,0x26,0x24,0x24,0x24,0x04,0x00,
0x49,0x49,0x49,0x49,0x49,0xF9,0x00,0x00,/*"信",16*/
0x00,0x00,0x00,0xFC,0x54,0x54,0x56,0x55,
0x40,0x30,0x00,0x03,0x39,0x41,0x41,0x45,
0x54,0x54,0x54,0xFC,0x00,0x00,0x00,0x00,
0x59,0x41,0x41,0x73,0x00,0x08,0x30,0x00,/*"息",18*/
0x40,0x30,0x11,0x96,0x90,0x90,0x91,0x96,
0x04,0x04,0x04,0x04,0x04,0x44,0x84,0x7E,
0x90,0x90,0x98,0x14,0x13,0x50,0x30,0x00,
0x06,0x05,0x04,0x04,0x04,0x04,0x04,0x00,/*"学",20*/
0x00,0xFE,0x22,0x5A,0x86,0x10,0x0C,0x24,
0x00,0xFF,0x04,0x08,0x07,0x80,0x41,0x31,
0x24,0x25,0x26,0x24,0x24,0x14,0x0C,0x00,
0x0F,0x01,0x01,0x3F,0x41,0x41,0x71,0x00,/*"院",22*/
0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,
0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,
0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00,
0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00,/*"电",24*/
0x80,0x82,0x82,0x82,0x82,0x82,0x82,0xE2,
0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,
0xA2,0x92,0x8A,0x86,0x82,0x80,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*"子",26*/
0x00,0x80,0x60,0xF8,0x07,0x00,0x04,0x24,
0x01,0x00,0x00,0xFF,0x00,0x00,0x00,0xF9,
0x24,0x25,0x26,0x24,0x24,0x24,0x04,0x00,
0x49,0x49,0x49,0x49,0x49,0xF9,0x00,0x00,/*"信",28*/
0x00,0x00,0x00,0xFC,0x54,0x54,0x56,0x55,
0x40,0x30,0x00,0x03,0x39,0x41,0x41,0x45,
0x54,0x54,0x54,0xFC,0x00,0x00,0x00,0x00,
0x59,0x41,0x41,0x73,0x00,0x08,0x30,0x00,/*"息",30*/
0x00,0x04,0x04,0x04,0x04,0x04,0x04,0xFC,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,
0x04,0x04,0x04,0x04,0x04,0x04,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00,/*"工",32*/
0x24,0x24,0xA4,0xFE,0x23,0x22,0x00,0x3E,
0x08,0x06,0x01,0xFF,0x01,0x06,0x40,0x49,
0x22,0x22,0x22,0x22,0x22,0x3E,0x00,0x00,
0x49,0x49,0x7F,0x49,0x49,0x49,0x41,0x00,//"程",34*
};
/*OLED字模库,宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,
0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,
0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,
0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,
0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,
0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,
0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,
0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,
0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,
0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,
0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,
0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,
0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,
0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,
0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,
0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,
0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,
0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,
0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,
0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,
0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,
0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,
0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,
0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,
0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,
0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,
0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,
0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,
0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,
0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,
0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,
0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,
0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,
0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,
0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,
0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,
0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,
0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,
0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,
0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,
0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,
0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,
0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,
0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
#endif
最后将小车的布线及演示展示于下:
小车循迹演示