一、前言

        本文使用的是四针脚的OLED模块(如图所示),采用的是I2C通信协议,并附上完整的oled.c.h代码,是本人在学习OLED屏幕的笔记,如有错误,望指出。

二、正文

2.1 接线

        四针的OLED屏幕的接线很简单,如上图,分别是电源,地,SCL时钟线和SDA数据线,本文的代码采用的是普通IO口模拟I2C通信。

2.2 点亮屏幕的某一位置的原理

        OLED屏幕,单片机是无法直接点亮的,需要通过一颗SSD1306芯片来驱动。OLED本身是没有显存的,他的显存是通过SSD1306提供的。

        我们知道点亮屏幕中的想要的位置其实就是点亮那一位,因此我们只需要对那个我们希望点亮的位置置1,就可以点亮那一个位置了。       

         OLED屏幕是128*64的屏幕,厂家把其分为了8页,即每一页都是8行128列。我们在修改每一页中的个别列时,就需要找到你想要的页,在找到对应的列进行修改。如下图所示。

        我们假设要点亮数学意义上的坐标(3,4)这一位置时,第一步需要找到这个点是属于第1页的;第二步确定这个点是处于第3列;第三步把0x11赋值给第1页的第3列,即可点亮这一位置。

2.3 驱动OLED的指令

 ( 图源正点原子)

        假设我要开启屏幕,需要给OLED发送0xAF,值得一提的是,如果发送的是0xAE即关闭显示,此时无论你如何点亮屏幕,屏幕都是黑屏,这点后面案例会有实验。

        设置页地址,和设置列地址的指令看图就很清晰了,主要关注一点就是设置高位列地址的时候,第5位是1。

2.4 代码讲解

        讲解部分只讲解了部分较难理解和常用代码。

2.4.1 I2C代码

        I2C就不多赘述了,不是本文的重点,我直接放在oled.c代码块里,感兴趣的读者可以跳转,有注释。

2.4.2 OLED操作代码

/**
   *  @brief     OLED发送一个字节(封装了Write_IIC_Command和Write_IIC_Data)
   *  @param	 dat:0 - 255
   *  @param	 mode:设置为0,发送的是命令
				       设置为1,发送的是数据
   *  @retval	 无
   */
void OLED_WR_Byte(uint8_t dat, uint8_t mode)
{
	if(mode)					      							// 如果mode为真,则dat发送的是数据
	{
		Write_IIC_Data(dat);		  							// 发送数据
	}
	else 							  							// 如果mode为假,则dat发送的是命令
	{
		Write_IIC_Command(dat);		  							// 发送命令
	}
}

/**
   *  @brief     设置光标,确立开始画点的初始位置(指定指定页的指定列)
   *  @param	 x:0 - 127,数学上的x
                 y:0 - 63,数学上的y
   *  @retval	 无
   */
void OLED_Set_Pos(uint8_t x, uint8_t y) 
{ 	
    OLED_WR_Byte((0xb0 | y/8), OLED_CMD);						// 翻到第y页
	OLED_WR_Byte(((x & 0xf0) >> 4) | 0x10, OLED_CMD);			// 发送高4位,x与上f0,得到高四位,再右移4位到低四位,或上10,得0001 x的初始高四位
	OLED_WR_Byte((x & 0x0f), OLED_CMD); 						// 发送低4位
}   
  

        主要需要关注的是设置光标函数,这确定了你之后显示字符等等操作,从哪个位置开始。

        第一句:0xb0 | y/8,y是行数,y/8即是页数,如:在63行显示,常识都知道是第八页,那么63/8 = 7,由于从第0页计算,所以7即是页数。至于0xb0是厂家要求的指令,可以翻阅2.3的图片对比学习。

        第二句:首先x & 0xf0,x是列数,与上f0,也就是取高4位(1&0得0,1&1得1),再>>4,即右移四位,但由于厂家要求第5位需置1,所以还需或上0x10(1|0得1,1|1得1)。

        第三句:x & 0x0f,与第二步类似,就是取低四位。

2.4.3 OLED显示字符代码

