当先锋百科网

首页 1 2 3 4 5 6 7

stm32 HAL库OLE相关驱动函数

4pin OLED显示字符串,单个字符等
stm32cubemx的配置只需要打开相关I2C,其余参数默认即可
在这里插入图片描述
GPIO相关代码(写在oled.c中,函数声明在.h中 )
发送数据函数

static void Write_IIC_Data(uint8_t IIC_Data)
{
	uint8_t *dat;
	dat = &IIC_Data;
	HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,
										I2C_MEMADD_SIZE_8BIT,
										dat,1,100);
}

发送命令函数

static void Write_IIC_Data(uint8_t IIC_Data)
{
	uint8_t *dat;
	dat = &IIC_Data;
	HAL_I2C_Mem_Write(&hi2c1,0x78,0x40,
										I2C_MEMADD_SIZE_8BIT,
										dat,1,100);
}

设置坐标函数

static void OLED_Set_Pos(uint8_t x,uint8_t y) 
{ 
	Write_IIC_Command(0xb0+y);
	Write_IIC_Command(((x&0xf0)>>4)|0x10);
	Write_IIC_Command((x&0x0f));
} 

清屏函数(清屏后屏幕是黑个 跟没点亮一样)

static void OLED_Set_Pos(uint8_t x,uint8_t y) 
{ 
	Write_IIC_Command(0xb0+y);
	Write_IIC_Command(((x&0xf0)>>4)|0x10);
	Write_IIC_Command((x&0x0f));
} 

void OLED_Clear(void)  
{  
	uint8_t i,n;		    
	for(i=0;i<8;i++)  
	{  
		Write_IIC_Command (0xb0+i);    //设置页地址(0~7)
		Write_IIC_Command (0x00);      //设置显示位置—列低地址
		Write_IIC_Command (0x10);      //设置显示位置—列高地址   
		
		for(n=0;n<128;n++){
			Write_IIC_Data(0x00); 
	} //更新显示
}
}

初始化函数 初始化以后调用了清屏函数 ,主函数里初始化后就不用重新手动调用了

void OLED_Init(void)
{ 	
	HAL_Delay(100);
	Write_IIC_Command(0XAE);
	Write_IIC_Command(0x20);//--display off
	Write_IIC_Command(0x10);//---set low column address
	Write_IIC_Command(0xb0);//---set high column address
	Write_IIC_Command(0xc8);//--set start line address  
	Write_IIC_Command(0x00);//--set page address
	Write_IIC_Command(0x10); // contract control
	Write_IIC_Command(0x40);//--128   
	Write_IIC_Command(0x81);//set segment remap 
	Write_IIC_Command(0xff);//--normal / reverse
	Write_IIC_Command(0xA1);//--set multiplex ratio(1 to 64)
	Write_IIC_Command(0xa6);//--1/32 duty
	Write_IIC_Command(0xa8);//Com scan direction
	Write_IIC_Command(0x3f);//-set display offset
	Write_IIC_Command(0xa4);//
	
	Write_IIC_Command(0xD3);//set osc division
	Write_IIC_Command(0x00);//
	
	Write_IIC_Command(0xd5);//set area color mode off
	Write_IIC_Command(0xF0);//
	
	Write_IIC_Command(0xD9);//Set Pre-Charge Period
	Write_IIC_Command(0x22);//
	
	Write_IIC_Command(0xDA);//set com pin configuartion
	Write_IIC_Command(0x12);//
	
	Write_IIC_Command(0xdb);//set Vcomh
	Write_IIC_Command(0x20);//
	
	Write_IIC_Command(0x8d);//set charge pump enable
	Write_IIC_Command(0x14);//
	
	Write_IIC_Command(0xaf);//--turn on oled panel
	
	OLED_Clear();
}

显示单个字符函数

void oled_display_char(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
{          
    unsigned char c=0,i=0;  
    c=chr-' ';      
    if(x>128-1){x=0;y=y+2;}
    if(Char_Size ==16)
    {
        OLED_Set_Pos(x,y);   
        for(i=0;i<8;i++)
        Write_IIC_Data(F8X16[c*16+i]);
        OLED_Set_Pos(x,y+1);
        for(i=0;i<8;i++)
        Write_IIC_Data(F8X16[c*16+i+8]);
    }
    else if(Char_Size ==12)
    {   
        OLED_Set_Pos(x,y);
        for(i=0;i<6;i++)
        Write_IIC_Data(F6x8[c*6+i][i]);          
    }
}

显示字符串函数

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

最后 只需要在主函数里调用相关的函数即可
在这里插入图片描述