STM32 i2c drives 0.42 inch OLED

STM32 i2c drives 0.42-inch OLED. This is the solution used in the project. Debugging is also more laborious. The main thing is to match the font with the code. The next chapter will explain the process of taking the font in detail and use it in this solution.

The test code used in this article

STM32i2c drives 0.42-inch OLED, SSD1306 resources-CSDN library

There is a project that needs to use the smallest OLED for display. After choosing and choosing, I found a super small OLED of 0.42 inches. Here is a brief record of the process of debugging the OLED in the actual project.

Dimensional drawing of this OLED

The determinant diagram of the OLED

The hardware interface of the OLED

Use STM32F030F4Px to connect the OLED on the hardware

It should be noted here that PA5/PA6 uses the i2c function, and a pull-up resistor needs to be designed 

The schematic diagram is relatively simple, so I won't describe it much.

 The clock configuration is as follows. Use the internal HSI clock and multiply the frequency to 48Mhz. This is also very important and has a lot to do with the delay of the clock. If the delay function is configured incorrectly, it will lead to incorrect I2C timing, and then incorrect OLED configuration. Of course, you can also Can use external clock

 The stm32cubemx version used, this is best to use the latest version

 Regarding the configuration of I2C pins in stm32cubemx, it can be configured as an output

 

 The generated project uses the following settings

 OLED initialization function

void write_i(uint8_t cmd)
{
	OLED_send_cmd(cmd);
}
void InitSSD1306()
{
	// RES=0;
	// delay(1000);
	// RES=1;
	///delay(10);
	HAL_Delay(1000);
	write_i(0xAE); /*display off*/
	write_i(0xD5); /*set osc division*/
	write_i(0xF0);
	write_i(0xA8); /*multiplex ratio*/
	write_i(0x27); /*duty = 1/40*/
	write_i(0xD3); /*set display offset*/
	write_i(0x00);
	write_i(0x40); /*Set Display Start Line */
	write_i(0x8d); /*set charge pump enable*/
	write_i(0x14);
	write_i(0x20); /*Set page address mode*/
	write_i(0x02);
	write_i(0xA1); /*set segment remap*/
	write_i(0xC8); /*Com scan direction*/
	write_i(0xDA); /*set COM pins*/
	write_i(0x12);
	write_i(0xAD); /*Internal IREF Setting*/
	write_i(0x30);
	write_i(0x81); /*contract control*/
	write_i(0x2F); /*128*/
	write_i(0xD9); /*set pre-charge period*/
	write_i(0x22);
	write_i(0xdb); /*set vcomh*/
	write_i(0x20);
	write_i(0xA4); /*Set Entire Display On/Off*/
	write_i(0xA6); /*normal / reverse*/
	write_i(0x0C); /*set lower column address*/
	write_i(0x11); /*set higher column address*/
	write_i(0xAF); /*display ON*/
}

Reverse the screen function, this is very useful, it is more convenient when debugging

void OLED_reverse_All(unsigned char mode)//(1黑底白字,0白底黑字)
{
	if(mode){
		OLED_send_cmd(0xA6);//1亮0灭
	}else{
		OLED_send_cmd(0xA7);//1灭0亮
	}
}

In the main function, perform the operation of flashing lights and refresh the screen

	HAL_Delay(1000);
	HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_4);//LED twinkle
	OLED_full_OLED();
	HAL_Delay(1000);
	OLED_clean_OLED();
	HAL_Delay(1000);
	OLED_drawString(28, 0, "012345678901234", 8, 16);//128 = 16*8
	OLED_drawString(28, 16, "012345678901234", 8, 16);//128 = 16*8
	OLED_drawString(28, 32, "012345678901234", 8, 16);//128 = 16*8
	OLED_display();

The use of I2C in the hal library requires input and output configuration of the SDA pin. Here, there is no direct operation on the register, but the library function is used, which can also meet the usage requirements.

#define IIC_SDA_PIN GPIO_PIN_6
#define IIC_SCL_PIN GPIO_PIN_5

#define IIC_SDA_H HAL_GPIO_WritePin(IIC_SDA, IIC_SDA_PIN, GPIO_PIN_SET)
#define IIC_SDA_L HAL_GPIO_WritePin(IIC_SDA, IIC_SDA_PIN, GPIO_PIN_RESET)

#define IIC_SCL_H HAL_GPIO_WritePin(IIC_SCL, IIC_SCL_PIN, GPIO_PIN_SET)
#define IIC_SCL_L HAL_GPIO_WritePin(IIC_SCL, IIC_SCL_PIN, GPIO_PIN_RESET)


void IIC_OUT(void)//SDA是输出方向
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.Pin=IIC_SDA_PIN;
	GPIO_InitStructure.Speed=GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStructure.Mode=GPIO_MODE_OUTPUT_PP;  //推挽输出模式
	HAL_GPIO_Init(IIC_SDA,&GPIO_InitStructure);
}
void IIC_IN(void)//SDA是输入方向
{
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_InitStructure.Pin=IIC_SDA_PIN;
	GPIO_InitStructure.Speed=GPIO_SPEED_FREQ_HIGH;
	GPIO_InitStructure.Pull = GPIO_PULLUP;
	GPIO_InitStructure.Mode=GPIO_MODE_INPUT;  //输入上拉模式
	HAL_GPIO_Init(IIC_SDA,&GPIO_InitStructure);
}

The actual test effect diagram is as follows,

The test code used is as follows

STM32i2c drives 0.42-inch OLED, SSD1306 resources-CSDN library

The version number of keil is as follows

Guess you like

Origin blog.csdn.net/li171049/article/details/130527062