中景园OLED基于I2C/SPI和汉字显示的程序分析

学习了一段时间的STM32,回顾过去也没写什么笔记,这次用到了OLED显示索性就写篇博客总结下OLED基于I2C/SPI通讯和汉字显示方法的程序分析
基于I2C的初始化函数分析
中景园采用的是使用普通IO口来模拟I2C外设,这样做的好处就是节省I2C的外设数量,分配给更需要的I2C外设,同时不受特殊引脚的限制;缺点是操作比较复杂,要根据时序图来进行相关配置
//OLED的初始化
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能A端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOD4,5,7
GPIO_SetBits(GPIOA,GPIO_Pin_7|GPIO_Pin_4);

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

}

基于SPI的初始化函数分析
//OLED的初始化
void OLED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能A端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOD3,6
GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_7|GPIO_Pin_4);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);	 //使能A端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 		 //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);	  //初始化GPIOD3,6
GPIO_SetBits(GPIOB,GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_8);

OLED_RST_Set();
delay_ms(100);
OLED_RST_Clr();//复位
delay_ms(200);
OLED_RST_Set();

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

}

汉字的显示程序分析

//显示汉字
//x,y:起点坐标
//num:汉字对应的序号
//取模方式 列行式
void OLED_ShowChinese(u8 x,u8 y,u8 num,u8 size1)
{
u8 i,m,n=0,temp,chr1;
u8 x0=x,y0=y;
u8 size3=size1/8;
while(size3–)
{
chr1=numsize1/8+n;
n++;
for(i=0;i<size1;i++)
{
if(size1==16)
{temp=Hzk1[chr1][i];}//调用16
16字体
else if(size124)
{temp=Hzk2[chr1][i];}//调用24*24字体
else if(size1
32)
{temp=Hzk3[chr1][i];}//调用3232字体
else if(size1==64)
{temp=Hzk4[chr1][i];}//调用64
64字体
else return;

					for(m=0;m<8;m++)
						{
							if(temp&0x01)OLED_DrawPoint(x,y);
							else OLED_ClearPoint(x,y);
							temp>>=1;
							y++;
						}
						x++;
						if((x-x0)==size1)
						{x=x0;y0=y0+8;}
						y=y0;
		 }
}

}

汉字滚动程序分析
//num 显示汉字的个数
//space 每一遍显示的间隔
void OLED_ScrollDisplay(u8 num,u8 space)
{
u8 i,n,t=0,m=0,r;
while(1)
{
if(m0)
{
OLED_ShowChinese(128,24,t,16); //写入一个汉字保存在OLED_GRAM[][]数组中
t++;
}
if(t
num)
{
for(r=0;r<16*space;r++) //显示间隔
{
for(i=0;i<144;i++)
{
for(n=0;n<8;n++)
{
OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
}
}
OLED_Refresh();
}
t=0;
}
m++;
if(m==16){m=0;}
for(i=0;i<144;i++) //实现左移
{
for(n=0;n<8;n++)
{
OLED_GRAM[i-1][n]=OLED_GRAM[i][n];
}
}
OLED_Refresh();
}
}
了解更多相关细节还得通过整个程序来分析。
日期:2021年8月1日

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
景园电子2.42寸OLED技术资料+单片机软件例程源码: 01-景园电子2.42寸OLED_arduino_I2C例程.zip 01-景园电子2.42寸OLED_arduino_SPI例程.zip 02景园2.42寸OLED-SPI STM32F407程序-直接配合景园开发板.zip 03景园电子2.42寸OLED显示屏_C51系列_IIC_例程推荐使用.zip 04景园电子2.42寸OLED显示屏_C51系列_SPI_例程.rar 05景园电子2.42寸OLED显示屏STC89C52RC系列_SPI_例程.zip 06景园电子2.42寸寸OLED SPI接口STM8L051F3系统参考程序.zip 07景园电子2.42寸寸OLED IIC接口STM8L051F3系统参考程序.zip 08景园电子2.42寸OLED显示屏_STM8S103K3T6_SPI_例程.zip 09景园电子2.42寸OLED显示屏_C8051F020系列_IIC_例程.zip 10景园电子2.42寸OLED显示屏_C8051F020系列_SPI_例程.zip 11景园电子2.42寸OLED显示屏_C8051F340系列_IIC_例程.zip 12景园电子2.42寸OLED显示屏_C8051F340系列_SPI_例程.zip 13景园电子2.42寸OLED显示屏_MSP430系列_IIC_例程.zip 14景园电子2.42寸OLED显示屏_MSP430系列_SPI_例程.zip 15景园电子STM32F030-2.42寸LED-IIC通信程序.zip 16景园电子STM32F030-2.42寸寸OLED-SPI通信程序.zip 17景园电子2.42寸OLED显示屏_STM8系列_IIC_例程.zip 18景园电子2.42寸OLED显示屏_STM8系列_SPI_例程.rar 19景园电子2.42寸OLED显示屏_STM32F103C8_IIC_V1.0.zip 20景园电子2.42寸OLED显示屏_STM32F103RC_IIC_V1.0.zip 23景园电子2.42寸OLED显示屏_STM32F103ZET6_IIC_V1.0.zip 24景园电子2.42寸OLED显示屏_STM32ZET系列_SPI_例程.zip 25景园电子2.42寸显示屏_STM32_F103C8系列_SPI_例程.rar 26景园电子2.42寸显示显示点、线、圆、不同尺寸字符以及汉子滚动C51_I2C例程20181129.rar 27景园电子2.42寸显示显示点、线、圆、不同尺寸字符以及汉子滚动STM32F103C8T6_IIC例程20181129.rar 28景园电子2.42寸OLED显示屏C51_显示显示点、线、圆、不同尺寸字符以及汉子滚动.rar 29景园电子2.42寸OLED显示屏STM32F103C8T6_SPI例程显示显示点、线、圆、不同尺寸字符以及汉子滚动.rar 30景园电子2.42寸OLED显示屏STM32F103C8T6_硬件IIC例程显示显示点、线、圆、不同尺寸字符以及汉子滚动.rar 31景园电子2.42寸OLED显示屏STM32F103C8T6_硬件SPI例程显示显示点、线、圆、不同尺寸字符以及汉子滚动.rar

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值