[OLED driver]: Routine for displaying numbers, letters, Chinese characters, and pictures on a 0.96-inch OLED screen driven by stm32F103's four-wire analog IIC


Article directory

Preface

1. What is a four-line OLED?

2. IIC principle

1. The four-line 0.96-inch OLED is driven by IIC to realize the display

2. OLED screen with IIC protocol

3.STM32 driver code

4.IIC usage code

Summarize


Preface

        Recently I am working on an open source solution - a balancing car. The hardware and software will be open sourced soon and will be open sourced on CSDN. Today's article is mainly to describe the OLED module on the board, which uses an analog IIC driving method. The driver method is simple, effective, and suitable for the U8g2 library.

        OLED stands for Organic Light-Emitting Diode, also known as Organic Electroluminesence Display (OELD). OLEDs are considered to be the best because they are self-luminous and do not require a backlight, have high contrast, are thin, have wide viewing angles, have fast response times, can be used in flexible panels, have a wide operating temperature range, and have simple structures and processes. Next-generation flat-panel display emerging application technology.

        OLED display technology is self-luminous and uses very thin organic material coatings and glass substrates. When current passes through, these organic materials will emit light. The OLED display screen has a large viewing angle and can save power.


1. What is a four-line OLED?

       

Pin description:

                GND common ground pin 
                VCC power supply pin, usually 3.3V DC power supply
                SCL clock line in iic bus
                SDA data line in iic bus

2. IIC principle

1. The four-line 0.96-inch OLED is driven by IIC to realize the display

        iic bus protocol:

        The IIC (Inter-Integrated Circuit) protocol , also known as the I2C (Inter-IC) protocol, is a serial communication protocol used for communication between integrated circuits (ICs). It was developed by Philips Semiconductors (now NXP Semiconductors) in 1982 to simplify communication between multiple chips.

The IIC protocol uses two wires for communication: the clock line (SCL) and the data line (SDA). This two-wire communication method makes the IIC protocol ideal for connecting multiple devices, because each device can be connected to the bus through the same wire.

In the IIC protocol, communication is between a master device (usually a microcontroller or processor) and a slave device. The master device is responsible for controlling the communication on the bus, while the slave device passively responds to the commands or requests of the master device.

The communication process of IIC protocol is as follows:

  1. The master device sends a start signal (Start): the master device keeps the clock line high, and then generates a falling edge on the data line, indicating the start of the start signal.
  2. Master Sends Device Address and Read/Write Bits: The master sends the address of the slave to communicate with and specifies a read (1) or write (0) operation.
  3. Slave Response: The slave that matches the address responds to the master and acknowledges receipt of the address.
  4. Data transmission: Data transmission occurs between the master device and the slave device. Each data byte is transmitted on the data line and sampled on the edge of the clock.
  5. Stop signal (Stop): The master device sends a stop signal, that is, while the clock line remains high, a rising edge is generated on the data line.

The IIC protocol has the following characteristics:

  • Simple: use only two wires for communication.
  • Multi-device support: Multiple devices can be connected via the same bus.
  • Flexible speed: Different clock frequencies can be selected according to needs.
  • Two-way communication: the master device can send commands to the slave device and read data from the slave device.

The IIC protocol is widely used in various fields, such as communication between sensors, memories, displays and other integrated circuits. It is widely adopted because of its simplicity and flexibility, which makes communication between devices more convenient and reliable.

As shown in the figure below, the protocol stipulates that when the SCLK clock signal is always at a high level, the SDA line jumps from high level to low level, indicating the start signal. Note that even if the level transition of the SDA data line is complete, the SCLK line is still high. When the peripheral module connected to the IIC bus detects this signal, it knows that the data is about to be transmitted. The same is true for the end signal. The protocol stipulates that when the SCLK clock signal is always at a high level, the SDA line jumps from low level to high level, indicating the end signal.

2. OLED screen with IIC protocol

The main MCU I use is STM32F103RCT6 as shown below:

i.e. define the pins:

OLED working principle of IIC protocol (SSD1306):

  • The ssd1306 itself supports a variety of bus driver modes including SPI and parallel ports. You can select which interface by pulling the corresponding IO port of the chip low and high. In this example, the module solidifies the corresponding IO port configuration through a resistor and uses the I2C interface method, but Maybe the module you bought with the same driver chip will use other interfaces
  • When using the I2C interface, the SSD1306 allows up to two 7-bit I2C addresses, which can also be switched by pulling the corresponding IO port low and high. The general default is 0x3c. On the back of the screen module, you can see an I2C address switching prompt. When you need to change the I2C address of the module, you only need to remove the resistor at the prompt position and solder it to the other end. It should be noted that the I2C address on the board is the value after adding the zeroth read and write bit, that is,0x78 = 0x3c<<1 0x7A = 0x3d<<1

