本帖最后由 jinglixixi 于 2023-8-6 18:31 编辑
在HC32L196片内,配置有RTC计时器,通过例程可了解其用法。 通过函数RTC_Cfg()可进行计时值的校正,其内容为: 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 = 0x01;
RtcInitStruct.rtcTime.u8Hour = 0x10;
RtcInitStruct.rtcTime.u8Day = 0x17;
RtcInitStruct.rtcTime.u8DayOfWeek = 0x04;
RtcInitStruct.rtcTime.u8Month = 0x04;
RtcInitStruct.rtcTime.u8Year = 0x19;
RtcInitStruct.rtcCompen = RtcCompenEnable;
RtcInitStruct.rtcCompValue = 0;//补偿值根据实际情况进行补偿
Rtc_Init(&RtcInitStruct);
Rtc_AlmIeCmd(TRUE); //使能闹钟中断
}
此外,通过中断函数Rtc_IRQHandler()可实现秒计时的标志处理,其内容为: void Rtc_IRQHandler(void)
{
if(Rtc_GetPridItStatus() == TRUE)
{
flag = 1;
Rtc_ClearPrdfItStatus(); //清除中断标志位
}
}
依托上面2个函数及LCD屏显示函数,可快速地实现电子时钟的功能,其主程序为: int32_t main(void)
{
int8_t i=0;
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开始计数
Sysctrl_ClkSourceEnable(SysctrlClkRCL,TRUE); ///< 使能RCL时钟
Sysctrl_SetRCLTrim(SysctrlRclFreq32768); ///< 配置内部低速时钟频率为32.768kHz
Sysctrl_SetPeripheralGate(SysctrlPeripheralLcd,TRUE); ///< 开启LCD时钟
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); ///< 开启GPIO时钟
App_PortCfg(); ///< LCD端口配置
App_LcdCfg(); ///< LCD模块配置
Lcd_ClearDisp(); ///< 清屏
Lcd_WriteRam(0,0x00000000); ///< 赋值寄存器LCDRAM0
Lcd_WriteRam(1,0x00000000); ///< 赋值寄存器LCDRAM1
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;
s=((d[(minute&0x0f)]+0x08)<<16)+d[minute/16];
Lcd_WriteRam(0,s);
s=(d[(second&0x0f)]<<16)+d[second/16];
Lcd_WriteRam(1,s);
}
}
}
经程序的编译与下载,其显示效果如下图所示。 RTC计时效果图
|