A learning step seven needles 0.96 inch OLED display SPI interface driver (portable code attached) - based STM32

A brief description and pin

Here Insert Picture Description
Get our hands will not use OLED, a program can not read, how to do? Began to read from the pin. If you know the pin is why, of course, be able to understand the driving code.
First look at the Pin Description :

GND VCC D0 D1 RES DC CS
Power Ground Power n (3 ~ 5.5V) SPI clock pin SPI data pins Reset pin Data and commands control pins Select pin

Obviously, GND and VCC connected to the power supply, no electricity talking about anything. D0 , D1 , for the SPI interface pins, or complete the data transfer command, for the SPI protocol is unaware of, it is recommended to learn the relevant information of the agreement, after all, it is more commonly used. CS is a chip select pin, when pulled low transmission valid . the DC it, for selecting a write data or write command or a write command down, for example, when the write data is pulled to set the contrast, set x, y coordinates, with the write command;.. when the display pixel with the write data. RES is a reset pin, active low reset down for some time during initialization achieve reset, to prepare for the follow-up work.

2 driver code

2.1 Initialization

2.1.1 Pin Configuration

Ordinary pins can all be configured to push-pull output.

2.2 OLED initialization

First reset,

    OLED_RST_Set();
	delay_ms(100);
	OLED_RST_Clr(); //拉低200ms实现复位
	delay_ms(200);
	OLED_RST_Set(); 

The next start writing to the appropriate command (this is mainly arranged SSD1306 (OLED driver IC) of the register, see the corresponding data sheet), the removal of part of the code:

	OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel(关闭oled面板显示)
	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  Set Mapping RAM Display Start Line (0x00~0x3F)
	OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register(设置对比度控制控制寄存器)
	                            //写0x81命令后,紧接着写入相应数值(0-255),数值越大亮度越大
	OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness(0xCF表示亮度大小)
	......

Finally, open oled panel, clear the screen, set the coordinates origin

	OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/ 
	OLED_Clear();
	OLED_Set_Pos(0,0);

2.2 Function Function

2.2.1 a Byte transfer function
void OLED_WR_Byte(u8 dat,u8 cmd)
{	
	u8 i;			  
	if(cmd) //cmd为1,拉高DC
	  OLED_DC_Set();
	else //cmd为0,拉低DC
	  OLED_DC_Clr();
	  		  
	OLED_CS_Clr();//拉低片选信号

	for(i=0;i<8;i++)//for循环完成一个字节的传输,每个CLK的上升沿,传输一个bit,参考spi传输时序图
	{			  
		OLED_SCLK_Clr();
		if(dat&0x80)
		   OLED_SDIN_Set();
		else 
		   OLED_SDIN_Clr();
		OLED_SCLK_Set();
		dat<<=1;   
	}				 		  
	OLED_CS_Set(); //拉高片选信号
	OLED_DC_Set(); //拉高DC 	  
} 

Transmitting a timing chart:
Here Insert Picture Description

2.2.2 Display Function of a char
void OLED_ShowChar(u8 x,u8 y,u8 chr)
{      	
	unsigned char c=0,i=0;	
		c=chr-' ';//得到偏移值			
		if(x>Max_Column-1){x=0;y=y+2;} //x超过最后一列, 换行
		if(SIZE ==16)//8*16
		{
			OLED_Set_Pos(x,y);	//设置坐标,其中x为列地址,y为页地址,参考下图分页情况
			for(i=0;i<8;i++) //显示8*16点阵的上半部分
			OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
			
			OLED_Set_Pos(x,y+1);//光标移到下一页
			for(i=0;i<8;i++) //显示8*16点阵的下半部分,参考下图显示示意图
			OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
		}
		else //6*8
		{	
			OLED_Set_Pos(x,y+1);
			for(i=0;i<6;i++)
			OLED_WR_Byte(F6x8[c][i],OLED_DATA);	
		}
}

Pagination:
Here Insert Picture Description
Character 8 * 16 format shows a schematic view, from left to right, top to bottom, the first half of the display, and then move the cursor to the next, continues to display the lower half of the
Here Insert Picture Description
other functions of the display functions, it is based on character display, are interested in their own research

3 driver code link

https://download.csdn.net/download/weixin_40134414/12302024

Write is not detailed enough not deep enough, there is a better understanding of the follow-up and then went on to add.

Released four original articles · won praise 7 · views 384

Guess you like

Origin blog.csdn.net/weixin_40134414/article/details/105308234