OLED屏有多种形式,如单色屏、双色屏及彩色屏;如0.91寸、0.96寸等;如I2C接口、SPI接口屏等。
这里使用的是一款0.96寸的双色I2C接口屏,它与开发板的连接关系为:
SDA---PA0
SCL---PA1
定义引脚输出高低电平的语句定义为:
#define SCL_high
GPIO_SET_OUTPUT_PIN_TO_1(GPIOA_BASE_ADDR,GPIO_PIN0)
#define SCL_low
GPIO_SET_OUTPUT_PIN_TO_0(GPIOA_BASE_ADDR,GPIO_PIN0)
#define SDA_high
GPIO_SET_OUTPUT_PIN_TO_1(GPIOA_BASE_ADDR,GPIO_PIN1)
#define SDA_low
GPIO_SET_OUTPUT_PIN_TO_0(GPIOA_BASE_ADDR,GPIO_PIN1)
对OLED屏进行初始化的函数为:void OLED_Init(void)
{ OLED_WR_Byte(0xAE,OLED_CMD); OLED_WR_Byte(0x02,OLED_CMD); OLED_WR_Byte(0x10,OLED_CMD); OLED_WR_Byte(0x40,OLED_CMD); OLED_WR_Byte(0x81,OLED_CMD); OLED_WR_Byte(0xff,OLED_CMD); OLED_WR_Byte(0xA1,OLED_CMD); OLED_WR_Byte(0xC8,OLED_CMD); OLED_WR_Byte(0xA6,OLED_CMD); OLED_WR_Byte(0xA8,OLED_CMD); OLED_WR_Byte(0x3f,OLED_CMD); OLED_WR_Byte(0xD3,OLED_CMD); OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0xd5,OLED_CMD); OLED_WR_Byte(0x80,OLED_CMD); OLED_WR_Byte(0xD9,OLED_CMD); OLED_WR_Byte(0xF1,OLED_CMD); OLED_WR_Byte(0xDA,OLED_CMD); OLED_WR_Byte(0x12,OLED_CMD); OLED_WR_Byte(0xDB,OLED_CMD); OLED_WR_Byte(0x40,OLED_CMD); OLED_WR_Byte(0x20,OLED_CMD); OLED_WR_Byte(0x02,OLED_CMD); OLED_WR_Byte(0x8D,OLED_CMD); OLED_WR_Byte(0x14,OLED_CMD); OLED_WR_Byte(0xA4,OLED_CMD); OLED_WR_Byte(0xA6,OLED_CMD); OLED_WR_Byte(0xAF,OLED_CMD); OLED_WR_Byte(0xAF,OLED_CMD); OLED_Clear(); OLED_Set_Pos(0,0);}复制代码在驱动OLED屏时,其辅助I2C启动与停止的函数如下:
void IIC_Start()
{ SCL_high; SDA_high; SDA_low; SCL_low;}void IIC_Stop(){ SCL_low; SDA_low; SCL_high; SDA_high;}复制代码以GPIO口模拟I2C发送字节数据的函数为:
void Write_IIC_Byte(unsigned char IIC_Byte)
{ unsigned char i; unsigned char m,da; da=IIC_Byte; SCL_low; for(i=0;i<8;i++) { m=da; m=m&0x80; if(m==0x80) { SDA_high; } else { SDA_low; } da=da<<1; SCL_high; SCL_low; }}复制代码实现OLED屏清屏的函数为:
void OLED_Clear(void)
{ u8 i,n; for(i=0;i<8;i++) { OLED_WR_Byte(0xb0+i,OLED_CMD); OLED_WR_Byte(0x00,OLED_CMD); OLED_WR_Byte(0x10,OLED_CMD); for(n=0;n<128;n++) OLED_WR_Byte(0,OLED_DATA); }}复制代码实现字符及字符串显示的函数如下:
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 Char_Size)
{ unsigned char c=0,i=0; c=chr-' '; if(x>Max_Column-1){x=0;y=y+2;} if(Char_Size ==16) { OLED_Set_Pos(x,y); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i],OLED_DATA); OLED_Set_Pos(x,y+1); for(i=0;i<8;i++) OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA); } else { OLED_Set_Pos(x,y); for(i=0;i<6;i++) OLED_WR_Byte(F6x8[c][i],OLED_DATA); }}void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size){ unsigned char j=0; while (chr[j]!='\0') { OLED_ShowChar(x,y,chr[j],Char_Size); x+=8; if(x>120){x=0;y+=2;} j++; }}复制代码驱动OLED屏显示的程序被放置在行程0中,其显示程序为:
void thread0_main(void)
{ //使能了GPIOA的时钟INTDEV_SET_CLK_RST(GPIOA_BASE_ADDR,(INTDEV_RUN|INTDEV_IS_GROUP0|INTDEV_CLK_IS_CORECLK_DIV2)); //将PA0设置为输出模式 GPIO_SET_OUTPUT_EN_VALUE(GPIOA_BASE_ADDR,(GPIO_PIN0),GPIO_SET_ENABLE); GPIO_SET_OUTPUT_EN_VALUE(GPIOA_BASE_ADDR,(GPIO_PIN1),GPIO_SET_ENABLE); OLED_Init(); OLED_Clear(); OLED_ShowString(0,0,"MC3172",16); OLED_ShowString(0,2,"OLED TEST",16); while(1){ } thread_end();}复制代码经编译与下载,其显示效果如图所示。

作者: jinglixixi_457498010, 来源:面包板社区
链接: https://mbb.eet-china.com/blog/uid-me-467667.html
版权声明:本文为博主原创,未经本人允许,禁止转载!
jinglixixi_457498010 2022-10-31 10:42
whik 2022-10-28 20:43