0.96寸OLED屏显示测试和代码详细分析SPI通信

本文讲述了使用Arduino和SSD1306OLED显示屏的初次尝试,涉及硬件连接、SPI通信、驱动时序分析、初始化设置、字符显示以及取模方法,强调了数据手册和正确接线的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一次尝试点亮

程序代码地址,密码:1234。
买了一个两色的oled,然后下载了资料,手里有一个8266的arduino,所以为了方便就直接用了。使用arduino主要原因,单片机keil太大,来回找文件修改引脚比较麻烦。实际在spi的通信方面的原理都是一样。
首次尝试,遇到问题,
1,资料中的引脚号需要修改
2,点阵取模得到的各种字符的**font.h文件和工程文件(.ino)**需要放在同级文件夹内。介绍中建议是放在arduino的lib文件夹中,但为了方便,也可以直接读取,所以直接移动一个文件夹就行。而且这个只是示例字符文件,没必要保存。
在这里插入图片描述
3,需要注意,如果只是给了电源电压,OLED也是不会亮的。个人本来怀疑屏坏了,后来接上信号线后,下完程序,发现能正常显示,不知道为什么手机拍的颜色有点儿失真。
在这里插入图片描述

驱动SSD1306读取工作时序分析

在这里插入图片描述
从时序图中可以看出,主要包含6种时间:时钟周期,地址设置时间,芯片选择时间,读写数据时间,时钟周期中高低时间,上升下降沿时间
时钟周期:大于100ns=10-7s,也就是时钟频率小于10MHz,
时钟周期中低电平和高电平时间:大于15ms
地址(数据/命令)设置/保持时间、写/读数据时间:设置是传送数据还是传送命令,会发现两种时间的值是一样的。从根本上来讲写入命令和写入地址都是进行信息传递。
上升沿和下降沿时间:最大是40ns,理论上,理想的波形应该是矩形波,也就是上升和下降沿是0s。

时间的整体分析:
首先,第一个时间是片选时间cs起作用,毕竟没选择好设备,发送数据或命令都无意义;由于数据或命令的传送都是在时钟的上升沿完成,所以在上升沿的那段时间,必须保证D/C(数据/命令)和数据传送的电平稳定。

常用设置分析(实际就是对驱动SSD1306的理解)

OLED基本参数

0.96寸双色显示屏,支持IIC和spi通信,128*64pixels,8page显示内存,相当于将屏幕的行按照字节数分为8份

驱动手册的命令使用和查找方式

来源:可以通过很多途径,有些只是给了部分常用内容,若想知道详细的手册可以去一些关于硬件手册的网站(例如半导小芯等)查询下载,下面是截取一部分命令表格说明使用方式
在这里插入图片描述

初始化使用的命令

下面的OLED_WR_Byte是定义的函数,MCU把程序写入到驱动,进而控制显示屏。不同的编程软件和MCU来看,程序的表现形式可能不同,但基本命令是一样的。这些命令都可以通过手册找到。
几个比较重要和常需要变化的命令:
1,设置对比度:这里感觉就是亮度,命令如下图,根据上面查看手册命令的方式,可以知道需要发送两次命令,第一次的0x81只是告诉驱动下个自己的命令是对比度的值
在这里插入图片描述
2,显示有关命令包括屏幕打开(AF)/关闭(AE),数据位0(A7)还是1(A6)亮,上下,左右(26/27)滚动,都可通过手册中查到
在这里插入图片描述

地址显示位置命令

参考手册中9 Command Table中的3 Addressing Setting Command Table
这个控制画面的显示位置,需要控制内存地址的模式,列开始,行开始和结束
首先知道,有8个page,相当于可以存储8个128*64的显示缓存

内存地址模式有三种Page addressing modeHorizontal addressing modeVertical addressing mode

在这里插入图片描述

	OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
	OLED_WR_Byte(0x02,OLED_CMD);//也就是A[1:0]=10b
Page addressing mode模式

这种模式:到每个page结尾指针会自动到开始,但并没有到下一个page
在这里插入图片描述
这种模式设置起始列位置需要两个命令
在这里插入图片描述
设置page起始
在这里插入图片描述

Horizontal addressing mode

这种地址分布是到行结尾自动跳转到开始,且page也自动到下一个,到最后page结束又会指向第一个page。在这里插入图片描述
设置列的起始和结束
在这里插入图片描述
设置page的起始
在这里插入图片描述

下面模式的地址分布从上到下,从左到右
在这里插入图片描述

在这里插入图片描述

字节数据(data/command)传送

在这里插入图片描述
下图程序中的设置和上图的数据传送时序图刚好匹配
在这里插入图片描述

字符数据显示

