stm32 IIC oled 介绍

本文详细介绍了IIC(Inter-Integrated Circuit)总线的工作原理,包括其信号类型如开始信号、结束信号和应答信号。此外,文章还深入探讨了如何使用IIC总线与OLED屏幕进行通信,提供了具体的代码示例,涵盖了OLED屏幕的初始化、数据写入、显示字符和位图等操作。

IIC 简介

IIC oled 介绍找另一篇文章,这里主要有IIC,和如何编写oled.c代码
IIC(Inter-Integrated Circuit)总线是一种由 PHILIPS 公司开发的两线式 总线,
用于连接微控制器及其外围设备。它是由数据线 SDA 和时钟 SCL 构成的串行总线, 可发送和接收数据。

在 CPU 与被控 IC 之间、IC 与 IC 之间进行双向传送,高速 IIC 总线一般可达 400kbps 以上。
I2C 总线在传送数据过程中共有三种类型信号, 它们分别是:开始信号、结束信号和应答信号。

开始信号:SCL 为高电平时,SDA 由高电平向低电平跳变,开始传送数据。
结束信号:SCL 为高电平时,SDA 由低电平向高电平跳变,结束传送数据。
应答信号:接收数据的 IC 在接收到 8bit 数据后,向发送数据的 IC 发出特定 的低电平脉
冲,表示已收到数据。CPU 向受控单元发出一个信号后,等待受控单元发出一个应答信号,CPU 接收到应答信号后,根据实际情况作出是否继续传递信号的判断。若未收到应答信号,由判断为 受控单元出现故障。
这些信号中,起始信号是必需的,结束信号和应答信号,都可以不要

软件设计

  • IIC设置
void I2C_Configuration(void)
{
	I2C_InitTypeDef  I2C_InitStructure;
	GPIO_InitTypeDef  GPIO_InitStructure; 

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

	/*STM32F103C8T6芯片的硬件I2C: PB6 -- SCL; PB7 -- SDA */
	GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode =  GPIO_Mode_AF_OD;//I2C必须开漏输出
	GPIO_Init(GPIOB, &GPIO_InitStructure);

	I2C_DeInit(I2C1);//使用I2C1
	I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
	I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
	I2C_InitStructure.I2C_OwnAddress1 = 0x30;//主机的I2C地址,随便写的
	I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
	I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
	I2C_InitStructure.I2C_ClockSpeed = 400000;//400K

	I2C_Cmd(I2C1, ENABLE);
	I2C_Init(I2C1, &I2C_InitStructure);
}
  • IIC传送数据
void I2C_WriteByte(uint8_t addr,uint8_t data)
{
  while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
	
	I2C_GenerateSTART(I2C1, ENABLE);//开启I2C1
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/

	I2C_Send7bitAddress(I2C1, OLED_ADDRESS, I2C_Direction_Transmitter);//器件地址 -- 默认0x78
	while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));

	I2C_SendData(I2C1, addr);//寄存器地址
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

	I2C_SendData(I2C1, data);//发送数据
	while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
	
	I2C_GenerateSTOP(I2C1, ENABLE);//关闭I2C1总线
}

oled的写命令和数据

void WriteCmd(unsigned char I2C_Command)//写命令
{
	I2C_WriteByte(0x00, I2C_Command);
}

void WriteDat(unsigned char I2C_Data)//写数据
{
	I2C_WriteByte(0x40, I2C_Data);
}

OLEd初始化


