嵌入式-stm32-江科大-OLED调试工具

时间:2024-01-25 21:17:03

文章目录

    • 一: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列显示字符A
	
	OLED_ShowString(1, 3, "HelloWorld!");	//1行3列显示字符串HelloWorld!
	
	OLED_ShowNum(2, 1, 12345, 5);			//2行1列显示十进制数字12345,长度为5
	
	OLED_ShowSignedNum(2, 7, -66, 2);		//2行7列显示有符号十进制数字-66,长度为2
	
	OLED_ShowHexNum(3, 1, 0xAA55, 4);		//3行1列显示十六进制数字0xA5A5,长度为4
	
	OLED_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,//  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,0x*FC,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,