热度 3
2022-10-26 20:37
1755 次阅读|
2 个评论
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-' '; 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 ,OLED_DATA); OLED_Set_Pos(x,y+1); for(i=0;i<8;i++) OLED_WR_Byte(F8X16 ,OLED_DATA); } else { OLED_Set_Pos(x,y); for(i=0;i<6;i++) OLED_WR_Byte(F6x8 ,OLED_DATA); } } void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size) { unsigned char j=0; while (chr !='\0') { OLED_ShowChar(x,y,chr ,Char_Size); x+=8; 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(); } 经编译与下载,其显示效果如图 所示。 显示效果