void OLED_Init(void)
{
	delay_ms(100); //这里的延时很重要
	
	WriteCmd(0xAE); //开显示
	WriteCmd(0x20);	//设置内存寻址模式	
	WriteCmd(0x10);	//00,水平寻址模式;01,垂直寻址模式;10,页面寻址模式(重置);11,无效
	WriteCmd(0xb0);	//为页面寻址模式设置页面开始地址,0-7
	WriteCmd(0xc8);	//设置COM输出扫描方向
	WriteCmd(0x00); //---设置低列地址
	WriteCmd(0x10); //---设置高列地址
	WriteCmd(0x40); //--设置起始行地址
	WriteCmd(0x81); //--set contrast control register
	WriteCmd(0xff); //亮度调节 0x00~0xff
	WriteCmd(0xa1); //--设置段重新映射0到127
	WriteCmd(0xa6); //--设置正常显示
	WriteCmd(0xa8); //--设置复用比(1 ~ 64)
	WriteCmd(0x3F); //
	WriteCmd(0xa4); //0xa4,输出遵循RAM内容;0xa5,Output忽略RAM内容
	WriteCmd(0xd3); //-设置显示抵消
	WriteCmd(0x00); //-not offset
	WriteCmd(0xd5); //--设置显示时钟分频/振荡器频率
	WriteCmd(0xf0); //--设置分率
	WriteCmd(0xd9); //--设置pre-charge时期
	WriteCmd(0x22); //
	WriteCmd(0xda); //--设置com大头针硬件配置
	WriteCmd(0x12);
	WriteCmd(0xdb); //--设置vcomh
	WriteCmd(0x20); //0x20,0.77xVcc
	WriteCmd(0x8d); //--设置DC-DC
	WriteCmd(0x14); //
	WriteCmd(0xaf); //--打开oled面板
}

设置起始点坐标

void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{ 
	WriteCmd(0xb0+y);
	WriteCmd(((x&0xf0)>>4)|0x10);
	WriteCmd((x&0x0f)|0x01);
}

全屏填充

void OLED_Fill(unsigned char fill_Data)//全屏填充
{
	unsigned char m,n;
	for(m=0;m<8;m++)
	{
		WriteCmd(0xb0+m);		//page0-page1
		WriteCmd(0x00);		//low column start address
		WriteCmd(0x10);		//high column start address
		for(n=0;n<128;n++)
			{
				WriteDat(fill_Data);
			}
	}
}

清屏

void OLED_CLS(void)//清屏
{
	OLED_Fill(0x00);
}

将OLED从休眠中唤醒

//--------------------------------------------------------------
void OLED_ON(void)
{
	WriteCmd(0X8D);  //设置电荷泵
	WriteCmd(0X14);  //开启电荷泵
	WriteCmd(0XAF);  //OLED唤醒
}

让OLED休眠 – 休眠模式下,OLED功耗不到10uA

//--------------------------------------------------------------
void OLED_OFF(void)
{
	WriteCmd(0X8D);  //设置电荷泵
	WriteCmd(0X10);  //关闭电荷泵 
	WriteCmd(0XAE);  //OLED休眠
}

显示codetab.h中的ASCII字符,有68和816可选择

/

/--------------------------------------------------------------
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
	unsigned char c = 0,i = 0,j = 0;
	switch(TextSize)
	{
		case 1:
		{
			while(ch[j] != '\0')
			{
				c = ch[j] - 32;
				if(x > 126)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<6;i++)
					WriteDat(F6x8[c][i]);
				x += 6;
				j++;
			}
		}break;
		case 2:
		{
			while(ch[j] != '\0')
			{
				c = ch[j] - 32;
				if(x > 120)
				{
					x = 0;
					y++;
				}
				OLED_SetPos(x,y);
				for(i=0;i<8;i++)
					WriteDat(F8X16[c*16+i]);
				OLED_SetPos(x,y+1);
				for(i=0;i<8;i++)
					WriteDat(F8X16[c*16+i+8]);
				x += 8;
				j++;
			}
		}break;
	}
}

//--------------------------------------------------------------
// Prototype      : void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
// Calls          : 
// Parameters     : x,y -- 起始点坐标(x:0~127, y:0~7); N:汉字在codetab.h中的索引
// Description    : 显示codetab.h中的汉字,16*16点阵
//--------------------------------------------------------------
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
	unsigned char wm=0;
	unsigned int  adder=32*N;
	OLED_SetPos(x , y);
	for(wm = 0;wm < 16;wm++)
	{
		WriteDat(F16x16[adder]);
		adder += 1;
	}
	OLED_SetPos(x,y + 1);
	for(wm = 0;wm < 16;wm++)
	{
		WriteDat(F16x16[adder]);
		adder += 1;
	}
}

