文章目录

    • 一:OLED调试工具
      • 1.1 OLED显示屏介绍
      • 1.2 实验:在OLED显示屏的使用
      • 1.3 自己新增功能测试
      • 道友:今天没有开始的事,明天绝不会完成。

一:OLED调试工具

1.1 OLED显示屏介绍

学习任何一门语言就需要进行调试,stm32调试方式主要有以下三种:

  1. 串口调试:通过串口,将调试信息发送到电脑端,电脑端使用串口调试助手显示调试信息。
  2. 显示屏调试:直接将显示屏连接到单片机,将调试信息打印在显示屏上。
  3. Keil调试模式:借助Keil软件的调试模式,可使用单步运行、设置断点、查看寄存器及变量等功能。
  4. 点灯调试法:在程序的某个关键位置设置点灯的代码,用于指示程序是否运行到该位置。
  5. 注释调试法:将可能有问题的代码块全部注释掉,然后一个功能一个功能的去掉注释,找出问题所在。

该实验暂时只要会使用驱动函数模块,代码的原理UP后续会补充讲解。


上图是各种各样的OLED,下图是OLED硬件电路

OLED(Organic Light Emitting Diode)意为 有机发光二极管。由OLED构成的OLED显示屏是一款性能优异的新型显示屏,其每一个像素都是一个单独的发光二极管,每个像素都可以自发光,所以具有功耗低、响应速度快、宽视角、轻薄柔韧等特点。本实验采用0.96寸OLED模块,其小巧玲珑、占用接口少、简单易用,是电子设计中非常常见的显示屏模块,下面是一些基本参数:

  • 供电:3~5.5V.
  • 通信协议:4针引脚常用I2C,7针引脚常用SPI。
  • 如上图所示。 分辨率:12864(宽高)

与“51单片机”的教程相同,为了方便调试,UP主提供了stm32驱动OLED屏幕的代码以供前期调试使用,后面会专门介绍OLED代码的书写。首先UP主将屏幕分成了 16 * 4个区域,每个区域大小是8 * 16个像素,然后在此基础上,调用下表所示代码即可。

函数作用
OLED_Init();初始化
OLED_Clear();清屏
OLED_ShowChar(1,1,‘A’);显示一个字符
OLED_ShowString(1,3,“HelloWorld”);显示字符串
OLED_ShowNum(2,1,12345,5);显示十进制数字
OLED_ShowSignedNum(2,7,-66,2);显示有符号十进制数字
OLED_ShowHexNum(3,1,0xAA55,4);显示十六进制数字
OLED_ShowBinNum(4,1,0xAA55,16);显示二进制数字

1.2 实验:在OLED显示屏的使用

需求:在OLED显示屏上,显示如下图内容:

上图是OLED显示实验现象

上图是OLED显示接线图

OLED显示屏有两种供电方式
第一是把OLED的GND和VCC直接接在面包板上的正负极()
第二是初始化PB6和PB7,让B6和B7供电,因为OLED功率小,能实现但不规范

  1. 上图的OLED显示屏背面的GND和VCC一定要外接面包板的vcc和GND,不然可能显示屏会显示内容不全,还有直接不亮。
  2. 把PB67PB7作为GPIO输出电平,给显示屏提供电平需要初始化输出GPIO 高低电平(输出1和0就能供电)
    网友:试了用PB6PB7供电,但是显示屏会显示不正确,有部分错误显示

上图是OLED显示的代码调用结构图,不包含库函数
看一下注释,这是OLED的字模库,每个数字或字母都以16进制放进了函数内,不用管数字怎么转换成二进制,有特定的字模软件

下面是代码展示:
main.c

#include "stm32f10x.h"// Device header#include "Delay.h"#include "OLED.h"int main(void){/*模块初始化*/OLED_Init();//OLED初始化/*OLED显示*/OLED_ShowChar(1, 1, 'A');//1行1列显示字符AOLED_ShowString(1, 3, "HelloWorld!");//1行3列显示字符串HelloWorld!OLED_ShowNum(2, 1, 12345, 5);//2行1列显示十进制数字12345,长度为5OLED_ShowSignedNum(2, 7, -66, 2);//2行7列显示有符号十进制数字-66,长度为2OLED_ShowHexNum(3, 1, 0xAA55, 4);//3行1列显示十六进制数字0xA5A5,长度为4OLED_ShowBinNum(4, 1, 0xAA55, 16);//4行1列显示二进制数字0xA5A5,长度为16//C语言无法直接写出二进制数字,故需要用十六进制表示while (1){}}

OLED_Font.h
看一下注释,这是OLED的字模库,每个数字或字母都以16进制放进了函数内,不用管数字怎么转换成二进制,有特定的字模软件