整体方法,对字符取模,在初始化设置完成的情况下,将字模矩阵数据传入驱动
ASCII字母标
在这里插入图片描述

	void OLED_Set_Pos(unsigned char x, unsigned char y) 
{ 
	/*
	x±íʾ¿ªÊ¼ÏÔʾµÄÏñËØÆðʼλÖÃ
	y±íʾÏÔʾÔÚÄÇÒ»Ò³
	*/
	OLED_WR_Byte(0xb0+y,OLED_CMD);     //ÉèÖõØÖ·Ò³£¬y¡Ê[0£¬7]
	//½«ÏÔʾµÄ¿ªÊ¼µØÖ·ÖеĸßËÄ룬ºÍµÍËÄλ·Ö¿ªºóдÈë¸øOLED
	//¶ø8λµÄ·¶Î§
	OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);  //ÉèÖÃÏÔʾλÖᪿªÊ¼ÁеØÖ·µÄµÍËÄλ
	OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);    //ÉèÖÃÏÔʾλÖᪿªÊ¼ÁеØÖ·¸ßËÄλ
}   	
/*参数含义(c51程序)
x,显示位置的横坐标x
y,显示位置纵坐标y
下面程序中几个参数是宏定义过的,
size=16(字模16*8),OLED_DATA=1(传送的是数据)
*/
void OLED_ShowChar(u8 x,u8 y,u8 chr)
{      	
	unsigned char c=0,i=0;	
		c=chr-' ';					//字符在ascii位置	
		if(x>Max_Column-1)  //如果显示到最右边
			{x=0;y=y+2;}	//到最左边,向下移动两个像素
		if(SIZE ==16)
			{
			OLED_Set_Pos(x,y);	//设置字符显示的起始地址
			for(i=0;i<8;i++)    //将一个字节的8个位按位传送出去
				OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);   //F8X16是取字模的数组,这个传送方式要看具体的取模方式
			OLED_Set_Pos(x,y+1);  //向下移动一个地址页page
			for(i=0;i<8;i++)
				OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
			}
		else {	
				OLED_Set_Pos(x,y+1);
				for(i=0;i<6;i++)
				OLED_WR_Byte(F6x8[c][i],OLED_DATA);
				
			}
}

想要分析上面显示代码的位置分配原因,需要考虑取字模的方式
在这里插入图片描述


{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01},/*"-",0*/
/* (8 X 16 , 隶书 )*/

从字模库中找到字符“-”的矩阵反推出取模方式
请添加图片描述

取模方式,低位先取到
在这里插入图片描述
下面是驱动中的像素分配地址方式,
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

			/*传入显示的数据*/
			OLED_Set_Pos(x,y);	//设置字符显示的起始地址
			for(i=0;i<8;i++)    //将一个字节的8个位按位传送出去
				OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);   //F8X16是取字模的数组,这个传送方式要看具体的取模方式
			OLED_Set_Pos(x,y+1);  //向下移动一个地址页page
			for(i=0;i<8;i++)
				OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
				
			/*设置地址OLED_Set_Pos中的纵轴方向设置,下面手册中命令可以看出*/
			OLED_WR_Byte(0xb0+y,OLED_CMD);	

这里设置的特定的模式,第二个16进制位表示进入的page
在这里插入图片描述

上面一段代码的写入字节过程如下图所示,F8X16[c*16+i],其中c*16是一个字符占16字节,所以得到了字符索引,一个字符中的16个字节需要按字节送入,所以才有偏置i。当前8个字节的数据传送完,需要在驱动中换page。**OLED_Set_Pos(x,y+1);**的作用不只是换一行,而是以字节为单位换行,也就是换page
在这里插入图片描述

字符串传送

//实际上就是将字符串看作指针,对每个字符遍历显示,直到最后一位
void OLED_ShowString(u8 x,u8 y,u8 *chr)
{
	unsigned char j=0;
	while (chr[j]!='\0')
	{		OLED_ShowChar(x,y,chr[j]);
			x+=8;   //每个字符横向占8个位
			//到屏幕最右边若不够放下一个字符,会跳转到下一行显示,y+=2是因为,一个字符高度是两个字节
		if(x>120){x=0;y+=2;} 
			j++;
	}
}

个人总结

整体来看,好像有很多命令。但按照手册中的介绍初始化还是很方便的。
数据手册的查看很重要。显示屏的正常显示需要很清楚知道硬件取模和软件驱动的数据写入方式
屏幕的显示很依赖字符或图片的取模方式和硬件的实际接线方式。
对于驱动的控制有两种方式:传入命令或数据。传入命令或数据的判断是通过给显示屏一个引脚高低电平控制的

