本帖最后由 werasd 于 2022-5-25 15:10 编辑
I2C接口的OLED屏是最节省I/O口的显示屏,它只需2个GPIO口即可驱动其显示,这里选用的是0.96寸的双色屏,其显示分辨率为128*64像素点。实则是一款准双色屏。因为尽管它有黄蓝2种色彩,但色彩是分区域使用的,而不能自主的设置色彩。 显示效果
以模拟方式输出高低电平的语句定义为: #define OLED_SCLK_Set() GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_2); #define OLED_SCLK_Clr() GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_2); #define OLED_SDIN_Set() GPIO_SetOutBits(HT_GPIOA, GPIO_PIN_3); #define OLED_SDIN_Clr() GPIO_ClearOutBits(HT_GPIOA, GPIO_PIN_3); 相应的引脚功能配置函数为: void OLED _GPIO_Configuration(void)
{
{
CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};
CKCUClock.Bit.AFIO = 1;
CKCUClock.Bit.PA = 1;
CKCU_PeripClockConfig(CKCUClock, ENABLE);
}
{
AFIO_GPxConfig(GPIO_PA, AFIO_PIN_2|AFIO_PIN_3, AFIO_FUN_GPIO);
GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_2|AFIO_PIN_3, GPIO_PR_DOWN);
GPIO_WriteOutBits(HT_GPIOA, GPIO_PIN_2, SET);
GPIO_WriteOutBits(HT_GPIOA, GPIO_PIN_3, SET);
GPIO_DirectionConfig(HT_GPIOA, GPIO_PIN_2|AFIO_PIN_3, GPIO_DIR_OUT);
}
}
模拟I2C方式字节数据的函数为: void Write_IIC_Byte(unsigned char IIC_Byte)
{
unsigned char i;
unsigned char m,da;
da=IIC_Byte;
OLED_SCLK_Clr();
for(i=0;i<8;i++)
{
m=da;
m=m&0x80;
if(m==0x80)
{
OLED_SDIN_Set();
}
else
OLED_SDIN_Clr();
__Delay(2);
da=da<<1;
OLED_SCLK_Set();
__Delay(2);
OLED_SCLK_Clr();
__Delay(2);
}
}
OLED屏的初始化函数为: void OLED_Init(void)
{
OLED_SCLK_Set();
OLED_SDIN_Set();
__Delay(800000);
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);
相应的字符串显示函数为: void OLED_ShowString(uint8_t x,uint8_t y,uint8_t *chr,uint8_t 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++;
}
}
实现图示效果的主程序为: int main(void)
{
NVIC_Configuration();
CKCU_Configuration();
GPIO_Configuration();
RETARGET_Configuration();
OLED _GPIO_Configuration();
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"OLED & TEST",16);
OLED_ShowString(20,2,"HT32F52352",16);
while (1)
{
;
}
}
|