本帖最后由 jinglixixi 于 2023-8-7 10:22 编辑
在HC32L196开发板上配有一个笔段式的液晶屏,但它的信息表现力还是较为有限,且占用引脚资源较多。为此,可以I2C接口的OLED屏来承担显示任务,其与开发板的连接关系为: PA11:SCL PA12:SDA 使这2个引脚输出高低电平的语句定义为: #define OLED_SCLK_Set() Gpio_SetIO(GpioPortA, GpioPin11) #define OLED_SCLK_Clr() Gpio_ClrIO(GpioPortA, GpioPin11) #define OLED_SDIN_Set() Gpio_SetIO(GpioPortA, GpioPin12) #define OLED_SDIN_Clr() Gpio_ClrIO(GpioPortA, GpioPin12)
图1 使用接口
配置这2个引脚为输出功能的函数为: void App_OledInit(void)
{
stc_gpio_cfg_t stcGpioCfg;
///< 打开GPIO外设时钟门控
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE);
///< 端口方向配置->输出(其它参数与以上(输入)配置参数一致)
stcGpioCfg.enDir = GpioDirOut;
///< 端口上下拉配置->下拉
stcGpioCfg.enPu = GpioPuDisable;
stcGpioCfg.enPd = GpioPdEnable;
///< GPIO IO OLED端口初始化
Gpio_Init(GpioPortA, GpioPin11, &stcGpioCfg);
Gpio_Init(GpioPortA, GpioPin12, &stcGpioCfg);
}
该显示屏的初始化函数为: void OLED_Init(void)
{
Write_IIC_Command(0xAE);
Write_IIC_Command(0x20);
Write_IIC_Command(0x10);
Write_IIC_Command(0xb0);
Write_IIC_Command(0xc8);
Write_IIC_Command(0x00);
Write_IIC_Command(0x10);
Write_IIC_Command(0x40);
Write_IIC_Command(0x81);
Write_IIC_Command(0xdf);
Write_IIC_Command(0xa1);
Write_IIC_Command(0xa6);
Write_IIC_Command(0xa8);
Write_IIC_Command(0x3F);
Write_IIC_Command(0xa4);
Write_IIC_Command(0xd3);
Write_IIC_Command(0x00);
Write_IIC_Command(0xd5);
Write_IIC_Command(0xf0);
Write_IIC_Command(0xd9);
Write_IIC_Command(0x22);
Write_IIC_Command(0xda);
Write_IIC_Command(0x12);
Write_IIC_Command(0xdb);
Write_IIC_Command(0x20);
Write_IIC_Command(0x8d);
Write_IIC_Command(0x14);
Write_IIC_Command(0xaf);
}
实现清屏处理的函数为:‘ void OLED_Clear(void)
{
uint8_t 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(uint8_t x,uint8_t y,uint8_t chr,uint8_t 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(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
unsigned char j=0;
while (chr[j]!='\0')
{
显示显示功能测试的主程序为: int32_t main(void)
{
///< LED端口初始化
App_LedInit();
App_OledInit();
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"HC32L196 TEST",16);
OLED_ShowString(0,2,"OLED DISPLAY",16);
while(1)
{
///< LED点亮
Gpio_SetIO(STK_LED_PORT, STK_LED_PIN);
delay1ms(1000);
///< LED关闭
Gpio_ClrIO(STK_LED_PORT, STK_LED_PIN);
delay1ms(1000);
}
}
经程序的编译与下载,其显示效果如图2所示。 图2 显示效果
|