/**
   *  @brief     在OLED上显示一个字符
   *  @param	 x:0 - 127,数学上的x
				 y:0 - 63,数学上的y
				 chr:所需显示的字符
				 Char_Size:字体大小,16/12
   *  @retval	 无
   */
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t Char_Size)
{      	 
	uint8_t c = 0,i = 0;	
	c = chr - ' ';												// 得到偏移后的值			
	if(Char_Size == 16)											// 字体为16的情况,即8*16
	{
		OLED_Set_Pos(x,y);										// 设置初始位置光标
		for(i=0; i<8; i++)
			OLED_WR_Byte(F8X16[c*16+i], OLED_DATA);				// 前八个
		OLED_Set_Pos(x,y+8);
		for(i=0; i<8; i++)
			OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);			// 后八个
	}
	else 														// 字体为12的情况,即8*6
	{	
		OLED_Set_Pos(x,y);										// 设置初始位置光标
		for(i=0; i<6; i++)
			OLED_WR_Byte(F6x8[c][i],OLED_DATA);					// 发送数据
	}
}

        后面的显示字符串、数字、中文都是直接循环调用显示字符函数,所以较为重要。

        第二句:c = chr - ' ';原因是ASCLL码表并不是从0x00开始的,第一个字符NULL是0x32,依次递增,所以这句代码就可以计算出你想要显示的字符的偏移量。

        F8X16是字体16的字库,意思就是一个字符x列占8,y行占16行,所以最多显示4行数据。

        F6x8是字体12的字库,意思就是一个字符x列占6,y行占8行,所以最多可以显示8行数据。

        然后大家在改代码的时候,一定不要在显示字符函数里,忽略了设置光标函数,因为其他代码都是以此为基础进行逐渐封装,大家可以参考注释。

三、 实验现象

实验1和2(同现象)

实验3

实验4

实验5

实验6

四、完整代码

4.1 oled.c

//--------------------------------------------------------------------------
//              GND   电源地
//              VCC   接5V或3.3v电源
//              SCL   接PC7(SCL)
//              SDA   接PC8(SDA)            
//--------------------------------------------------------------------------

#include "./BSP/OLED/oled.h"
#include "stdlib.h"
#include "./BSP/OLED/oledfont.h"
#include "./SYSTEM/delay/delay.h"

//OLED的显存
//存放格式如下.
//[0]0 1 2 3 ... 127	
//[1]0 1 2 3 ... 127	
//[2]0 1 2 3 ... 127	
//[3]0 1 2 3 ... 127	
//[4]0 1 2 3 ... 127	
//[5]0 1 2 3 ... 127	
//[6]0 1 2 3 ... 127	
//[7]0 1 2 3 ... 127 			   



/***********************I2C操作****************************************/
/**
   *  @brief     I2C开始运行
   *  @param	 无
   *  @retval	 无
   */
void IIC_Start(void)
{
	OLED_SCLK_Set();												// 拉高SCL
	OLED_SDIN_Set();												// 拉高SDA
	OLED_SDIN_Clr();												// 拉低SDA
	OLED_SCLK_Clr();												// 拉低SCL
}

/**
   *  @brief     I2C结束运行
   *  @param	 无
   *  @retval	 无
   */
void IIC_Stop(void)
{
	OLED_SCLK_Set();												// 拉高SCL
//	OLED_SCLK_Clr();
	OLED_SDIN_Clr();												// 拉低SDA
	OLED_SDIN_Set();												// 拉高SDA
}

/**
   *  @brief     I2C等待应答
   *  @param	 无
   *  @retval	 无
   */
void IIC_Wait_Ack(void)
{
	OLED_SCLK_Set();												// 拉高SCL
	OLED_SCLK_Clr();												// 拉低SCL
}

/**
   *  @brief     I2C发送一个字节
   *  @param	 IIC_Byte:0 - 255
   *  @retval	 无
   */
void Write_IIC_Byte(uint8_t IIC_Byte)
{
	uint8_t dat;
	dat = IIC_Byte;
	OLED_SCLK_Clr();												// 拉低SCL
	for(uint8_t i = 0;i < 8;i++)									// 把写入的字节的8位依次从最高位写入
	{
		//	OLED_SCLK_Clr();		
		if((dat & 0x80) == 0x80)									// 如果传入的最高位是1,则拉高SDA
		{
			OLED_SDIN_Set();						
		}
		else 														// 如果传入的最高位是0,则拉低SDA	
		{
			OLED_SDIN_Clr();
		}
		dat = dat << 1;							    				// 传入的数据左移1位,取最高位
		OLED_SCLK_Set();											// 拉高SCL
		OLED_SCLK_Clr();											// 拉低SCL
	}
}

/**
   *  @brief     I2C发送一个命令Command
   *  @param	 IIC_Command:0 - 255
   *  @retval	 无
   */
