【CH32V208】1、体验RTC - RISC-V MCU技术社区 - 电子技术william hill官网 - 广受欢迎的专业电子william hill官网 - 威廉希尔官方网站
分享 收藏 返回

[文章]

【CH32V208】1、体验RTC

【实验器材】

CH32V208WBU6 评估板

image.png
【开发环境】
MounRiver Studio
Version: v1.91

OS: Windows 10, v.10.0, x86 / win32
Java version: 1.8.0_221
【实验程序】
1、RTC实始化,函数如下:

u8 RTC_Init(void)
{
    u8 temp = 0;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
    PWR_BackupAccessCmd(ENABLE);

    /* Is it the first configuration */

    BKP_DeInit();
    RCC_LSEConfig(RCC_LSE_ON);
    while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET && temp < 250)
    {
            temp++;
            Delay_Ms(20);
    }
    if(temp >= 250)
    return 1;
    RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
    RCC_RTCCLKCmd(ENABLE);
    RTC_WaitForLastTask();
    RTC_WaitForSynchro();
    //		RTC_ITConfig(RTC_IT_ALR, ENABLE);
    RTC_ITConfig(RTC_IT_SEC, ENABLE);
    RTC_WaitForLastTask();
    RTC_EnterConfigMode();
    RTC_SetPrescaler(32767);
    RTC_WaitForLastTask();
    RTC_Set(2024, 7, 30, 13, 58, 55); /* Setup Time */
    RTC_ExitConfigMode();
    BKP_WriteBackupRegister(BKP_DR1, 0XA1A1);

    RTC_NVIC_Config();
    RTC_Get();

    return 0;
}

函数初期打开时钟与RTC的备份域电源,接着启用LSE时钟,并等待时钟就绪,如果超时,则返回错误码,接着设置初始化日期时间。
接着写入备份寄存器0xA1A1保存参数。
开启RTC中断。

在中断函数中,判断是进入哪个中断,并读取时间,其代码如下:

/*********************************************************************
 * @fn      RTC_IRQHandler
 *
 * [url=home.php?mod=space&uid=2666770]@Brief[/url]   This function handles RTC Handler.
 *
 * [url=home.php?mod=space&uid=1141835]@Return[/url]  none
 */
void RTC_IRQHandler(void)
{
	if (RTC_GetITStatus(RTC_IT_SEC) != RESET)   /* Seconds interrupt */
	{
    RTC_Get();
 	}
	if(RTC_GetITStatus(RTC_IT_ALR)!= RESET)     /* Alarm clock interrupt */
	{
		RTC_ClearITPendingBit(RTC_IT_ALR);
    RTC_Get();
  }

	RTC_ClearITPendingBit(RTC_IT_SEC|RTC_IT_OW);
	RTC_WaitForLastTask();
}

在主程序中,初始化RTC后,周期打印时间:

int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
    SystemCoreClockUpdate();
    Delay_Init();
    USART_Printf_Init(115200);
    printf("SystemClk:%d\r\n", SystemCoreClock);
    printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
    printf("RTC Test\r\n");
    RTC_Init();

    while(1)
    {
        Delay_Ms(1000);
        printf("year/month/day/week/hour/min/sec:\r\n");
        printf("%d-%d-%d  %d  %d:%d:%d\r\n", calendar.w_year, calendar.w_month, calendar.w_date,
               calendar.week, calendar.hour, calendar.min, calendar.sec);
    }
}

实验效果:
image.png

更多回帖

×
发帖