当先锋百科网

首页 1 2 3 4 5 6 7

一、概述

OLED驱动方式有8080、6800、3线/4线SPI以及IIC,能够显示字符、汉字的图片,无字库需通过取模软件获取显示内容数组。本次实验使用的是IIC通信协议,SSD1306驱动芯片的OLED屏。

二、实验材料

1、0.96寸OLED屏。
2、最小系统STM32F103C8T6.
3、杜邦线若干。

三、接线

功能引脚GPIO
GNDGND
VCCVCC
SCLPA6
SDAPA7

四、程序代码

1、GPIO初始化

void Oled_Init(void)
{
	IIC_Pin_Init();//iic引脚初始化
	
	OLED_Send_Command(0xAE);//--turn off oled panel
	OLED_Send_Command(0x02);//---SET low column address
	OLED_Send_Command(0x10);//---SET high column address
	OLED_Send_Command(0x40);//--SET start line address  SET Mapping RAM Display Start Line (0x00~0x3F)
	OLED_Send_Command(0x81);//--SET contrast control register
	OLED_Send_Command(0xCF); // SET SEG Output Current Brightness
	OLED_Send_Command(0xA1);//--SET SEG/Column Mapping     0xa0左右反置 0xa1正常
	OLED_Send_Command(0xC8);//SET COM/Row Scan Direction   0xc0上下反置 0xc8正常
	OLED_Send_Command(0xA6);//--SET normal display
	OLED_Send_Command(0xA8);//--SET multiplex ratio(1 to 64)
	OLED_Send_Command(0x3f);//--1/64 duty
	OLED_Send_Command(0xD3);//-SET display offSET	Shift Mapping RAM Counter (0x00~0x3F)
	OLED_Send_Command(0x00);//-not offSET
	OLED_Send_Command(0xd5);//--SET display clock divide ratio/oscillator frequency
	OLED_Send_Command(0x80);//--SET divide ratio, SET Clock as 100 Frames/Sec
	OLED_Send_Command(0xD9);//--SET pre-charge period
	OLED_Send_Command(0xF1);//SET Pre-Charge as 15 Clocks & Discharge as 1 Clock
	OLED_Send_Command(0xDA);//--SET com pins hardware configuration
	OLED_Send_Command(0x12);
	OLED_Send_Command(0xDB);//--SET vcomh
	OLED_Send_Command(0x40);//SET VCOM Deselect Level
	OLED_Send_Command(0x20);//-SET Page Addressing Mode (0x00/0x01/0x02)
	OLED_Send_Command(0x02);//
	OLED_Send_Command(0x8D);//--SET Charge Pump enable/disable
	OLED_Send_Command(0x14);//--SET(0x10) disable
	OLED_Send_Command(0xA4);// Disable Entire Display On (0xa4/0xa5)
	OLED_Send_Command(0xA6);// Disable Inverse Display On (0xa6/a7) 
	OLED_Send_Command(0xAF);//--turn on oled panel
	OLED_Send_Command(0xAF); /*display ON*/ 
	
	OLED_Clear(0x00); //清屏
}

2、写数据

void Oled_Write_Data(u8 data)
{
	IIC_Start();
	IIC_Send_Data(0x78);//地址
	IIC_Send_Data(0x40);//写数据
	IIC_Send_Data(data);//指令
	IIC_Stop();
}

3、写命令

void OLED_Send_Command(u8 com)
{
	IIC_Start();
	IIC_Send_Data(0x78);//地址
	IIC_Send_Data(0x00);//写指令 第6位控制写数据/命令
	IIC_Send_Data(com);//指令
	IIC_Stop();
}

4、清屏

void OLED_Clear(u8 clear_dat)
{ 
	u8 i = 0, j = 0;
	
	for(i = 0; i < 8; i++)
	{
		OLED_Send_Command(0xB0 + i);
		OLED_Send_Command(0X00);  //低列地址
		OLED_Send_Command(0X10);  //高列地址
		for(j = 0; j < 128; j++)
		{
			Oled_Write_Data(clear_dat);
		}
	}

}

5、显示位置

void Oled_Address(u8 row,u8 col)
{
	OLED_Send_Command(0xB0 + row);
	OLED_Send_Command(0X10 + ((col & 0xf0) >> 4));  //高列地址
	OLED_Send_Command(0X00 + (col & 0x0f)); 
}

6、显示字符

void Oled_Display_Char(u8 page,u8 col,char ch)
{
	u8 loca = ch - ' ';
	u8 i = 0;
	
	//页地址
	Oled_Address(page,col);
	for(i = 0; i < 8; i++)
	{
		Oled_Write_Data(Aciss_8X16[loca * 16 + i]);
	}
	Oled_Address(page + 1,col);
	for(i = 0; i < 8; i++)
	{
		Oled_Write_Data(Aciss_8X16[loca * 16 + 8 + i]);
	}
}

7、显示字符串

void Oled_Display_String(u8 page,u8 col,char *str)
{
	while(*str)
	{
		Oled_Display_Char(page,col,*str);
		col += 8;
		str++;
	}
}

8、显示汉字或图片

void Oled_Display_Pic(u8 wight,u8 high,u8 page,u8 col,u8 *str)
{
	u8 i = 0, j = 0;
	
	for(i = 0; i < high / 8; i++)
	{
		Oled_Address(page + i,col);
		for(j = 0; j < wight;j++)
		{
			Oled_Write_Data(str[wight * i + j]);
		}
	}
}

9、主函数

int main(void)
{
	char buf[] = {"There is no luck"};
	char buf1[] = {"There is only"};
	char buf2[] = {"word."};
	char author[] = {"Qing"};
	
	Sys_Delay_Init();
	Oled_Init();
//	Oled_Display_Char(0,0,'A');//显示单个字符
	
	//There is no luck.There is only work.幸运是不存在的,努力才是硬道理。
	Oled_Display_String(0,0,buf); //显示字符串
	Oled_Display_String(2,0,buf1); //显示字符串
	Oled_Display_String(4,0,buf2); //显示字符串
	Oled_Display_String(6,80,author);
	while(1)
	{
		
	}
}

五、实验效果

在这里插入图片描述

程序及相关资料
链接:
https://pan.baidu.com/s/1Bo093SotTLxs1NgNwa01nQ
提取码:ygrv

如有错误还望指出,谢谢!