void Write_IIC_Command(uint8_t IIC_Command)
{
	IIC_Start();
	Write_IIC_Byte(0x78);            								// 发送从机地址0x78
	IIC_Wait_Ack();	
	Write_IIC_Byte(0x00);			 								// 发送控制字节,传输单个字节充当命令
	IIC_Wait_Ack();	
	Write_IIC_Byte(IIC_Command); 	 								// 发送命令
	IIC_Wait_Ack();	
	IIC_Stop();
}

/**
   *  @brief     I2C发送一个数据Data
   *  @param	 IIC_Data:0 - 255
   *  @retval	 无
   */
void Write_IIC_Data(uint8_t IIC_Data)
{
	IIC_Start();
	Write_IIC_Byte(0x78);           							 	// 发送从机地址0x78
	IIC_Wait_Ack();		
	Write_IIC_Byte(0x40);			 								// 发送控制字节,传输单个字节充当数据
	IIC_Wait_Ack();	
	Write_IIC_Byte(IIC_Data);	     								// 发送数据
	IIC_Wait_Ack();	
	IIC_Stop();
}
/*********************************************************************/




/***********************OLED操作**************************************/
/**
   *  @brief     OLED发送一个字节(封装了Write_IIC_Command和Write_IIC_Data)
   *  @param	 dat:0 - 255
   *  @param	 mode:设置为0,发送的是命令
				       设置为1,发送的是数据
   *  @retval	 无
   */
void OLED_WR_Byte(uint8_t dat, uint8_t mode)
{
	if(mode)					      							// 如果mode为真,则dat发送的是数据
	{
		Write_IIC_Data(dat);		  							// 发送数据
	}
	else 							  							// 如果mode为假,则dat发送的是命令
	{
		Write_IIC_Command(dat);		  							// 发送命令
	}
}

/**
   *  @brief     设置光标,确立开始画点的初始位置(指定指定页的指定列)
   *  @param	 x:0 - 127,数学上的x
                 y:0 - 63,数学上的y
   *  @retval	 无
   */
void OLED_Set_Pos(uint8_t x, uint8_t y) 
{ 	
	OLED_WR_Byte((0xb0 | y/8), OLED_CMD);						// 翻到第y页
	OLED_WR_Byte(((x & 0xf0) >> 4) | 0x10, OLED_CMD);			// 发送高4位,x与上f0,得到高四位,再右移4位到低四位,或上10,得0001 x的初始高四位
	OLED_WR_Byte((x & 0x0f), OLED_CMD); 						// 发送低4位
}   
  
/**
   *  @brief     开启OLED显示
   *  @param	 无
   *  @retval	 无
   */
void OLED_Display_On(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD);  								// SET DCDC命令
	OLED_WR_Byte(0X14,OLED_CMD);  								// DCDC ON
	OLED_WR_Byte(0XAF,OLED_CMD);  								// DISPLAY ON
}
   
/**
   *  @brief     关闭OLED显示
   *  @param	 无
   *  @retval	 无
   */
void OLED_Display_Off(void)
{
	OLED_WR_Byte(0X8D,OLED_CMD);  								// SET DCDC命令
	OLED_WR_Byte(0X10,OLED_CMD); 								// DCDC OFF
	OLED_WR_Byte(0XAE,OLED_CMD);  								// DISPLAY OFF
} 
 
/**
   *  @brief     清屏(屏幕是黑色的,跟没有点亮一样)
   *  @param	 无
   *  @retval	 无
   */
void OLED_Clear(void)  
{  
	uint8_t i,n;		    
	for(i=0; i<8; i++)  
	{  
		OLED_WR_Byte(0xb0+i,OLED_CMD);    						// 设置页地址(0~7)
		OLED_WR_Byte(0x00,OLED_CMD);      						// 设置显示位置—列低地址
		OLED_WR_Byte(0x10,OLED_CMD);      						// 设置显示位置—列高地址   
		for(n=0; n<128; n++)
			OLED_WR_Byte(0,OLED_DATA); 	   						// 置0全灭
	} 
}

/**
   *  @brief     全亮屏幕
   *  @param	 无
   *  @retval	 无
   */
void OLED_Fill(void)  
{  
	uint8_t i,n;		    
	for(i=0; i<8; i++)  
	{  
		OLED_WR_Byte(0xb0+i,OLED_CMD);    						// 设置页地址(0~7)
		OLED_WR_Byte(0x00,OLED_CMD);      						// 设置显示位置—列低地址
		OLED_WR_Byte(0x10,OLED_CMD);      						// 设置显示位置—列高地址   
		for(n=0; n<128; n++)
			OLED_WR_Byte(1,OLED_DATA); 	 						// 全亮 
	}
}
/*******************************************************************/



