【新提醒】HC32L136开发板的OLED驱动及应用
在HC32L136开发板上配有一个笔段式的液晶屏,但无奈的是厂家却没提供一个能直接调用的显示函数。
在没解决该显示屏的显示函数前,先以轻车熟路的OLED屏来实现显示的问题。
由于手头的OLED屏是一款I2C接口的单色显示屏,故另其与MCU的连接关系为:
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);
分配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);
- }
复制代码
实现图1显示效果的主程序为:
- int32_t main(void)
- {
- stc_rtc_time_t readtime;
- Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //GPIO外设时钟打开
- Sysctrl_SetPeripheralGate(SysctrlPeripheralRtc,TRUE); //RTC模块时钟打开
- Sysctrl_ClkSourceEnable(SysctrlClkRCL, TRUE);
- RTC_Cfg(); //配置RTC
- EnableNvic(RTC_IRQn, IrqLevel3, TRUE); //使能RTC中断向量
- Rtc_Cmd(TRUE); //使能RTC开始计数
- App_OledInit();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"HC32L136 TEST",16);
- OLED_ShowString(0,2,"OLED DISPLAY",16);
- while (1);
- }
复制代码
图1 OLED屏显示效果
实现RTC时间设置的函数为:
- void RTC_Cfg(void)
- {
- stc_rtc_initstruct_t RtcInitStruct;
- RtcInitStruct.rtcAmpm = RtcPm; //12小时制
- RtcInitStruct.rtcClksrc = RtcClkRcl; //内部低速时钟
- RtcInitStruct.rtcPrdsel.rtcPrdsel = RtcPrds; //周期中断类型PRDS
- RtcInitStruct.rtcPrdsel.rtcPrds = Rtc1S; //周期中断事件间隔
- RtcInitStruct.rtcTime.u8Second = 0x55;
- RtcInitStruct.rtcTime.u8Minute = 0x55;
- RtcInitStruct.rtcTime.u8Hour = 0x10;
- RtcInitStruct.rtcTime.u8Day = 0x06;
- RtcInitStruct.rtcTime.u8DayOfWeek = 0x04;
- RtcInitStruct.rtcTime.u8Month = 0x10;
- RtcInitStruct.rtcTime.u8Year = 0x20;
- RtcInitStruct.rtcCompen = RtcCompenEnable;
- RtcInitStruct.rtcCompValue = 0;//补偿值根据实际情况进行补偿
- Rtc_Init(&RtcInitStruct);
- Rtc_AlmIeCmd(TRUE); //使能闹钟中断
- }
复制代码
实现图3 RTC计时效果的主程序为:
- int32_t main(void)
- {
- stc_rtc_time_t readtime;
- Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //GPIO外设时钟打开
- Sysctrl_SetPeripheralGate(SysctrlPeripheralRtc,TRUE); //RTC模块时钟打开
- Sysctrl_ClkSourceEnable(SysctrlClkRCL, TRUE);
- RTC_Cfg(); //配置RTC
- EnableNvic(RTC_IRQn, IrqLevel3, TRUE); //使能RTC中断向量
- Rtc_Cmd(TRUE); //使能RTC开始计数
- App_OledInit();
- OLED_Init();
- OLED_Clear();
- OLED_ShowString(0,0,"HC32L136 TEST",16);
- OLED_ShowString(0,2,"OLED & RTC",16);
- delay1ms(2000);
- OLED_Clear();
- OLED_ShowString(0,0,"20 - -",16);
- OLED_ShowString(0,2," : :",16);
- while (1)
- {
- if(flag == 1)
- {
- flag = 0;
- Rtc_ReadDateTime(&readtime);
- second = readtime.u8Second;
- minute = readtime.u8Minute;
- hour = readtime.u8Hour;
- day = readtime.u8Day;
- week = readtime.u8DayOfWeek;
- month = readtime.u8Month;
- year = readtime.u8Year;
- OLED_ShowNum(16,0,year/16*10+year%16,2,16);
- OLED_ShowNum(40,0,month/16*10+month%16,2,16);
- OLED_ShowNum(64,0,day/16*10+day%16,2,16);
- OLED_ShowNum(16,2,hour/16*10+hour%16,2,16);
- OLED_ShowNum(40,2,minute/16*10+minute%16,2,16);
- OLED_ShowNum(64,2,second/16*10+second%16,2,16);
- }
- }
- }
复制代码
图2 RTC起始界面
图3 RTC计时效果
赞 (0)