打印
[DemoCode下载]

M031的RTC报警中断使用方法

[复制链接]
313|7
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
  • /****************************************************************************
    * [url=home.php?mod=space&uid=288409]@file[/url]     main.c
    * [url=home.php?mod=space&uid=895143]@version[/url]  V1.00
    * [url=home.php?mod=space&uid=247401]@brief[/url]    Demonstrate the RTC alarm function. It sets an alarm 10 seconds
    *           after execution
    *
    * SPDX-License-Identifier: Apache-2.0
    * [url=home.php?mod=space&uid=17282]@CopyRight[/url] (C) 2016 Nuvoton Technology Corp. All rights reserved.
    ******************************************************************************/
    #include <stdio.h>
    #include "NuMicro.h"

    /*---------------------------------------------------------------------------------------------------------*/
    /* Global variables                                                                                        */
    /*---------------------------------------------------------------------------------------------------------*/

    volatile int32_t   g_bAlarm  = FALSE;


    /*---------------------------------------------------------------------------------------------------------*/
    /* RTC Alarm Handle                                                                             */
    /*---------------------------------------------------------------------------------------------------------*/
    void RTC_AlarmHandle(void)
    {
        printf(" Alarm!!\n");
        g_bAlarm = TRUE;
    }

    /**
      * @brief  RTC ISR to handle interrupt event
      * @param  None
      * @retval None
      */
    void RTC_IRQHandler(void)
    {
        if ( (RTC->INTEN & RTC_INTEN_ALMIEN_Msk) && (RTC->INTSTS & RTC_INTSTS_ALMIF_Msk) )        /* alarm interrupt occurred */
        {
            RTC->INTSTS = 0x1;

            RTC_AlarmHandle();
        }
    }

    void SYS_Init(void)
    {
        /* Unlock protected registers */
        SYS_UnlockReg();

        /* Enable LXT clock */
        CLK_EnableXtalRC(CLK_PWRCTL_LXTEN_Msk);

        /* Enable HIRC clock */
        CLK_EnableXtalRC(CLK_PWRCTL_HIRCEN_Msk);

        /* Waiting for HIRC clock ready */
        CLK_WaitClockReady(CLK_STATUS_HIRCSTB_Msk);

        /* Switch HCLK clock source to HIRC and HCLK source divide 1 */
        CLK_SetHCLK(CLK_CLKSEL0_HCLKSEL_HIRC, CLK_CLKDIV0_HCLK(1));

        /* Enable UART0 clock */
        CLK_EnableModuleClock(UART0_MODULE);

        /* Switch UART0 clock source to HIRC */
        CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART0SEL_HIRC, CLK_CLKDIV0_UART0(1));

        /* Enable RTC clock */
        CLK_EnableModuleClock(RTC_MODULE);

        /* Update System Core Clock */
        SystemCoreClockUpdate();

        /* Set PB multi-function pins for UART0 RXD=PB.12 and TXD=PB.13 */
        SYS->GPB_MFPH = (SYS->GPB_MFPH & ~(SYS_GPB_MFPH_PB12MFP_Msk | SYS_GPB_MFPH_PB13MFP_Msk))
                        |(SYS_GPB_MFPH_PB12MFP_UART0_RXD | SYS_GPB_MFPH_PB13MFP_UART0_TXD);

        /* Lock protected registers */
        SYS_LockReg();
    }

    void UART0_Init()
    {
        /*---------------------------------------------------------------------------------------------------------*/
        /* Init UART                                                                                               */
        /*---------------------------------------------------------------------------------------------------------*/
        /* Reset UART module */
        SYS_ResetModule(UART0_RST);

        /* Configure UART0 and set UART0 Baudrate */
        UART_Open(UART0, 115200);
    }


    int32_t main(void)
    {
        S_RTC_TIME_DATA_T sInitTime;
        S_RTC_TIME_DATA_T sCurTime;

        SYS_Init();

        UART0_Init();

        /* Time Setting */
        sInitTime.u32Year       = 2017;
        sInitTime.u32Month      = 5;
        sInitTime.u32Day        = 1;
        sInitTime.u32Hour       = 12;
        sInitTime.u32Minute     = 30;
        sInitTime.u32Second     = 0;
        sInitTime.u32DayOfWeek  = RTC_MONDAY;
        sInitTime.u32TimeScale  = RTC_CLOCK_24;

        RTC_Open(&sInitTime);

        printf("\n RTC Alarm Test (Alarm after 10 seconds)\n\n");

        g_bAlarm = FALSE;

        /* Get the current time */
        RTC_GetDateAndTime(&sCurTime);

        printf(" Current Time:%d/%02d/%02d %02d:%02d:%02d\n",sCurTime.u32Year,sCurTime.u32Month,
               sCurTime.u32Day,sCurTime.u32Hour,sCurTime.u32Minute,sCurTime.u32Second);

        /* The alarm time setting */
        sCurTime.u32Second = sCurTime.u32Second + 10;

        /* Set the alarm time */
        RTC_SetAlarmDateAndTime(&sCurTime);

        /* Clear interrupt status */
        RTC->INTSTS = RTC_INTSTS_ALMIF_Msk;

        /* Enable RTC Alarm Interrupt */
        RTC_EnableInt(RTC_INTEN_ALMIEN_Msk);

        NVIC_EnableIRQ(RTC_IRQn);

        while(!g_bAlarm);

        /* Get the current time */
        RTC_GetDateAndTime(&sCurTime);

        printf(" Current Time:%d/%02d/%02d %02d:%02d:%02d\n",sCurTime.u32Year,sCurTime.u32Month,
               sCurTime.u32Day,sCurTime.u32Hour,sCurTime.u32Minute,sCurTime.u32Second);

        /* Disable RTC Alarm Interrupt */
        RTC_DisableInt(RTC_INTEN_ALMIEN_Msk);
        NVIC_DisableIRQ(RTC_IRQn);

        printf("\n RTC Alarm Test End !!\n");

        while(1);

    }



    /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/





使用特权

评论回复
沙发
dongnanxibei| | 2023-2-26 20:00 | 只看该作者
可以直接触发一个中断吗,可以可以。

使用特权

评论回复
板凳
dongnanxibei| | 2023-2-26 20:13 | 只看该作者
一般带RTC片上外设的都可以实现。

使用特权

评论回复
地板
jiekou001| | 2023-2-26 20:19 | 只看该作者
if ( (RTC->INTEN & RTC_INTEN_ALMIEN_Msk) && (RTC->INTSTS & RTC_INTSTS_ALMIF_Msk) )
判断是否发生了RTC报警中断。

使用特权

评论回复
5
jiekou001| | 2023-2-26 20:20 | 只看该作者
    /* Set the alarm time */

    RTC_SetAlarmDateAndTime(&sCurTime);



    /* Clear interrupt status */

    RTC->INTSTS = RTC_INTSTS_ALMIF_Msk;



    /* Enable RTC Alarm Interrupt */

    RTC_EnableInt(RTC_INTEN_ALMIEN_Msk);



    NVIC_EnableIRQ(RTC_IRQn);



    while(!g_bAlarm);

主要操作点在这。

使用特权

评论回复
6
mintspring| | 2023-2-26 21:25 | 只看该作者
中断源太丰富了,非常好用。

使用特权

评论回复
7
小夏天的大西瓜| | 2023-2-27 14:20 | 只看该作者
M031本身不带RTC功能吧

使用特权

评论回复
8
AloneKaven| | 2023-3-1 20:09 | 只看该作者
带RTC的都可以实现吧

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

167

主题

3341

帖子

13

粉丝