/***********************OLED显示屏**********************************/
/**
   *  @brief     在OLED上显示一个字符
   *  @param	 x:0 - 127,数学上的x
				 y:0 - 63,数学上的y
				 chr:所需显示的字符
				 Char_Size:字体大小,16/12
   *  @retval	 无
   */
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t Char_Size)
{      	 
	uint8_t c = 0,i = 0;	
	c = chr - ' ';												// 得到偏移后的值			
	if(Char_Size == 16)											// 字体为16的情况,即8*16
	{
		OLED_Set_Pos(x,y);										// 设置初始位置光标
		for(i=0; i<8; i++)
			OLED_WR_Byte(F8X16[c*16+i], OLED_DATA);				// 前八个
		OLED_Set_Pos(x,y+8);
		for(i=0; i<8; i++)
			OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);			// 后八个
	}
	else 														// 字体为12的情况,即8*6
	{	
		OLED_Set_Pos(x,y);										// 设置初始位置光标
		for(i=0; i<6; i++)
			OLED_WR_Byte(F6x8[c][i],OLED_DATA);					// 发送数据
	}
}

/**
   *  @brief     在OLED上显示数字
   *  @param	 x:0 - 127,数学上的x
				 y:0 - 63,数学上的y
				 num:0 - 4,294,967,295,所需显示的数字
				 len:0 - 255,数字长度
				 size2:16/12,字体大小
   *  @retval	 无
   */
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t Num_Size)
{         	
	uint8_t len1 = len;												// 存储一下数字长度
	uint8_t i = 0;
	while(len1 != 0)
	{
		OLED_ShowChar(x, y, num / OLED_Pow(10, len - i - 1) % 10 + '0',Num_Size);
		if(Num_Size == 16)											// 字体16		
		{
			x = x + 8;
			if(x > 120)
			{
				x = 0;
				y += 16;											// 列超出范围,换行
			}
		}
		if(Num_Size == 12)											// 字体12
		{
			x = x + 6;
			if(x > 122)
			{
				x = 0;
				y += 8;												// 列超出范围,换行
			}
		}
		len1--;
		i++;
	}
} 

/**
   *  @brief     在OLED上显示字符串
   *  @param	 x:0 - 127,数学上的x
				 y:0 - 63,数学上的y
				 chr:所需显示的字符串
				 Char_Size:16/12,字体大小
   *  @retval	 无
   */
void OLED_ShowString(uint8_t x, uint8_t y, uint8_t *chr, uint8_t Str_Size)
{
	uint8_t j = 0;
	while (chr[j] != '\0')										// 字符串显示未结束
	{		
		OLED_ShowChar(x,y,chr[j],Str_Size);						// 显示字符串单个字符
		if(Str_Size == 16)										// 字体16		
		{
			x = x + 8;
			if(x > 120)
			{
				x = 0;
				y += 16;										// 列超出范围,换行
			}
		}
		if(Str_Size == 12)										// 字体12
		{
			x = x + 6;
			if(x > 122)
			{
				x = 0;
				y += 8;											// 列超出范围,换行
			}
		}
		j++;
	}
}

/**
   *  @brief     在OLED上显示汉字
   *  @param	 x:0 - 127,数学上的x
				 y:0 - 63,数学上的y
				 no:所需显示的汉字坐标--词典需要自己做
   *  @retval	 无
   */
void OLED_ShowChinese(uint8_t x, uint8_t y, uint8_t no)
{      			    
	uint8_t t,adder = 0;
	OLED_Set_Pos(x,y);											// 设置光标
    for(t=0; t<16; t++)
	{
		OLED_WR_Byte(Dic[2 * no][t],OLED_DATA);					// 显示某个文字的第一行数据
		adder += 1;
    }	
	OLED_Set_Pos(x,y+8);										// 设置下一行的光标
    for(t=0;t < 16;t++)
	{	
		OLED_WR_Byte(Dic[2 * no + 1][t],OLED_DATA);				// 显示某个文字的第二行数据
		adder += 1;
    }					
}
/*******************************************************************/



/***********************初始化****************************************/
/**
   *  @brief     初始化SSD1306	
   *  @param	 无
   *  @retval	 无
   */