#ifndef __OLED_FONT_H#define __OLED_FONT_H/*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,//00x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 10x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 20x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 30x00,0x70,0x88,0x*FC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 40xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 50x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 60x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 70x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 80x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 90x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 100x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 110x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 120x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 130x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 140x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 150x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 160x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 170x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 180x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 190x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 200x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 210x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 220x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 230x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 240x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 250x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 260x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 270x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 280x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 290x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 300x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//" />0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 320x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 330x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 340xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 350x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 360x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 370x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 380xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 390x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 400x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 410x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 420x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 430x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 440x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 450x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 460xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 470x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 480xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 490x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 500x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 510x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 520x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 530x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 540xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 550x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 560x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 570x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 580x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 590x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 600x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 610x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 620x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 630x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 640x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 650x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 660x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 670x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 680x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 690x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 700x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 710x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 720x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 730x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 740x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 750x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 760x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 770x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 780x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 790x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 800x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 810x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 820x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 830x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 840x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 850x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 860x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 870x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 880x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 890x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 900x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 910x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 920x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 930x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94};#endif

OLED.c

  • 因为这个写的屏幕驱动使用的是I2C总线, I2C协议要求必须开漏输出GPIO_Mode_OUT_OD
  • I2C多机通信情况下,利用开漏输出的线与来避免设备间相互干扰。但是这里只有一个屏幕,推挽输出应该也没问题
#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;//因为这个写的屏幕驱动使用的是I2C总线, I2C协议要求必须开漏输出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);}/*** @briefI2C开始* @param无* @retval 无*/void OLED_I2C_Start(void){OLED_W_SDA(1);OLED_W_SCL(1);OLED_W_SDA(0);OLED_W_SCL(0);}/*** @briefI2C停止* @param无* @retval 无*/void OLED_I2C_Stop(void){OLED_W_SDA(0);OLED_W_SCL(1);OLED_W_SDA(1);}/*** @briefI2C发送一个字节* @paramByte 要发送的一个字节* @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);}/*** @briefOLED写命令* @paramCommand 要写入的命令* @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();}/*** @briefOLED写数据* @paramData 要写入的数据* @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();}/*** @briefOLED设置光标位置* @paramY 以左上角为原点,向下方向的坐标,范围:0~7* @paramX 以左上角为原点,向右方向的坐标,范围: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位}/*** @briefOLED清屏* @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);}}}/*** @briefOLED显示一个字符* @paramLine 行位置,范围:1~4* @paramColumn 列位置,范围:1~16* @paramChar 要显示的一个字符,范围: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]);//显示下半部分内容}}/*** @briefOLED显示字符串* @paramLine 起始行位置,范围:1~4* @paramColumn 起始列位置,范围:1~16* @paramString 要显示的字符串,范围: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]);}}/*** @briefOLED次方函数* @retval 返回值等于X的Y次方*/uint32_t OLED_Pow(uint32_t X, uint32_t Y){uint32_t Result = 1;while (Y--){Result *= X;}return Result;}/*** @briefOLED显示数字(十进制,正数)* @paramLine 起始行位置,范围:1~4* @paramColumn 起始列位置,范围:1~16* @paramNumber 要显示的数字,范围:0~4294967295* @paramLength 要显示数字的长度,范围: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');}}/*** @briefOLED显示数字(十进制,带符号数)* @paramLine 起始行位置,范围:1~4* @paramColumn 起始列位置,范围:1~16* @paramNumber 要显示的数字,范围:-2147483648~2147483647* @paramLength 要显示数字的长度,范围: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');}}/*** @briefOLED显示数字(十六进制,正数)* @paramLine 起始行位置,范围:1~4* @paramColumn 起始列位置,范围:1~16* @paramNumber 要显示的数字,范围:0~0xFFFFFFFF* @paramLength 要显示数字的长度,范围: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');}}}/*** @briefOLED显示数字(二进制,正数)* @paramLine 起始行位置,范围:1~4* @paramColumn 起始列位置,范围:1~16* @paramNumber 要显示的数字,范围:0~1111 1111 1111 1111* @paramLength 要显示数字的长度,范围: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');}}/*** @briefOLED初始化* @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

#ifndef __OLED_H#define __OLED_Hvoid 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);#endif

编程感想:

  1. OLED的供电。由于stm32引脚不仅初始化默认是浮空输入的模式,所以可以直接把B6和B7引脚接在电源上,用于给OLED进行供电。
  2. 视频分为两部分,前面讲了OLED显示内容,后半段只演示了“Keil的调试模式”,暂时看不懂,哈哈。

1.3 自己新增功能测试

下面是自己做了一些小调整,加了延时函数+清屏函数+在while(1){}里面循环执行某个字符

效果是:显示屏首先先按顺序显示内容,倒数第二是屏幕突然黑了,最后只有一个字符A停留在显示屏上。

道友:今天没有开始的事,明天绝不会完成。