STM32—4线SPI驱动SSD1306 OLED

一.OLED简介

OLED,即有机发光二极管(Organic Light-Emitting Diode),又称为有机电激光显示(OrganicElectroluminesence Display, OELD)。 OLED 由于同时具备自发光,不需背光源、对比度高、厚度薄、视角广、反应速度快、可用于挠曲性面板、使用温度范围广、构造及制程较简单等优异之特性,被认为是下一代的平面显示器新兴应用技术。
模块特点:

  • 没有背光,需要通过写入指令来开启显示
  • 尺寸小:0.96寸,分辨率高:128*64
  • 提供多种通信接口:6800、8080、3线/4线SPI、IIC
  • 供电3.3V

需要改变模块的通信接口时,只需要改变模块背后的电阻位置即可,如示:
STM32—4线SPI驱动SSD1306 OLED
我是使用4线SPI驱动OLED的。

二.驱动SSD1306所需知识

1.引脚介绍

STM32—4线SPI驱动SSD1306 OLED
如图所示,各引脚的介绍如下:

  • GND:接地引脚
  • VCC:接电源引脚,接3.3V直流电源即可
  • D0:SPI的时钟线SCLK
  • D1:SPI的数据线SDIN
  • RES:复位接口,低电平初始化(复位),正常时高电平
  • DC:用来选择命令or数据,低电平命令,高电平数据
  • CS:片选线

注意:4线SPI模式时,只能写,不能读!
在本文的代码中,对应的接口如下:

SCLK:PC0 D0
SDIN:PC1 D1
RES :PG15 拉低时初始化(复位),正常时拉高
DC :PD3 (0:命令 1:数据)
CS :PD6

2.通信时序

单片机与OLED通信是4线SPI,每个数据长度为8位,在SCLK的上升沿数据有效,在上升沿数据从SDIN移入SSD1306,高位先行,D/C用来说明是传输命令还是数据。其通信时序图如示:
STM32—4线SPI驱动SSD1306 OLED
由图知,SSD1306与单片机通信可以分为如下几步:
1.拉低片选CS,作为正常通信的前提
2.根据需求操作D/C,低电平代表传输命令,高电平代表传输数据
3.时钟线SCLK不断产生时钟信号,上升沿数据线有效
4.数据线SDIN根据SCLK的上升沿,从高位开始传输一帧数据(8位),置高代表1,拉低代表0

3.显存GRAM

在OLED上显示图像,图像的数据就存储在显存GRAM中,由于SSD1306的分辨率是128*64,代表屏幕上总共有128x64个像素点,所以其显存大小就是128x64bit,恰好可以存储一帧图像的信息。
SSD1306的在逻辑上把GRAM分为8页,每页包含128个字节(1个字节8位),即8x128x8=128x64,所以屏幕上的每一个像素点都对应着GRAM中的每一位,要想点亮屏幕上的某一个像素点,只要在开启显示的前提下,将GRAM中的相应位置一即可。
SSD1306的GRAM与屏幕上像素点的对应关系表如示:
STM32—4线SPI驱动SSD1306 OLED
以屏幕的左上角为原点,可以想象出一副坐标系,屏幕的大小范围就是x:0~ 127,y:0~64,实际上我们后续画点也是以此为基础的!
我们用一个二维数组来抽象的映射出屏幕上的每一个像素点:

/* OLED的显存 */
uint8_t OLED_GRAM[128][8];
/*
OLED_GRAM[128][8]中是8位二进制为一个单位
其逻辑排序如示:(高位先行)
7 7
6 6
5 5
4 4 ...
3 3
2 2
1 1
0 0
*/

到此,我们只需要把要显示图像的信息写入OLED_GRAM[128][8]数组中,再通过画点函数操作OLED_GRAM[128][8]中的每一位对应的像素点,就可以在OLED屏幕中显示图像了。

4.字库