void OLED_Init(void)
{ 		
	// 初始化结构体
	GPIO_InitTypeDef gpio_init_struct;
	
	// 使能 
	OLED_I2C_SCL_CLK_ENABLE();										// SCL时钟使能
	OLED_I2C_SDA_CLK_ENABLE();										// SDA时钟使能
	
	// 初始化GPIO
	gpio_init_struct.Pin = OLED_I2C_SCL_PIN;						
	gpio_init_struct.Mode = GPIO_MODE_OUTPUT_PP;					// 推挽输出
	gpio_init_struct.Speed = GPIO_SPEED_FREQ_HIGH;					// 高速输出
	HAL_GPIO_Init(OLED_I2C_SCL_PORT, &gpio_init_struct);			// 初始化SCL
	
	gpio_init_struct.Pin = OLED_I2C_SDA_PIN;						
	HAL_GPIO_Init(OLED_I2C_SDA_PORT, &gpio_init_struct);			// 初始化SDA
	
	delay_ms(200);

	// 出厂的初始化(由厂家提供,无需深究)
	OLED_WR_Byte(0xAE,OLED_CMD);									//--display off
	OLED_WR_Byte(0x00,OLED_CMD);									//---set low column address
	OLED_WR_Byte(0x10,OLED_CMD);									//---set high column address
	OLED_WR_Byte(0x40,OLED_CMD);									//--set start line address  
	OLED_WR_Byte(0xB0,OLED_CMD);									//--set page address
	OLED_WR_Byte(0x81,OLED_CMD);								    // contract control
	OLED_WR_Byte(0xFF,OLED_CMD);									//--128   
	OLED_WR_Byte(0xA1,OLED_CMD);									//set segment remap 
	OLED_WR_Byte(0xA6,OLED_CMD);									//--normal / reverse
	OLED_WR_Byte(0xA8,OLED_CMD);									//--set multiplex ratio(1 to 64)
	OLED_WR_Byte(0x3F,OLED_CMD);									//--1/32 duty
	OLED_WR_Byte(0xC8,OLED_CMD);									//Com scan direction
	OLED_WR_Byte(0xD3,OLED_CMD);									//-set display offset
	OLED_WR_Byte(0x00,OLED_CMD);
	OLED_WR_Byte(0xD5,OLED_CMD);									//set osc division
	OLED_WR_Byte(0x80,OLED_CMD);
	OLED_WR_Byte(0xD8,OLED_CMD);									//set area color mode off
	OLED_WR_Byte(0x05,OLED_CMD);
	OLED_WR_Byte(0xD9,OLED_CMD);									//Set Pre-Charge Period
	OLED_WR_Byte(0xF1,OLED_CMD);
	OLED_WR_Byte(0xDA,OLED_CMD);									//set com pin configuartion
	OLED_WR_Byte(0x12,OLED_CMD);
	OLED_WR_Byte(0xDB,OLED_CMD);									//set Vcomh
	OLED_WR_Byte(0x30,OLED_CMD);
	OLED_WR_Byte(0x8D,OLED_CMD);									//set charge pump enable
	OLED_WR_Byte(0x14,OLED_CMD);
	OLED_WR_Byte(0xAF,OLED_CMD);									//--turn on oled panel
	
	OLED_Clear();
}  
/**********************************************************************/



/***********************其他操作****************************************/
/**
   *  @brief     指数函数
   *  @param	 m:底数0 - 255
				 n:指数0 - 255
   *  @retval	 result:返回计算完的结果
   */
uint32_t OLED_Pow(uint8_t m,uint8_t n)
{
	uint32_t result = 1;	 
	while(n--)
		result *= m;    									    // 传入m=3,n=3;结果res = 27
	return result;
}	

void Delay_50ms(unsigned int Del_50ms)
{
	unsigned int m;
	for(;Del_50ms>0;Del_50ms--)
		for(m=6245;m>0;m--);
}

void Delay_1ms(unsigned int Del_1ms)
{
	uint8_t j;
	while(Del_1ms--)
	{	
		for(j=0;j<123;j++);
	}
}
/**********************************************************************/

        这里的delay_ms,大家需要修改一下,我使用的是系统滴答定时器的,并不是最底下的Delay_1ms。

4.2 oled.h

#ifndef __OLED_H
#define __OLED_H	

#include "./SYSTEM/sys/sys.h"
#include "stdlib.h"	
  