3.STM32 driver code

/*引脚初始化*/
void OLED_I2C_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
 	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
 	GPIO_Init(GPIOC, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
 	GPIO_Init(GPIOC, &GPIO_InitStructure);
	
	OLED_W_SCL(1);
	OLED_W_SDA(1);
}

4.IIC usage code

/**
  * @brief  I2C开始
  * @param  无
  * @retval 无
  */
void OLED_I2C_Start(void)
{
	OLED_W_SDA(1);
	OLED_W_SCL(1);
	OLED_W_SDA(0);
	OLED_W_SCL(0);
}

/**
  * @brief  I2C停止
  * @param  无
  * @retval 无
  */
void OLED_I2C_Stop(void)
{
	OLED_W_SDA(0);
	OLED_W_SCL(1);
	OLED_W_SDA(1);
}

/**
  * @brief  I2C发送一个字节
  * @param  Byte 要发送的一个字节
  * @retval 无
  */
void OLED_I2C_SendByte(uint8_t Byte)
{
	uint8_t i;
	for (i = 0; i < 8; i++)
	{
		OLED_W_SDA(Byte & (0x80 >> i));
		OLED_W_SCL(1);
		OLED_W_SCL(0);
	}
	OLED_W_SCL(1);	//额外的一个时钟,不处理应答信号
	OLED_W_SCL(0);
}

/**
  * @brief  OLED写命令
  * @param  Command 要写入的命令
  * @retval 无
  */
void OLED_WriteCommand(uint8_t Command)
{
	OLED_I2C_Start();
	OLED_I2C_SendByte(0x78);		//从机地址
	OLED_I2C_SendByte(0x00);		//写命令
	OLED_I2C_SendByte(Command); 
	OLED_I2C_Stop();
}

/**
  * @brief  OLED写数据
  * @param  Data 要写入的数据
  * @retval 无
  */
void OLED_WriteData(uint8_t Data)
{
	OLED_I2C_Start();
	OLED_I2C_SendByte(0x78);		//从机地址
	OLED_I2C_SendByte(0x40);		//写数据
	OLED_I2C_SendByte(Data);
	OLED_I2C_Stop();
}

/**
  * @brief  OLED设置光标位置
  * @param  Y 以左上角为原点,向下方向的坐标,范围:0~7
  * @param  X 以左上角为原点,向右方向的坐标,范围:0~127
  * @retval 无
  */
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
	OLED_WriteCommand(0xB0 | Y);					//设置Y位置
	OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4));	//设置X位置低4位
	OLED_WriteCommand(0x00 | (X & 0x0F));			//设置X位置高4位
}

/**
  * @brief  OLED清屏
  * @param  无
  * @retval 无
  */
void OLED_Clear(void)
{  
	u8 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); 
	} //更新显示
}


/**
  * @brief  OLED显示一个字符
  * @param  Line 行位置,范围:1~4
  * @param  Column 列位置,范围:1~16
  * @param  Char 要显示的一个字符,范围:ASCII可见字符
  * @retval 无
  */
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{      	
	uint8_t i;
	OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8);		//设置光标位置在上半部分
	for (i = 0; i < 8; i++)
	{
		OLED_WriteData(OLED_F8x16[Char - ' '][i]);			//显示上半部分内容
	}
	OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8);	//设置光标位置在下半部分
	for (i = 0; i < 8; i++)
	{
		OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]);		//显示下半部分内容
	}
}

/**
  * @brief  OLED显示字符串
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  String 要显示的字符串,范围:ASCII可见字符
  * @retval 无
  */
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i++)
	{
		OLED_ShowChar(Line, Column + i, String[i]);
	}
}


void OLED_WR_Byte(unsigned dat,unsigned cmd)
{
	if(cmd)
	{
		OLED_WriteData(dat);
	}
	else {
		OLED_WriteCommand(dat);
	}
}
//坐标设置
void OLED_Set_Pos(unsigned char x, unsigned char y) 
{ 	OLED_WR_Byte(0xb0+y,OLED_CMD);
	OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
	OLED_WR_Byte((x&0x0f),OLED_CMD); 
} 

