文章目录[隐藏]
目录
1.软件准备
(1)编程平台:Keil5
(2)CubeMX
(3)中景园移植代码——点我下载 提取码:6666
2.硬件准备
(1)F1的板子,本例使用经典F103C8T6
(2)0.96'/1.3' OLED屏幕模块(7针,SSD1306驱动)
(3)ST-link 下载器
(4)杜邦线若干
3.CubeMX配置
(1)芯片选择
(2)配置RCC、SYS、时钟树
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/75f2d412d7b3414ea5fcbb61d69b6711.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/f5f718bdc5454a00bbd4005b372048c0.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/7cc04c5897cd48dd94ec73a6959018c0.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
(3)配置SPI
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/f273e5424a974224a458fbda14759319.png?imageMogr2/thumbnail/!1338x1152r|imageMogr2/gravity/Center/crop/1338x1152/interlace/1/quality/60)
(4)配置GPIO
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/542f1dc6b93941cd8751f32a5471bfa6.png?imageMogr2/thumbnail/!1196x814r|imageMogr2/gravity/Center/crop/1196x814/interlace/1/quality/60)
(5)生成代码工程
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/7c3d478a52c34abf94697880727a0ced.png?imageMogr2/thumbnail/!1056x134r|imageMogr2/gravity/Center/crop/1056x134/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/ed45fddf9ef8494a8cdfd4497eb79858.png?imageMogr2/thumbnail/!1082x960r|imageMogr2/gravity/Center/crop/1082x960/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/ecbc61d94c854145a485919d0c140259.png?imageMogr2/thumbnail/!1196x834r|imageMogr2/gravity/Center/crop/1196x834/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/c315095ab8ea4b46a4071fa9cc2b283b.png?imageMogr2/thumbnail/!1148x698r|imageMogr2/gravity/Center/crop/1148x698/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/b9186d8b0c8c43b8a676201bb5279fc0.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
4.代码移植
(1)复制文件
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/269f5010a9b3438ca46fbd71be59d7a9.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/e4f7d965526641ec833e63ddca8c70e5.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
(2)在Keil5里添加文件
(3)修改oled.h文件
![](https://cdn.chandianzi.cn/wp-content/uploads/csstm/512feedbe5d24536ad5ce6f8463745aa.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
//--------------------------------给各位复制的代码
//--------------------------------给各位复制的代码
//--------------------------------给各位复制的代码
#ifndef __OLED_H
#define __OLED_H
#include "main.h"
#include "stdlib.h"
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//-----------------OLED端口定义----------------
//#define OLED_SCL_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_0)//SCL
//#define OLED_SCL_Set() GPIO_SetBits(GPIOA,GPIO_Pin_0)
//#define OLED_SDA_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_1)//SDA
//#define OLED_SDA_Set() GPIO_SetBits(GPIOA,GPIO_Pin_1)
//#define OLED_RES_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_2)//RES
//#define OLED_RES_Set() GPIO_SetBits(GPIOA,GPIO_Pin_2)
//#define OLED_DC_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_3)//DC
//#define OLED_DC_Set() GPIO_SetBits(GPIOA,GPIO_Pin_3)
//
//#define OLED_CS_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_4)//CS
//#define OLED_CS_Set() GPIO_SetBits(GPIOA,GPIO_Pin_4)
//****************复位*********************
#define OLED_RES_Clr() HAL_GPIO_WritePin(OLED_RES_GPIO_Port,OLED_RES_Pin, GPIO_PIN_RESET)
#define OLED_RES_Set() HAL_GPIO_WritePin(OLED_RES_GPIO_Port,OLED_RES_Pin, GPIO_PIN_SET)
//****************数据/命令***************
#define OLED_DC_Clr() HAL_GPIO_WritePin(OLED_DC_GPIO_Port,OLED_DC_Pin, GPIO_PIN_RESET)
#define OLED_DC_Set() HAL_GPIO_WritePin(OLED_DC_GPIO_Port,OLED_DC_Pin, GPIO_PIN_SET)
//****************片选*********************
#define OLED_CS_Clr() HAL_GPIO_WritePin(OLED_CS_GPIO_Port,OLED_CS_Pin, GPIO_PIN_RESET)
#define OLED_CS_Set() HAL_GPIO_WritePin(OLED_CS_GPIO_Port,OLED_CS_Pin, GPIO_PIN_SET)
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
(4)修改oled.c文件
添加:
#include "spi.h"
添加:
HAL_SPI_Transmit(&hspi2,&dat,1, 1000);
完成到这里,此时编译不会报错
(5)修改main.c函数
添加:
#include "oled.h"
#include "bmp.h"
添加:
u8 t=' ';
OLED_Init();
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
//--------------------------给各位复制的代码
//--------------------------给各位复制的代码
//--------------------------给各位复制的代码
OLED_ShowPicture(0,0,128,64,BMP1,1);
OLED_Refresh();
HAL_Delay(200);
OLED_Clear();
OLED_ShowChinese(0,0,0,16,1);//中
OLED_ShowChinese(18,0,1,16,1);//景
OLED_ShowChinese(36,0,2,16,1);//园
OLED_ShowChinese(54,0,3,16,1);//电
OLED_ShowChinese(72,0,4,16,1);//子
OLED_ShowChinese(90,0,5,16,1);//技
OLED_ShowChinese(108,0,6,16,1);//术
OLED_ShowString(8,16,"ZHONGJINGYUAN",16,1);
OLED_ShowString(20,32,"2014/05/01",16,1);
OLED_ShowString(0,48,"ASCII:",16,1);
OLED_ShowString(63,48,"CODE:",16,1);
OLED_ShowChar(48,48,t,16,1);//显示ASCII字符
t++;
if(t>'~')t=' ';
OLED_ShowNum(103,48,t,3,16,1);
OLED_Refresh();
HAL_Delay(200);
OLED_Clear();
OLED_ShowChinese(0,0,0,16,1); //16*16 中
OLED_ShowChinese(16,0,0,24,1); //24*24 中
OLED_ShowChinese(24,20,0,32,1);//32*32 中
OLED_ShowChinese(64,0,0,64,1); //64*64 中
OLED_Refresh();
HAL_Delay(200);
OLED_Clear();
OLED_ShowString(0,0,"ABC",8,1);//6*8 “ABC”
OLED_ShowString(0,8,"ABC",12,1);//6*12 “ABC”
OLED_ShowString(0,20,"ABC",16,1);//8*16 “ABC”
OLED_ShowString(0,36,"ABC",24,1);//12*24 “ABC”
OLED_Refresh();
HAL_Delay(200);
OLED_ScrollDisplay(11,4,1);
(6).代码下载
到这一步,如果编译没有错误就可以自行下载程序了。如果有错,就是哪里自己搞错了,回去重新仔细看一下。
接线: (如引脚未按照上述例程配置,则自行通过自己映射的引脚接线即可)
5.最终效果
本例程源码下载:点击跳转
版权声明:本文为CSDN博主「昨天忘签到了」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lwb450921/article/details/121866668
目录
1.软件准备
(1)编程平台:Keil5
(2)CubeMX
(3)中景园移植代码——点我下载 提取码:6666
2.硬件准备
(1)F1的板子,本例使用经典F103C8T6
(2)0.96'/1.3' OLED屏幕模块(7针,SSD1306驱动)
(3)ST-link 下载器
(4)杜邦线若干
3.CubeMX配置
(1)芯片选择
(2)配置RCC、SYS、时钟树
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/75f2d412d7b3414ea5fcbb61d69b6711.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/f5f718bdc5454a00bbd4005b372048c0.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/7cc04c5897cd48dd94ec73a6959018c0.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
(3)配置SPI
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/f273e5424a974224a458fbda14759319.png?imageMogr2/thumbnail/!1338x1152r|imageMogr2/gravity/Center/crop/1338x1152/interlace/1/quality/60)
(4)配置GPIO
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/542f1dc6b93941cd8751f32a5471bfa6.png?imageMogr2/thumbnail/!1196x814r|imageMogr2/gravity/Center/crop/1196x814/interlace/1/quality/60)
(5)生成代码工程
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/7c3d478a52c34abf94697880727a0ced.png?imageMogr2/thumbnail/!1056x134r|imageMogr2/gravity/Center/crop/1056x134/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/ed45fddf9ef8494a8cdfd4497eb79858.png?imageMogr2/thumbnail/!1082x960r|imageMogr2/gravity/Center/crop/1082x960/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/ecbc61d94c854145a485919d0c140259.png?imageMogr2/thumbnail/!1196x834r|imageMogr2/gravity/Center/crop/1196x834/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/c315095ab8ea4b46a4071fa9cc2b283b.png?imageMogr2/thumbnail/!1148x698r|imageMogr2/gravity/Center/crop/1148x698/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/b9186d8b0c8c43b8a676201bb5279fc0.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
4.代码移植
(1)复制文件
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/269f5010a9b3438ca46fbd71be59d7a9.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/e4f7d965526641ec833e63ddca8c70e5.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
(2)在Keil5里添加文件
(3)修改oled.h文件
![](https://cdn.chandianzi.cn/wp-content/uploads/csmcu/512feedbe5d24536ad5ce6f8463745aa.png?imageMogr2/thumbnail/2048x0/interlace/1/quality/60)
//--------------------------------给各位复制的代码
//--------------------------------给各位复制的代码
//--------------------------------给各位复制的代码
#ifndef __OLED_H
#define __OLED_H
#include "main.h"
#include "stdlib.h"
#define u8 uint8_t
#define u16 uint16_t
#define u32 uint32_t
//-----------------OLED端口定义----------------
//#define OLED_SCL_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_0)//SCL
//#define OLED_SCL_Set() GPIO_SetBits(GPIOA,GPIO_Pin_0)
//#define OLED_SDA_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_1)//SDA
//#define OLED_SDA_Set() GPIO_SetBits(GPIOA,GPIO_Pin_1)
//#define OLED_RES_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_2)//RES
//#define OLED_RES_Set() GPIO_SetBits(GPIOA,GPIO_Pin_2)
//#define OLED_DC_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_3)//DC
//#define OLED_DC_Set() GPIO_SetBits(GPIOA,GPIO_Pin_3)
//
//#define OLED_CS_Clr() GPIO_ResetBits(GPIOA,GPIO_Pin_4)//CS
//#define OLED_CS_Set() GPIO_SetBits(GPIOA,GPIO_Pin_4)
//****************复位*********************
#define OLED_RES_Clr() HAL_GPIO_WritePin(OLED_RES_GPIO_Port,OLED_RES_Pin, GPIO_PIN_RESET)
#define OLED_RES_Set() HAL_GPIO_WritePin(OLED_RES_GPIO_Port,OLED_RES_Pin, GPIO_PIN_SET)
//****************数据/命令***************
#define OLED_DC_Clr() HAL_GPIO_WritePin(OLED_DC_GPIO_Port,OLED_DC_Pin, GPIO_PIN_RESET)
#define OLED_DC_Set() HAL_GPIO_WritePin(OLED_DC_GPIO_Port,OLED_DC_Pin, GPIO_PIN_SET)
//****************片选*********************
#define OLED_CS_Clr() HAL_GPIO_WritePin(OLED_CS_GPIO_Port,OLED_CS_Pin, GPIO_PIN_RESET)
#define OLED_CS_Set() HAL_GPIO_WritePin(OLED_CS_GPIO_Port,OLED_CS_Pin, GPIO_PIN_SET)
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
(4)修改oled.c文件
添加:
#include "spi.h"
添加:
HAL_SPI_Transmit(&hspi2,&dat,1, 1000);
完成到这里,此时编译不会报错
(5)修改main.c函数
添加:
#include "oled.h"
#include "bmp.h"
添加:
u8 t=' ';
OLED_Init();
OLED_ColorTurn(0);//0正常显示,1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
//--------------------------给各位复制的代码
//--------------------------给各位复制的代码
//--------------------------给各位复制的代码
OLED_ShowPicture(0,0,128,64,BMP1,1);
OLED_Refresh();
HAL_Delay(200);
OLED_Clear();
OLED_ShowChinese(0,0,0,16,1);//中
OLED_ShowChinese(18,0,1,16,1);//景
OLED_ShowChinese(36,0,2,16,1);//园
OLED_ShowChinese(54,0,3,16,1);//电
OLED_ShowChinese(72,0,4,16,1);//子
OLED_ShowChinese(90,0,5,16,1);//技
OLED_ShowChinese(108,0,6,16,1);//术
OLED_ShowString(8,16,"ZHONGJINGYUAN",16,1);
OLED_ShowString(20,32,"2014/05/01",16,1);
OLED_ShowString(0,48,"ASCII:",16,1);
OLED_ShowString(63,48,"CODE:",16,1);
OLED_ShowChar(48,48,t,16,1);//显示ASCII字符
t++;
if(t>'~')t=' ';
OLED_ShowNum(103,48,t,3,16,1);
OLED_Refresh();
HAL_Delay(200);
OLED_Clear();
OLED_ShowChinese(0,0,0,16,1); //16*16 中
OLED_ShowChinese(16,0,0,24,1); //24*24 中
OLED_ShowChinese(24,20,0,32,1);//32*32 中
OLED_ShowChinese(64,0,0,64,1); //64*64 中
OLED_Refresh();
HAL_Delay(200);
OLED_Clear();
OLED_ShowString(0,0,"ABC",8,1);//6*8 “ABC”
OLED_ShowString(0,8,"ABC",12,1);//6*12 “ABC”
OLED_ShowString(0,20,"ABC",16,1);//8*16 “ABC”
OLED_ShowString(0,36,"ABC",24,1);//12*24 “ABC”
OLED_Refresh();
HAL_Delay(200);
OLED_ScrollDisplay(11,4,1);
(6).代码下载
到这一步,如果编译没有错误就可以自行下载程序了。如果有错,就是哪里自己搞错了,回去重新仔细看一下。
接线: (如引脚未按照上述例程配置,则自行通过自己映射的引脚接线即可)
5.最终效果
本例程源码下载:点击跳转
版权声明:本文为CSDN博主「昨天忘签到了」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lwb450921/article/details/121866668
暂无评论