/****************************************定义*****************************************************/
/* 引脚定义 */
#define OLED_I2C_SCL_PORT                GPIOC
#define OLED_I2C_SCL_PIN                 GPIO_PIN_7
#define OLED_I2C_SCL_CLK_ENABLE()        do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0)   					/* PC口时钟使能 */
#define OLED_I2C_SDA_PORT                GPIOC
#define OLED_I2C_SDA_PIN                 GPIO_PIN_8
#define OLED_I2C_SDA_CLK_ENABLE()        do{ __HAL_RCC_GPIOC_CLK_ENABLE(); }while(0)   					/* PC口时钟使能 */

/* OLED IIC端口定义 */
#define OLED_SCLK_Clr() HAL_GPIO_WritePin(OLED_I2C_SCL_PORT, OLED_I2C_SCL_PIN, GPIO_PIN_RESET)			//SCL_IIC接口设置为0
#define OLED_SCLK_Set() HAL_GPIO_WritePin(OLED_I2C_SCL_PORT, OLED_I2C_SCL_PIN, GPIO_PIN_SET)			//SCL_IIC接口设置为1
#define OLED_SDIN_Clr() HAL_GPIO_WritePin(OLED_I2C_SDA_PORT, OLED_I2C_SDA_PIN, GPIO_PIN_RESET)			//SDA_IIC接口设置为0
#define OLED_SDIN_Set() HAL_GPIO_WritePin(OLED_I2C_SDA_PORT, OLED_I2C_SDA_PIN, GPIO_PIN_SET)			//SDA_IIC接口设置为1
 		     
/* 其他定义 */
#define OLED_CMD  0	//写命令
#define OLED_DATA 1	//写数据
/*************************************************************************************************/



/******************************************函数****************************************************/
/* I2C操作函数 */
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Wait_Ack(void);
void Write_IIC_Byte(uint8_t IIC_Byte);
void Write_IIC_Command(uint8_t IIC_Command);
void Write_IIC_Data(uint8_t IIC_Data);

/* OLED操作函数 */
void OLED_WR_Byte(uint8_t dat, uint8_t mode);
void OLED_Set_Pos(uint8_t x, uint8_t y);
void OLED_Display_On(void);
void OLED_Display_Off(void);
void OLED_Clear(void);
void OLED_Fill(void);  

/* OLED显示屏写函数 */
void OLED_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t Char_Size);
void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t Num_Size);
void OLED_ShowString(uint8_t x, uint8_t y, uint8_t *chr, uint8_t Str_Size);
void OLED_ShowChinese(uint8_t x, uint8_t y, uint8_t no);

/* OLED初始化函数 */
void OLED_Init(void);

/* 其他操作函数 */
uint32_t OLED_Pow(uint8_t m,uint8_t n);
void Delay_50ms(unsigned int Del_50ms);
void Delay_1ms(unsigned int Del_1ms);
/*************************************************************************************************/

#endif  
	 

        大家在移植的时候只需要改一下oled.h的引脚和delay延时函数。

4.3 oledfont.h

#ifndef __OLEDFONT_H
#define __OLEDFONT_H 	   
//常用ASCII表
//偏移量32
//大小:12*6
/************************************6*8的点阵************************************/
const unsigned char F6x8[][6] =		
{
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp
0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// !
0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// "
0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// #
0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $
0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// %
0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// &
0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// '
0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// (
0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// )
0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// *
0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// +
0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// ,
0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// -
0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// .
0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// /
0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0
0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1
0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2
0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3
0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4
0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5
0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6
0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7
0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8
0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9
0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// :
0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ;
0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// <
0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// =
0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// >
0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ?
0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @
0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A
0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B
0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C
0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D
0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E
0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F
0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G
0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H
0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I
0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J
0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K
0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L
0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M
0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N
0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O
0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P
0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q
0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R
0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S
0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T
0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U
0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V
0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W
0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X
0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y
0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z
0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [
0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55
0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ]
0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^
0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _
0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// '
0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a
0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b
0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c
0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d
0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e
0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f
0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g
0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h
0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i
0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j
0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k
0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l
0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m
0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n
0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o
0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p
0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q
0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r
0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s
0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t
0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u
0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v
0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w
0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x
0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y
0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z
0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines
};
/****************************************8*16的点阵************************************/
const unsigned char F8X16[]=	  
{
  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
};