//显示汉字
void OLED_ShowCHinese(u8 x,u8 y,u8 no)
{      			    
	u8 t,adder=0;
	OLED_Set_Pos(x,y);	
	for(t=0;t<16;t++)
	{
		OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
		adder+=1;
	}	
	OLED_Set_Pos(x,y+1);	
	for(t=0;t<16;t++)
	{	
		OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
		adder+=1;
	}					
}
/***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/
void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
{ 	
 unsigned int j=0;
 unsigned char x,y;
  
  if(y1%8==0) y=y1/8;      
  else y=y1/8+1;
	for(y=y0;y<y1;y++)
	{
		OLED_Set_Pos(x0,y);
    for(x=x0;x<x1;x++)
	    {      
	    	OLED_WR_Byte(BMP[j++],OLED_DATA);	    	
	    }
	}
} 


/**
  * @brief  OLED次方函数
  * @retval 返回值等于X的Y次方
  */
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;
	while (Y--)
	{
		Result *= X;
	}
	return Result;
}

/**
  * @brief  OLED显示数字(十进制,正数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~4294967295
  * @param  Length 要显示数字的长度,范围:1~10
  * @retval 无
  */
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i++)							
	{
		OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
	}
}

/**
  * @brief  OLED显示数字(十进制,带符号数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:-2147483648~2147483647
  * @param  Length 要显示数字的长度,范围:1~10
  * @retval 无
  */
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
	uint8_t i;
	uint32_t Number1;
	if (Number >= 0)
	{
		OLED_ShowChar(Line, Column, '+');
		Number1 = Number;
	}
	else
	{
		OLED_ShowChar(Line, Column, '-');
		Number1 = -Number;
	}
	for (i = 0; i < Length; i++)							
	{
		OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
	}
}

/**
  * @brief  OLED显示数字(十六进制,正数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~0xFFFFFFFF
  * @param  Length 要显示数字的长度,范围:1~8
  * @retval 无
  */
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
	uint8_t i, SingleNumber;
	for (i = 0; i < Length; i++)							
	{
		SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
		if (SingleNumber < 10)
		{
			OLED_ShowChar(Line, Column + i, SingleNumber + '0');
		}
		else
		{
			OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
		}
	}
}

/**
  * @brief  OLED显示数字(二进制,正数)
  * @param  Line 起始行位置,范围:1~4
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~1111 1111 1111 1111
  * @param  Length 要显示数字的长度,范围:1~16
  * @retval 无
  */
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i++)							
	{
		OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
	}
}


/**
  * @brief  OLED初始化
  * @param  无
  * @retval 无
  */
void OLED_Init(void)
{
	uint32_t i, j;
	
	for (i = 0; i < 1000; i++)			//上电延时
	{
		for (j = 0; j < 1000; j++);
	}
	
	OLED_I2C_Init();			//端口初始化
	
	OLED_WriteCommand(0xAE);	//关闭显示
	
	OLED_WriteCommand(0xD5);	//设置显示时钟分频比/振荡器频率
	OLED_WriteCommand(0x80);
	
	OLED_WriteCommand(0xA8);	//设置多路复用率
	OLED_WriteCommand(0x3F);
	
	OLED_WriteCommand(0xD3);	//设置显示偏移
	OLED_WriteCommand(0x00);
	
	OLED_WriteCommand(0x40);	//设置显示开始行
	
	OLED_WriteCommand(0xA1);	//设置左右方向,0xA1正常 0xA0左右反置
	
	OLED_WriteCommand(0xC8);	//设置上下方向,0xC8正常 0xC0上下反置

	OLED_WriteCommand(0xDA);	//设置COM引脚硬件配置
	OLED_WriteCommand(0x12);
	
	OLED_WriteCommand(0x81);	//设置对比度控制
	OLED_WriteCommand(0xCF);

	OLED_WriteCommand(0xD9);	//设置预充电周期
	OLED_WriteCommand(0xF1);

	OLED_WriteCommand(0xDB);	//设置VCOMH取消选择级别
	OLED_WriteCommand(0x30);

	OLED_WriteCommand(0xA4);	//设置整个显示打开/关闭

	OLED_WriteCommand(0xA6);	//设置正常/倒转显示

	OLED_WriteCommand(0x8D);	//设置充电泵
	OLED_WriteCommand(0x14);

	OLED_WriteCommand(0xAF);	//开启显示
		
	OLED_Clear();				//OLED清屏
}




Summarize

        The basic usage is mentioned in the above code, please refer to it. In addition, the explanation of IIC is not very professional. You can refer to various teaching videos on a certain website. There are too many teaching resources, so these parts will not be introduced in detail.

        Engineering link: OLED


Nothing replaces hard work

Guess you like

Origin blog.csdn.net/oxygen23333/article/details/132683475