【CW32L052测评】+RTC电子时钟计时与显示
[复制链接]
本帖最后由 jinglixixi 于 2023-8-3 21:14 编辑
在CW32L052内部配置了RTC计时器,这可是一个标准的RTC,而非目前有些厂家推出的芯片,说是有RTC,却是个冒牌货,只能称得上是一个准RTC,必须通过软件来实现日历功能。
因此用其片内的RTC可以轻松地实现电子时钟的功能,以串口进行测试的效果如图1和图2所示。
图1 计时测试
图2 进制测试
读取RTC计时值的函数为:
void RTC_GetTimeV(RTC_TimeTypeDef* RTC_TimeStruct)
{
uint32_t RegTmp = 0;
RTC_TimeStruct->H24 = CW_RTC->CR0_f.H24;
RegTmp = CW_RTC->TIME;
while (RegTmp != CW_RTC->TIME)
{
RegTmp = CW_RTC->TIME;
}
RTC_TimeStruct->Hour = (uint8_t)((RegTmp & RTC_TIME_HOUR_Msk) >> 16);
RTC_TimeStruct->Hour=(RTC_TimeStruct->Hour/16)*10+(RTC_TimeStruct->Hour%16);
RTC_TimeStruct->Minute = (uint8_t)((RegTmp & RTC_TIME_MINUTE_Msk) >> 8);
RTC_TimeStruct->Minute=(RTC_TimeStruct->Minute/16)*10+(RTC_TimeStruct->Minute%16);
RTC_TimeStruct->Second = (uint8_t)(RegTmp & RTC_TIME_SECOND_Msk);
RTC_TimeStruct->Second=(RTC_TimeStruct->Second/16)*10+(RTC_TimeStruct->Second%16);
if (RTC_TimeStruct->H24 == RTC_HOUR12)
{
RTC_TimeStruct->AMPM = RTC_TimeStruct->Hour >> 5;
RTC_TimeStruct->Hour &= 0x1f;
}
}
通过与前面介绍的LCD屏显示功能相配合,就可完成电子时钟的效果,其主程序为:
int32_t main(void)
{
RTC_InitTypeDef* RTC_Config;
RTC_TimeTypeDef RTC_DateTime;
RTC_TimeTypeDef TempTime;
RTC_DateTypeDef TempDate;
uint8_t hh,mm,ss;
RCC_Configuration();
RCC_SystemCoreClockUpdate(24000000);
InitTick(24000000);
RCC_HSI_OUT();
RCC_LSI_Enable();
LCD_Configuration();
LCD_Write(LCD_RAMRegister_0, 0x0);
LCD_Write(LCD_RAMRegister_1, 0x0);
LCD_Write(LCD_RAMRegister_2, 0x0);
LCD_Write(LCD_RAMRegister_3, 0x0);
LCD_Write(LCD_RAMRegister_4, 0x0);
__RCC_RTC_CLK_ENABLE();
RTC_Init(RTC_Config);
TempDate.Year = 23;
TempDate.Month = 0x07;
TempDate.Day = 16;
TempDate.Week = 0x04;
TempTime.AMPM = 0;
TempTime.Hour = 0X23;
TempTime.Minute = 0X59;
TempTime.Second = 0X30;
TempTime.H24 =1;
RTC_SetDate(&TempDate);
RTC_SetTime(&TempTime);
while(1)
{
RTC_GetDate(&TempDate);
RTC_GetTimeV(&TempTime);
LCD_Write(LCD_RAMRegister_0,(b[TempTime.Hour%10]<<16)+b[TempTime.Hour/10]);
LCD_Write(LCD_RAMRegister_1, (b[TempTime.Minute/10]<<16)+0x2);
LCD_Write(LCD_RAMRegister_2, (0x2<<16)+b[TempTime.Minute%10]);
LCD_Write(LCD_RAMRegister_3, 0x00+(b[TempTime.Second/10]<<16));//
LCD_Write(LCD_RAMRegister_4, (0x2<<16)+b[TempTime.Second%10]);
SysTickDelay(1000);
}
}
经程序的编译和下载,其测试结果如图3所示,说明功能正确。
图3 电子时钟效果
|