char Dic[][32]={

{0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00},
{0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00},/*"电",3*/

{0x80,0x82,0x82,0x82,0x82,0x82,0x82,0xE2,0xA2,0x92,0x8A,0x86,0x82,0x80,0x80,0x00},
{0x00,0x00,0x00,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"子",4*/

{0x24,0x24,0xA4,0xFE,0xA3,0x22,0x00,0x22,0xCC,0x00,0x00,0xFF,0x00,0x00,0x00,0x00},
{0x08,0x06,0x01,0xFF,0x00,0x01,0x04,0x04,0x04,0x04,0x04,0xFF,0x02,0x02,0x02,0x00},/*"科",5*/

{0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00},
{0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00},/*"?",0*/

{0x40,0x40,0x48,0x48,0x48,0xC8,0x78,0x4F,0x48,0x48,0x48,0x48,0x48,0x40,0x40,0x00},
{0x00,0x00,0x00,0x00,0x03,0x12,0x12,0x22,0x22,0x52,0x8A,0x06,0x00,0x00,0x00,0x00},/*"专",0*/

{0x00,0x10,0x60,0x80,0x00,0xFF,0x00,0x00,0x00,0xFF,0x00,0x00,0xC0,0x30,0x00,0x00},
{0x40,0x40,0x40,0x43,0x40,0x7F,0x40,0x40,0x40,0x7F,0x42,0x41,0x40,0x40,0x40,0x00},/*"业",1*/

{0x10,0x10,0x10,0xFF,0x10,0x90,0x08,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x08,0x00},
{0x04,0x44,0x82,0x7F,0x01,0x80,0x80,0x40,0x43,0x2C,0x10,0x28,0x46,0x81,0x80,0x00},/*"技",2*/

{0x00,0x10,0x10,0x10,0x10,0xD0,0x30,0xFF,0x30,0xD0,0x12,0x1C,0x10,0x10,0x00,0x00},
{0x10,0x08,0x04,0x02,0x01,0x00,0x00,0xFF,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x00},/*"术",3*/

{0x00,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0xFE,0x82,0x82,0x92,0xA2,0x9E,0x00,0x00},
{0x80,0x60,0x1F,0x02,0x42,0x82,0x7F,0x00,0xFF,0x40,0x2F,0x10,0x2C,0x43,0x80,0x00},/*"服",4*/

{0x00,0x00,0x90,0x88,0x4C,0x57,0xA4,0x24,0x54,0x54,0x8C,0x84,0x00,0x00,0x00,0x00},
{0x01,0x01,0x80,0x42,0x22,0x1A,0x07,0x02,0x42,0x82,0x42,0x3E,0x01,0x01,0x01,0x00},/*"务",5*/

{0x00,0x04,0xE4,0x24,0x2C,0xB4,0x25,0x26,0x24,0xB4,0x2C,0x24,0xE4,0x04,0x00,0x00},
{0x00,0x00,0xFF,0x02,0x01,0x1E,0x12,0x12,0x12,0x1E,0x41,0x82,0x7F,0x00,0x00,0x00},/*"商",6*/



};

#endif

        这段代码是ASCCI字库和个别中文演示字库,来自淘宝器件商家提供的代码,直接复制即可。

4.4 main.c

#include "./SYSTEM/sys/sys.h"
#include "./SYSTEM/delay/delay.h"
#include "./BSP/OLED/oled.h"

int main(void)
{
    HAL_Init();                              /* 初始化HAL库 */
    sys_stm32_clock_init(RCC_PLL_MUL9);      /* 设置时钟, 72Mhz */
    delay_init(72);                          /* 延时初始化 */
    OLED_Init();							 // OLED初始化
	
    while(1)
    { 	
		/* 案例1:在开启屏幕的情况下使用全亮函数,持续0.5s;在关闭屏幕下,即使用全亮函数也不会亮,持续2s
		OLED_Display_On();
		OLED_Fill();
		delay_ms(500);
		OLED_Display_Off();
		OLED_Fill();
		delay_ms(2000);
		*/
		
		
		/* 案例2:在开启屏幕的情况下使用全亮函数,持续0.5s;在开启屏幕下,使用清屏函数,持续2s
		OLED_Display_On();
		OLED_Fill();
		delay_ms(500);
		OLED_Clear();
		delay_ms(2000);
		*/
		
		
		/* 案例3:展示OLED两种字符大小写单个字符
		// Note: 16字体x轴间距 >= 8、y轴间距 >= 16;12字体x轴间距 >= 6、y轴间距 >= 8;如果小于此间距,会造成字符部分重叠并闪烁
		OLED_ShowChar(0, 0, 'O', 16);
		OLED_ShowChar(8, 0, 'L', 16);
		OLED_ShowChar(16, 0, 'E', 16);
		OLED_ShowChar(24, 0, 'D', 16);
		OLED_ShowChar(0, 16, 'O', 12);
		OLED_ShowChar(6, 16, 'L', 12);
		OLED_ShowChar(12, 16, 'E', 12);
		OLED_ShowChar(18, 16, 'D', 12);
		OLED_ShowChar(18, 24, 'D', 12);
		*/
		
		
		/* 案例4:展示OLED显示数字
		// Note: 数字如果超过一行,会把剩余的显示在下一行
		OLED_ShowNum(0, 0, 123, 3, 16);
		OLED_ShowNum(0, 16, 123, 3, 12);
		OLED_ShowNum(18, 24, 789, 3, 12);
		delay_ms(1000);
		OLED_Clear();
		OLED_ShowNum(80, 0, 4294967295, 10, 12);
		OLED_ShowNum(80, 16, 4294967295, 10, 16);
		delay_ms(1000);
		OLED_Clear();
		*/	
		
		
		/* 案例5:展示OLED显示全屏字符
		// Note: 16字体是8*16,最多显示四行,12字体是6*8,最多显示8行;若出现重复覆盖的情况,必须先清屏,不然屏幕会出现重叠闪烁
		OLED_ShowString(0, 0,  (uint8_t *)"OLED_DISPLAY", 16);						// 字体16的演示
		OLED_ShowString(0, 16, (uint8_t *)"OLED_DISPLAY", 16);
		OLED_ShowString(0, 32, (uint8_t *)"OLED_DISPLAY", 16);
		OLED_ShowString(0, 48, (uint8_t *)"OLED_DISPLAY", 16);
		delay_ms(1000);
		OLED_Clear();
		
		OLED_ShowString(0, 0, (uint8_t *)"OLED_DISPLAY", 12);						// 字体12的演示
		OLED_ShowString(0, 8, (uint8_t *)"OLED_DISPLAY", 12);
		OLED_ShowString(0, 16, (uint8_t *)"OLED_DISPLAY",12);
		OLED_ShowString(0, 24, (uint8_t *)"OLED_DISPLAY", 12);
		OLED_ShowString(0, 32, (uint8_t *)"OLED_DISPLAY", 12);
		OLED_ShowString(0, 40, (uint8_t *)"OLED_DISPLAY", 12);
		OLED_ShowString(0, 48, (uint8_t *)"OLED_DISPLAY", 12);
		OLED_ShowString(0, 56, (uint8_t *)"OLED_DISPLAY", 12);
		delay_ms(1000);
		OLED_Clear();
		
		OLED_ShowString(80, 0, (uint8_t *)"OLED_DISPLAYABcsadasd", 16);				// 显示字符串如果超过一行,会在下一行的开头延续下去并覆盖原有的字符
		OLED_ShowString(80, 32, (uint8_t *)"OLED_DISPLAYABcbmbjgtcbsdad", 12);		// 显示字符串如果超过一行,会在下一行的开头延续下去并覆盖原有的字符
		delay_ms(1000);
		OLED_Clear();
		*/
		
		
		/* 案例6:展示OLED显示中文
		// Note: 字库的中文字体是16*16
		OLED_ShowChinese(0,0,0);
		OLED_ShowChinese(0,16,1);
		OLED_ShowChinese(0,32,2);
		OLED_ShowChinese(16,0,1);
		OLED_ShowChinese(32,0,2);
		*/
		
	}
}

五、结语

        本人现在是大三,算是第一次写博客,记录自己的学习笔记,也便于自己下次使用OLED复习。代码都是来自淘宝店家提供的,但改了一些,但总体框架没改。

        我看很多人使用的是建立一个128*64的大数组,我感觉确实是有好处的,这样一次性赋值过去,就不用再每次显示的时候,都需要使用以下清屏函数,尤其是在重复显示的区域那里,从案例可以看出,如果不适用清屏会造成闪烁。但缺点就是很占SRAM,在STM32问题应该不大,但如果移植到51单片机这种资源少点的,可能会比较麻烦,所以还是采用和学习商家例程代码的那套风格。

        然后对例程源码加了很多自己的想法,比如如果给的字符串太长了,自动显示到下一行删除了很多没用上的宏定义,进行了引脚的宏定义,补了一下注释,改了一下缩进(这点真的非常讨厌,一段代码没有缩进真的可读性很差)。

        碎碎念这么多,希望大家觉得有用的话给我点个赞呀!!!如果有错误的话欢迎立刻指出,不希望有错误的理解或者代码在互联网上流通,谢谢大家!

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