要在屏幕中显示图像,最常见的是显示一些字符,这个时候就需要这些字符的图像信息了,这就涉及到字库。我理解,一个包含了一些字符图像的数组就可以称作字库,这样做的好处就是,可以根据所要显示的字符直接从字库中提取图像信息到GRAM,这样显示字符就方便多了。
本项目所使用的字库大小有(高x宽):12x6、16x8、24x12
而且字库的取模方式:阴码+逐列式+顺向+C51格式(用了取模软件PC2LCD2002)
每个字符所占用的字节数为:(size/8+((size%8)?1:0))*(size/2),其中size:是字库生成时的点阵大小
比如12x6字库信息如示:

//12*6 ASCII字符集点阵
const unsigned char asc2_1206[95][12]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x3F,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
{0x00,0x00,0x30,0x00,0x40,0x00,0x30,0x00,0x40,0x00,0x00,0x00},/*""",2*/
{0x09,0x00,0x0B,0xC0,0x3D,0x00,0x0B,0xC0,0x3D,0x00,0x09,0x00},/*"#",3*/
{0x18,0xC0,0x24,0x40,0x7F,0xE0,0x22,0x40,0x31,0x80,0x00,0x00},/*"$",4*/
{0x18,0x00,0x24,0xC0,0x1B,0x00,0x0D,0x80,0x32,0x40,0x01,0x80},/*"%",5*/
{0x03,0x80,0x1C,0x40,0x27,0x40,0x1C,0x80,0x07,0x40,0x00,0x40},/*"&",6*/
{0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x80,0x20,0x40,0x40,0x20},/*"(",8*/
{0x00,0x00,0x40,0x20,0x20,0x40,0x1F,0x80,0x00,0x00,0x00,0x00},/*")",9*/
{0x09,0x00,0x06,0x00,0x1F,0x80,0x06,0x00,0x09,0x00,0x00,0x00},/*"*",10*/
{0x04,0x00,0x04,0x00,0x3F,0x80,0x04,0x00,0x04,0x00,0x00,0x00},/*"+",11*/
{0x00,0x10,0x00,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
{0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x00,0x00},/*"-",13*/
{0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
{0x00,0x20,0x01,0xC0,0x06,0x00,0x38,0x00,0x40,0x00,0x00,0x00},/*"/",15*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"0",16*/
{0x00,0x00,0x10,0x40,0x3F,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"1",17*/
{0x18,0xC0,0x21,0x40,0x22,0x40,0x24,0x40,0x18,0x40,0x00,0x00},/*"2",18*/
{0x10,0x80,0x20,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"3",19*/
{0x02,0x00,0x0D,0x00,0x11,0x00,0x3F,0xC0,0x01,0x40,0x00,0x00},/*"4",20*/
{0x3C,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x23,0x80,0x00,0x00},/*"5",21*/
{0x1F,0x80,0x24,0x40,0x24,0x40,0x34,0x40,0x03,0x80,0x00,0x00},/*"6",22*/
{0x30,0x00,0x20,0x00,0x27,0xC0,0x38,0x00,0x20,0x00,0x00,0x00},/*"7",23*/
{0x1B,0x80,0x24,0x40,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"8",24*/
{0x1C,0x00,0x22,0xC0,0x22,0x40,0x22,0x40,0x1F,0x80,0x00,0x00},/*"9",25*/
{0x00,0x00,0x00,0x00,0x08,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
{0x00,0x00,0x00,0x00,0x04,0x60,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
{0x00,0x00,0x04,0x00,0x0A,0x00,0x11,0x00,0x20,0x80,0x40,0x40},/*"<",28*/
{0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x09,0x00,0x00,0x00},/*"=",29*/
{0x00,0x00,0x40,0x40,0x20,0x80,0x11,0x00,0x0A,0x00,0x04,0x00},/*">",30*/
{0x18,0x00,0x20,0x00,0x23,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"?",31*/
{0x1F,0x80,0x20,0x40,0x27,0x40,0x29,0x40,0x1F,0x40,0x00,0x00},/*"@",32*/
{0x00,0x40,0x07,0xC0,0x39,0x00,0x0F,0x00,0x01,0xC0,0x00,0x40},/*"A",33*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x40,0x1B,0x80,0x00,0x00},/*"B",34*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x30,0x80,0x00,0x00},/*"C",35*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"D",36*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x40,0x30,0xC0,0x00,0x00},/*"E",37*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x2E,0x00,0x30,0x00,0x00,0x00},/*"F",38*/
{0x0F,0x00,0x10,0x80,0x20,0x40,0x22,0x40,0x33,0x80,0x02,0x00},/*"G",39*/
{0x20,0x40,0x3F,0xC0,0x04,0x00,0x04,0x00,0x3F,0xC0,0x20,0x40},/*"H",40*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x20,0x40,0x20,0x40,0x00,0x00},/*"I",41*/
{0x00,0x60,0x20,0x20,0x20,0x20,0x3F,0xC0,0x20,0x00,0x20,0x00},/*"J",42*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x0B,0x00,0x30,0xC0,0x20,0x40},/*"K",43*/
{0x20,0x40,0x3F,0xC0,0x20,0x40,0x00,0x40,0x00,0x40,0x00,0xC0},/*"L",44*/
{0x3F,0xC0,0x3C,0x00,0x03,0xC0,0x3C,0x00,0x3F,0xC0,0x00,0x00},/*"M",45*/
{0x20,0x40,0x3F,0xC0,0x0C,0x40,0x23,0x00,0x3F,0xC0,0x20,0x00},/*"N",46*/
{0x1F,0x80,0x20,0x40,0x20,0x40,0x20,0x40,0x1F,0x80,0x00,0x00},/*"O",47*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x24,0x00,0x18,0x00,0x00,0x00},/*"P",48*/
{0x1F,0x80,0x21,0x40,0x21,0x40,0x20,0xE0,0x1F,0xA0,0x00,0x00},/*"Q",49*/
{0x20,0x40,0x3F,0xC0,0x24,0x40,0x26,0x00,0x19,0xC0,0x00,0x40},/*"R",50*/
{0x18,0xC0,0x24,0x40,0x24,0x40,0x22,0x40,0x31,0x80,0x00,0x00},/*"S",51*/
{0x30,0x00,0x20,0x40,0x3F,0xC0,0x20,0x40,0x30,0x00,0x00,0x00},/*"T",52*/
{0x20,0x00,0x3F,0x80,0x00,0x40,0x00,0x40,0x3F,0x80,0x20,0x00},/*"U",53*/
{0x20,0x00,0x3E,0x00,0x01,0xC0,0x07,0x00,0x38,0x00,0x20,0x00},/*"V",54*/
{0x38,0x00,0x07,0xC0,0x3C,0x00,0x07,0xC0,0x38,0x00,0x00,0x00},/*"W",55*/
{0x20,0x40,0x39,0xC0,0x06,0x00,0x39,0xC0,0x20,0x40,0x00,0x00},/*"X",56*/
{0x20,0x00,0x38,0x40,0x07,0xC0,0x38,0x40,0x20,0x00,0x00,0x00},/*"Y",57*/
{0x30,0x40,0x21,0xC0,0x26,0x40,0x38,0x40,0x20,0xC0,0x00,0x00},/*"Z",58*/
{0x00,0x00,0x00,0x00,0x7F,0xE0,0x40,0x20,0x40,0x20,0x00,0x00},/*"[",59*/
{0x00,0x00,0x70,0x00,0x0C,0x00,0x03,0x80,0x00,0x40,0x00,0x00},/*"\",60*/
{0x00,0x00,0x40,0x20,0x40,0x20,0x7F,0xE0,0x00,0x00,0x00,0x00},/*"]",61*/
{0x00,0x00,0x20,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00},/*"^",62*/
{0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x10},/*"_",63*/
{0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
{0x00,0x00,0x02,0x80,0x05,0x40,0x05,0x40,0x03,0xC0,0x00,0x40},/*"a",65*/
{0x20,0x00,0x3F,0xC0,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"b",66*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x06,0x40,0x00,0x00},/*"c",67*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x24,0x40,0x3F,0xC0,0x00,0x40},/*"d",68*/
{0x00,0x00,0x03,0x80,0x05,0x40,0x05,0x40,0x03,0x40,0x00,0x00},/*"e",69*/
{0x00,0x00,0x04,0x40,0x1F,0xC0,0x24,0x40,0x24,0x40,0x20,0x00},/*"f",70*/
{0x00,0x00,0x02,0xE0,0x05,0x50,0x05,0x50,0x06,0x50,0x04,0x20},/*"g",71*/
{0x20,0x40,0x3F,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"h",72*/
{0x00,0x00,0x04,0x40,0x27,0xC0,0x00,0x40,0x00,0x00,0x00,0x00},/*"i",73*/
{0x00,0x10,0x00,0x10,0x04,0x10,0x27,0xE0,0x00,0x00,0x00,0x00},/*"j",74*/
{0x20,0x40,0x3F,0xC0,0x01,0x40,0x07,0x00,0x04,0xC0,0x04,0x40},/*"k",75*/
{0x20,0x40,0x20,0x40,0x3F,0xC0,0x00,0x40,0x00,0x40,0x00,0x00},/*"l",76*/
{0x07,0xC0,0x04,0x00,0x07,0xC0,0x04,0x00,0x03,0xC0,0x00,0x00},/*"m",77*/
{0x04,0x40,0x07,0xC0,0x04,0x40,0x04,0x00,0x03,0xC0,0x00,0x40},/*"n",78*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x40,0x03,0x80,0x00,0x00},/*"o",79*/
{0x04,0x10,0x07,0xF0,0x04,0x50,0x04,0x40,0x03,0x80,0x00,0x00},/*"p",80*/
{0x00,0x00,0x03,0x80,0x04,0x40,0x04,0x50,0x07,0xF0,0x00,0x10},/*"q",81*/
{0x04,0x40,0x07,0xC0,0x02,0x40,0x04,0x00,0x04,0x00,0x00,0x00},/*"r",82*/
{0x00,0x00,0x06,0x40,0x05,0x40,0x05,0x40,0x04,0xC0,0x00,0x00},/*"s",83*/
{0x00,0x00,0x04,0x00,0x1F,0x80,0x04,0x40,0x00,0x40,0x00,0x00},/*"t",84*/
{0x04,0x00,0x07,0x80,0x00,0x40,0x04,0x40,0x07,0xC0,0x00,0x40},/*"u",85*/
{0x04,0x00,0x07,0x00,0x04,0xC0,0x01,0x80,0x06,0x00,0x04,0x00},/*"v",86*/
{0x06,0x00,0x01,0xC0,0x07,0x00,0x01,0xC0,0x06,0x00,0x00,0x00},/*"w",87*/
{0x04,0x40,0x06,0xC0,0x01,0x00,0x06,0xC0,0x04,0x40,0x00,0x00},/*"x",88*/
{0x04,0x10,0x07,0x10,0x04,0xE0,0x01,0x80,0x06,0x00,0x04,0x00},/*"y",89*/
{0x00,0x00,0x04,0x40,0x05,0xC0,0x06,0x40,0x04,0x40,0x00,0x00},/*"z",90*/
{0x00,0x00,0x00,0x00,0x04,0x00,0x7B,0xE0,0x40,0x20,0x00,0x00},/*"{",91*/
{0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xF0,0x00,0x00,0x00,0x00},/*"|",92*/
{0x00,0x00,0x40,0x20,0x7B,0xE0,0x04,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
{0x40,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x20,0x00,0x40,0x00},/*"~",94*/
};

5.SSD1306基本命令

上面介绍了显存,我们已经知道OLED的图像显示原理了。然鹅,在显示图像之前,还要对SSD1306做一系列的初始化操作,这就用到一些相关指令了。

暂停一下!!!捋一下命令与数据的关系:
上面介绍过D/C线,D/C低电平代表传输命令,高电平代表传输数据。数据就是显存GRAM的内容,而命令就是下面要介绍的。所以OLED与单片机通信就是由单片机向OLED写入信息,开始写入命令来初始化,之后写入GRAM(图像的信息)即可!

一些常用的命令如图所示:
STM32—4线SPI驱动SSD1306 OLED
比较重要的是设置起始坐标的三个命令
命令为 0XB0~B7,该命令用于设置页地址,其低三位的值对应着 GRAM 的页地址。
命令为 0X00~0X0F,该指令用于设置显示时的起始列地址低四位。
命令为 0X10~0X1F,该指令用于设置显示时的起始列地址高四位。

有关初始化OLED的所有命令会在下午代码讲解部分列出!

三.代码讲解

1.相关引脚配置

上面介绍了相应的引脚,这里对相应引脚进行配置,注意:除了GND和VCC引脚,其余所有引脚的模式均为推挽输出,代码如下:

/*
配置4线SPI模式的所有引脚
4线SPI模式只能写,不能读
SCLK:PC0 D0
SDIN:PC1 D1
RES :PG15 拉低时初始化(复位),正常时拉高
DC :PD3 (0:命令 1:数据)
CS :PD6
*/
void OLED_GPIO_Config( void )
{
GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOG, ENABLE ); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOC, &GPIO_InitStruct ); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_6;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOD, &GPIO_InitStruct ); GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init( GPIOG, &GPIO_InitStruct );
}

由通信时序知,通信的过程中需要不断改变引脚的电平,所以我写了一些对引脚电平操作的函数(格式都一样):

void OLED_SCLK( uint8_t x )
{
if( x==1 )
GPIO_SetBits( GPIOC, GPIO_Pin_0 );
else
GPIO_ResetBits( GPIOC, GPIO_Pin_0 );
} void OLED_SDIN( uint8_t x )
{
if( x==0 )
GPIO_ResetBits( GPIOC, GPIO_Pin_1 );
else
GPIO_SetBits( GPIOC, GPIO_Pin_1 );
} void OLED_RES( uint8_t x )
{
if( x==1 )
GPIO_SetBits( GPIOG, GPIO_Pin_15 );
else
GPIO_ResetBits( GPIOG, GPIO_Pin_15 );
} void OLED_DC( uint8_t x )
{
if( x==1 )
GPIO_SetBits( GPIOD, GPIO_Pin_3 );
else
GPIO_ResetBits( GPIOD, GPIO_Pin_3 );
} void OLED_CS( uint8_t x )
{
if( x==1 )
GPIO_SetBits( GPIOD, GPIO_Pin_6 );
else
GPIO_ResetBits( GPIOD, GPIO_Pin_6 );
}

2.模拟SPI通信

我根据时序图模拟了SPI的通信协议,按照时序图对相应的引脚操作即可,一共写了俩个函数:写数据和写命令。函数如示:


/* 写一个命令 高位先行 */
void OLED_WR_Cmd( uint8_t cmd )
{
uint8_t i,k;
/* 拉低片选CS,写命令拉低DC */
OLED_CS(0);
OLED_DC(0); for( i=0;i<8;i++ )
{
/* 时钟线,上升沿有效 */
OLED_SCLK(0);
k=cmd&(0x80);
OLED_SDIN(k);
OLED_SCLK(1);
cmd<<=1;
}
OLED_CS(1);
OLED_DC(1);
} /* 写一个数据 高位先行 */
void OLED_WR_Data( uint8_t data )
{
uint8_t i,k;
/* 拉低片选CS,写数据拉高DC */
OLED_CS(0);
OLED_DC(1); for( i=0;i<8;i++ )
{
/* 时钟线,上升沿有效 */
OLED_SCLK(0);
k=data&(0x80);
OLED_SDIN(k);
OLED_SCLK(1);
data<<=1;
}
OLED_CS(1);
OLED_DC(1);
}

要注意:无论数据还是命令的传输,都是高位先行,这是根据时序图知道的。

3.OLED初始化函数

前面说了,会有一堆初始化命令:


/* OLED初始化函数 */
void OLED_Init( void )
{
/* 引脚初始化 */
OLED_GPIO_Config(); OLED_CS(1);
OLED_DC(1); /* 复位 */
OLED_RES(0);
Delay_ms(1000);
OLED_RES(1); /* 开始写入初始化命令 */
OLED_WR_Cmd(0xAE);//关闭显示
OLED_WR_Cmd(0xD5);//设置时钟分频因子
OLED_WR_Cmd(80); OLED_WR_Cmd(0xA8);//设置驱动路数
OLED_WR_Cmd(0x3F);//路数默认0x3F(1/64) OLED_WR_Cmd(0xD3);//设置显示偏移
OLED_WR_Cmd(0x00);//偏移默认为0 OLED_WR_Cmd(0x40);//设置显示开始行[5:0] OLED_WR_Cmd(0x8D);//电荷泵设置
OLED_WR_Cmd(0x14);//bit2,开启/关闭 OLED_WR_Cmd(0x20);//设置内存地址模式
OLED_WR_Cmd(0x02);//[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10; OLED_WR_Cmd(0xA1);//段重定义设置,bit0:0,0->0;1,0->127;
OLED_WR_Cmd(0xC0);//设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数
OLED_WR_Cmd(0xDA);//设置COM硬件引脚配置
OLED_WR_Cmd(0x12);//[5:4]配置 OLED_WR_Cmd(0x81);//对比度设置
OLED_WR_Cmd(0xEF);//默认0x7F(范围1~255,越大越亮) OLED_WR_Cmd(0xD9);//设置预充电周期
OLED_WR_Cmd(0xF1);//[3:0],PHASE 1;[7:4],PHASE 2; OLED_WR_Cmd(0xDB);//设置VCOMH 电压倍率
OLED_WR_Cmd(0x30);//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc; OLED_WR_Cmd(0xA4);//全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
OLED_WR_Cmd(0xA6);//设置显示方式;bit0:1,反相显示;0,正常显示 OLED_WR_Cmd(0xAF);//开启显示 /* 清屏函数 */
OLED_Clear();
}

啥也不说了,照着写就完事了!

4.图像刷新与清屏函数

假设图像的信息都已经写到GRAM中了,那么就可以通过图像刷新函数,将GRAM中的图像画到OLED屏幕中,刷新函数如示:

/* 图像刷新函数 */
void OLED_Refresh_GRAM( void )
{
uint8_t i,j;
for( i=0;i<8;i++ )
{
/* 设置显示的起始地址 */
OLED_WR_Cmd(0xB0+i);//设置页地址(行)
OLED_WR_Cmd(0x00);//设置列地址的低四位
OLED_WR_Cmd(0x10);//设置列地址的高四位
for( j=0;j<128;j++ )
{
OLED_WR_Data(OLED_GRAM[j][i]);//将GRAM中图像信息写入屏幕
}
}
}

对应的,清屏函数就是将GRAM中图像的信息抹去,然后刷新一下:

* 清屏函数 */
void OLED_Clear( void )
{
uint8_t i,j;
for( i=0;i<8;i++ )
for( j=0;j<128;j++ )
OLED_GRAM[j][i]=0x00;
OLED_Refresh_GRAM();
}

5.画点函数

所谓画点,就是将屏幕中指定位置的像素点点亮。在GRAM中就相当于把相对应的位置一,难点在于根据屏幕上的坐标推算出GRAM中的位置,函数如下:

//OLED_GRAM[128][8]
/* 画点函数,以屏幕像素点为单位,以左上角为原点 x:0~127 y:0~63
(x,y)坐标换算:OLED_GRAM[x][7-y/8]|=1<<(7-y%8); mode取1正显,取0反显
*/
void OLED_DrawPoint( uint8_t x,uint8_t y,uint8_t mode )
{
/* 主要是求出y坐标,根据y坐标来位运算OLED_GRAM中相应的位 */
uint8_t i,j,temp;
/* 判断坐标是否超出范围 */
if(x>127||y>63)return; i=7-y/8;//算出第几页
j=y%8;
temp=0x01<<(7-j);//由位运算精确找出坐标像素点
if(mode==0)
OLED_GRAM[x][i]&=~temp;
else
OLED_GRAM[x][i]|=temp;
}

6.显示字符函数

在某一坐标显示指定大小的字符,而且是从字库中提取字符图像信息,这就是显示字符函数:

/* 在(x,y)坐标正显/反显指定大小字符chr
mode:0是反显,1是正常显示
size:12/16/24
ASCII字符集: !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
*/
void OLED_Show_Char( uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode )
{
/* temp是当前对应的一个字节的点集,y0是初始坐标 */
uint8_t temp,t,t1;
uint8_t y0=y;
/* csize是单个字符所占字节的多少 */
uint8_t csize=(size/8+((size%8)?1:0))*(size/2);
/* 求出偏移的地址差,即得到该字符在字库中的序号(从零开始) */
/* chr会锁定字符在字库中的的序号 */
chr=chr-' '; /* 相应字符点集有多少个字节 */
for( t=0;t<csize;t++ )
{
/* 根据字符的大小选择相应字库,根据chr得到具体的字符地址 */
switch( size )
{
case 12:temp=asc2_1206[chr][t];break;//12x6(行x列)
case 16:temp=asc2_1608[chr][t];break;//16x8
case 24:temp=asc2_2412[chr][t];break;//24x12
default:return;//没有相应字库
} /* 画出每一列的点 */
for( t1=0;t1<8;t1++ )
{
if( temp&0x80 )
OLED_DrawPoint( x,y,mode );
else
OLED_DrawPoint( x,y,!mode ); temp<<=1;
y++; /* 根据给出每一列的像素点的多少,判断列是否满:
满了就换列,不满继续在此列画点 (与字库画点原理相关)
*/
if( ( y-y0 )==size )
{
y=y0;
x++;
/* 一列满,跳出循环,直接转到下一个字节点集的打印 */
break;
}
}
}
}

7.显示字符串函数

显示字符串,就是显示字符的加强版:

/* (x,y)是显示的坐标,*p是字符串的首地址,size是字符点集大小 */
void OLED_Show_String( uint8_t x, uint8_t y, const uint8_t *p, uint8_t size, uint8_t mode )
{
/* 判断是否合法字符,同时也限定了范围 */
while( (*p<='~')&&(*p>=' ') )
{
/* 如果初始行放不下,移动到下一行 */
if( x>(128-(size/2)) )
{
x=0;
y=y+size;
}
if( y>(64-size) )
{
x=y=0;
OLED_Clear();
} OLED_Show_Char( x,y,*p,size,mode );
/* 移动到下一个字符位置,size/2是因为做点集时就是:行X列,而且 行=2X列,所以size就是行数 */
x=x+size/2;
p++;
}
}

8.显示特殊图像

显示特殊图像的方法和显示ASCII字符一样,只需要知道特殊图像的点阵信息即可,这个可以使用取模软件自己画点,再适当修改显示函数即可,学会了可以做一些特殊图像,包括汉字。当然,汉字也有字库,就是太大了,加上使用英文完全可以表达,所以只把需要显示的汉字做出来就行。当然,用来表白也是不错的!
STM32—4线SPI驱动SSD1306 OLED

上一篇:SOA与C#


下一篇:axis2开发实例(一)