//--------------------------------------------------------------
// Prototype      : void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]);
// Calls          : 
// Parameters     : x0,y0 -- 起始点坐标(x0:0~127, y0:0~7); x1,y1 -- 起点对角线(结束点)的坐标(x1:1~128,y1:1~8)
// Description    : 显示BMP位图
//--------------------------------------------------------------
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_SetPos(x0,y);
    for(x=x0;x<x1;x++)
		{
			WriteDat(BMP[j++]);
		}
	}
}
### STM32 IIC OLED HAL Library Usage For interfacing an OLED display with the STM32 microcontroller using the IIC (Inter-Integrated Circuit) protocol, one can utilize the U8G2 library alongside the Hardware Abstraction Layer (HAL) provided by STMicroelectronics to simplify development work[^1]. The following sections detail how this setup is achieved. #### Initializing the IIC Interface To establish communication between the STM32 and the OLED screen via IIC, initialization of both hardware components must be performed correctly. This involves configuring pins as well as setting up parameters such as clock speed within the CubeMX tool or directly through code: ```c // Initialize I2C peripheral configuration structure. I2C_HandleTypeDef hi2c1; void MX_I2C1_Init(void) { hi2c1.Instance = I2C1; hi2c1.Init.Timing = 0x20909CEC; // Adjust timing according to your system requirements hi2c1.Init.OwnAddress1 = 0; hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hi2c1.Init.OwnAddress2 = 0; hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&hi2c1) != HAL_OK) { Error_Handler(); } } ``` This function sets up the necessary configurations for operating the IIC interface at a specific frequency while ensuring compatibility with most standard-mode devices like typical OLED displays which operate on 7-bit addressing mode. #### Configuring the U8G2 Library After initializing the IIC bus, attention turns towards preparing the software side responsible for rendering graphics onto the connected OLED panel. Here’s where integrating the U8G2 library comes into play – it offers high-level functions that abstract away complexities associated with low-level drawing operations: ```c U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE); void init_oled() { u8g2.begin(); // Start the U8G2 library after all other initializations have been completed successfully // Optionally set default font size here... } int main(void) { // ... Other startup routines ... init_oled(); while(1){ draw_something_on_display(); delay_ms(50); } return 0; } ``` In this example snippet above, `init_oled()` initializes the connection between MCU and SSD1306-based OLED module over IIC before entering infinite loop dedicated solely toward updating visual content periodically without blocking further execution flow elsewhere inside application firmware. #### Drawing Text and Shapes With everything configured properly now, developers may proceed creating visually appealing interfaces leveraging built-in primitives offered out-of-the-box courtesy of aforementioned third-party graphic toolkit mentioned earlier: ```c void draw_something_on_display(){ u8g2.clearBuffer(); // Clear entire framebuffer u8g2.setFont(u8g2_font_ncenB08_tr); // Select desired typeface style/size combination u8g2.drawStr(0 ,10,"Hello World!"); // Print string starting from coordinates X=0,Y=10 relative top-left corner u8g2.drawLine(0,20,128,20); // Draw horizontal line across full width spanning Y-coordinate value equals twenty pixels down vertically below origin point u8g2.sendBuffer(); // Send updated image data stored temporarily in RAM buffer memory area managed internally by driver instance object referenced previously during instantiation phase when declaring global variable named 'u8g2' } ``` The presented C source listing demonstrates basic text output along with geometric shapes rendered interactively upon request whenever called repeatedly throughout program lifecycle thanks largely due support provided natively within chosen external dependency package specifically designed around ease-of-use principles aimed primarily hobbyists but equally suitable professional environments alike given its versatility feature-set encompassing wide range graphical capabilities beyond simple alphanumeric characters alone. --related questions-- 1. How does changing the IIC address affect communication stability? 2. What are some common pitfalls encountered when debugging IIC connections? 3. Can you provide more advanced examples utilizing bitmap images instead of plain text? 4. Is there any performance impact noticed running complex animations versus static drawings? 5. Are there alternative libraries available offering similar functionality yet better suited certain applications?
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@Hwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值