Pin Number Symbol I/O Function P Po o we er r S Su up pp pl ly y 9 VDD P P Po o we er r S Su up pp pl ly y f fo or r L Lo og gi ic c This is a voltage supply pin. It must be connected to external source. 8 VSS P G Gr ro ou un nd d o of f L Lo og gi ic c C Ci ir rc cu ui it t This is a ground pin. It acts as a reference for the logic pins. It must be connected to external ground. 28 VCC P P Po o we er r S Su up pp pl ly y f fo or r OE EL L P Pa an ne el l This is the most positive voltage supply pin of the chip. A stabilization capacitor should be connected between this pin and V SS when the converter is used. It must be connected to external source when the converter is not used. 29 VLSS P G Gr ro ou un nd d o of f A An na al lo og g C Ci ir rc cu ui it t This is an analog ground pin. It should be connected to V SS externally. D Dr ri iv ve er r 26 IREF I C Cu ur rr re en nt t R Re ef fe er re en nc ce e f fo or r B Br ri ig gh ht tn ne es ss s A Ad dj ju us st t me en nt t This pin is segment current reference pin. A resistor should be connected between this pin and V SS . Set the current at 12.5μA maximum. 27 VCOMH O V Vo ol lt ta ag ge e Ou ut tp pu ut t Hi ig gh h L Le ev ve el l f fo or r C C O M S Si ig gn na al l This pin is the input pin for the voltage output high level for COM signals. A capacitor should be connected between this pin and V SS . D DC C/ /D DC C C Co on nv ve er rt te er r 6 VDDB P P Po o we er r S Su up pp pl ly y f fo or r DC C/ / DC C C Co on nv ve er rt te er r C Ci ir rc cu ui it t This is the power supply pin for the internal buffer of the DC/DC voltage converter. It must be connected to external source when the converter is used. It should be connected to V DD when the converter is not used. 4 / 5 2 / 3 C1P / C1N C2P / C2N I P Po os si it ti iv ve e T Te er r mi in na al l o of f t th he e F Fl ly yi in ng g I In nv ve er rt ti in ng g C Ca ap pa ac ci it to or r Ne eg ga at ti iv ve e T Te er r mi in na al l o of f t th he e F Fl ly yi in ng g B Bo oo os st t C Ca ap pa ac ci it to or r The charge-pump capacitors are required between the terminals. They must be floated when the converter is not used. I In nt te er rf fa ac ce e 10 11 12 BS0 BS1 BS2 I C Co o m mu un ni ic ca at ti in ng g P Pr ro ot to oc co ol l S Se el le ec ct t These pins are MCU interface selection input. See the following table: BS0 BS1 BS2 I 2 C 0 1 0 3-wire SPI 1 0 0 4-wire SPI 0 0 0 8-bit 68XX Parallel 0 0 1 8-bit 80XX Parallel 0 1 1 14 RES# I P Po o we er r R Re es se et t f fo or r C Co on nt tr ro ol ll le er r a an nd d Dr ri iv ve er r This pin is reset signal input. When the pin is low, initialization of the chip is executed. Keep this pin pull high during normal operation. 13 CS# I C Ch hi ip p S Se el le ec ct t This pin is the chip select input. The chip is enabled for MCU communication only when CS# is pulled low. 15 D/C# I Da at ta a/ /C Co o m ma an nd d C Co on nt tr ro ol l This pin is Data/Command control pin. When the pin is pulled high, the input at D7~D0 is treated as display data. When the pin is pulled low, the input at D7~D0 will be transferred to the command register. When the pin is pulled high and serial interface mode is selected, the data at SDIN will be interpreted as data. When it is pulled low, the data at SDIN will be transferred to the command register. In I 2 C mode, this pin acts as SA0 for slave address selection. For detail relationship to MCU interface signals, please refer to the Timing Characteristics Diagrams. 17 E/RD# I R Re ea ad d/ / Wr ri it te e E En na ab bl le e o or r R Re ea ad d This pin is MCU interface input. When interfacing to a 68XX-series microprocessor, this pin will be used as the Enable (E) signal. Read/write operation is initiated when this pin is pulled high and the CS# is pulled low. When connecting to an 80XX-microprocessor, this pin receives the Read (RD#) signal. Data read operation is initiated when this pin is pulled low and CS# is pulled low. When serial or I 2 C mode is selected, this pin must be connected to V SS . GoldenMorning Electronic 4 1.5 Pin Definition (Continued) Pin Number Symbol I/O Function I In nt te er rf fa ac ce e ( (C Co on nt ti in nu ue ed d) ) 16 R/W# I R Re ea ad d/ / Wr ri it te e S Se el le ec ct t o or r Wr ri it te e This pin is MCU interface input. When interfacing to a 68XX-series microprocessor, this pin will be used as Read/Write (R/W#) selection input. Pull this pin to “High” for read mode and pull it to “Low” for write mode. When 80XX interface mode is selected, this pin will be the Write (WR#) input. Data write operation is initiated when this pin is pulled low and the CS# is pulled low. When serial or I 2 C mode is selected, this pin must be connected to V SS . 18~25 D0~D7 I/O Ho os st t Da at ta a I In np pu ut t/ / Ou ut tp pu ut t B Bu us s These pins are 8-bit bi-directional data bus to be connected to the microprocessor’s data bus. When serial mode is selected, D1 will be the serial data input SDIN and D0 will be the serial clock input SCLK. When I 2 C mode is selected, D2 & D1 should be tired together and serve as SDA out & SDA in in application and D0 is the serial clock input SCL. Unused pins must be connected to V SS except for D2 in serial mode. R Re es se er rv ve e 7 N.C. - R Re es se er rv ve ed d P Pi in n The N.C. pin between function pins are reserved for compatible and flexible design. 1, 30 N.C. (GND) - R Re es se er rv ve ed d P Pi in n ( (S Su up pp po or rt ti in ng g P Pi in n) ) The supporting pins can reduce the influences from stresses on the function pins. These pins must be connected to external ground as the ESD protection circuit.
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值