rtc.c

来自「Freescale MCF5445evb 参考测试代码」· C语言 代码 · 共 289 行

C
289
字号
/* * File:		main.c * Purpose:		Main process * */#include "common.h"#include "uif.h"__interrupt__void rtc_exception_handler (void);void rtc_display_tod (void);void rtc_set_tod (int, int, int);/********************************************************************//* * Setup user interface */void mainloop (void);void cmd_tod (int, char**);void cmd_rd (int, char **);void cmd_stop (int, char **);const char PROMPT[] = "RTC> ";UIF_CMD UIF_CMDTAB[] ={    UIF_CMDS_ALL    {"rd",  0,0,0,cmd_rd,   "Register Disply",""},    {"tod",0,0,0,cmd_tod,   "Display Time",""},    {"stop",0,1,0,cmd_stop, "Enter stop mode","<secs to wakeup>"},};const int UIF_NUM_CMD    = UIF_CMDTAB_SIZE;UIF_SETCMD UIF_SETCMDTAB[] = {"",0,0,NULL,""};const int UIF_NUM_SETCMD = 0;/********************************************************************/void main (void){	char ch;    /* Display register reset values */    cmd_rd(0, NULL);    			/* Initialize the interrupts for the RTC */	MCF_INTC0_ICR63 = MCF_INTC_ICR_IL(7);	MCF_INTC0_IMRH &= ~MCF_INTC_IMRH_INT_MASK63;    mcf5xxx_set_handler(64 + 63, (ADDRESS)rtc_exception_handler);	mcf5xxx_irq_enable();	    MCF_RTC_GOCL = 32768;    MCF_RTC_ISR = 0xFFFFFFFF;        printf("\n");    printf("**************************************************\n");    printf("*                                                *\n");    printf("*                  RTC Utility                   *\n");    printf("*                                                *\n");    printf("**************************************************\n");    printf(HELPMSG);    printf("\n");    rtc_set_tod(0,0,0);    	/* Initialize RTC alarm */	//MCF_RTC_ALRM_HM = MCF_RTC_ALRM_HM_MINUTES(0);    //MCF_RTC_ALRM_SEC = MCF_RTC_ALRM_SEC_SECONDS(15-1);    		/* Enable RTC Alarm interrupts */	//MCF_RTC_IER |= MCF_RTC_IER_ALM;	//MCF_RTC_IER |= MCF_RTC_IER_2HZ;	    mainloop();}/********************************************************************/voidmainloop (void){    /* Enable interrupts to the core */    mcf5xxx_irq_enable();    while (TRUE)    {        printf(PROMPT);        run_cmd();    }}/********************************************************************/voidcmd_tod (int argc, char **argv){    rtc_display_tod();}/********************************************************************/voidcmd_stop (int argc, char **argv){    int alrm_sec, success = FALSE;    int hr, min, sec;        if (argc == 0)        alrm_sec = 15;    else    {        alrm_sec = get_value(argv[1], &success, 10);        if (!success || alrm_sec < 1 || alrm_sec > 60)            alrm_sec = 15;    }        MCF_PMM_LPCR = 0        | MCF_PMM_LPCR_FWKUP        | MCF_PMM_LPCR_STPMD(3);    MCF_PMM_WCR = 0        | MCF_PMM_WCR_PRILVL(0)        | MCF_PMM_WCR_LPMD_STOP        | MCF_PMM_WCR_ENBWCR;    MCF_RTC_IER |= MCF_RTC_IER_ALM;        printf("Going to sleep. Will wakeup in %d seconds\n", alrm_sec);    /* Wait for the UART to finish transmitting */    while(!(MCF_UART_UCSR(TERM_PORT) & MCF_UART_USR_TXEMP));        alrm_sec--;    hr = (MCF_RTC_HOURMIN >> 8) & 0x1F;    min = MCF_RTC_HOURMIN & 0x3F;    sec = MCF_RTC_SECONDS & 0x3F;	printf("Time: %02d:%02d:%02d\n", hr, min, sec);    min = (min + ((alrm_sec + sec) / 60)) % 60;    hr += (min / 60);    sec = (alrm_sec + sec) % 60;    printf("Alrm: %02d:%02d:%02d\n", hr, min, sec+1);        MCF_RTC_ALRM_SEC = sec;    MCF_RTC_ALRM_HM = 0        | MCF_RTC_ALRM_HM_HOURS(hr)        | MCF_RTC_ALRM_HM_MINUTES(min);        stop_2000();    //while(1);}/********************************************************************/voidcmd_rd (int argc, char **argv){    printf("RTC Registers:\n");    printf("MCF_RTC_HOURMIN  = %08X\n", MCF_RTC_HOURMIN);    printf("MCF_RTC_SECONDS  = %08X\n", MCF_RTC_SECONDS);    printf("MCF_RTC_ALRM_HM  = %08X\n", MCF_RTC_ALRM_HM);    printf("MCF_RTC_ALRM_SEC = %08X\n", MCF_RTC_ALRM_SEC);    printf("MCF_RTC_CR       = %08X\n", MCF_RTC_CR);    printf("MCF_RTC_ISR      = %08X\n", MCF_RTC_ISR);    printf("MCF_RTC_IER      = %08X\n", MCF_RTC_IER);    printf("MCF_RTC_STPWCH   = %08X\n", MCF_RTC_STPWCH);    printf("MCF_RTC_DAYS     = %08X\n", MCF_RTC_DAYS);    printf("MCF_RTC_ALRM_DAY = %08X\n", MCF_RTC_ALRM_DAY);    printf("MCF_RTC_GOCU     = %08X\n", MCF_RTC_GOCU);    printf("MCF_RTC_GOCL     = %08X\n", MCF_RTC_GOCL);}/********************************************************************/__interrupt__void rtc_exception_handler (void) {    int isr = MCF_RTC_ISR & MCF_RTC_IER;        if (isr & MCF_RTC_ISR_SAM7)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM7\n");        #endif    }    if (isr & MCF_RTC_ISR_SAM6)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM6\n");        #endif    }    if (isr & MCF_RTC_ISR_SAM5)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM5\n");        #endif    }    if (isr & MCF_RTC_ISR_SAM4)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM4\n");        #endif    }    if (isr & MCF_RTC_ISR_SAM3)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM3\n");        #endif    }    if (isr & MCF_RTC_ISR_SAM2)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM2\n");        #endif    }    if (isr & MCF_RTC_ISR_SAM1)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM1\n");        #endif    }    if (isr & MCF_RTC_ISR_SAM0)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SAM0\n");        #endif    }    if (isr & MCF_RTC_ISR_2HZ)    {        #ifdef DEBUG_PRINT        printf("\nRTC-2HZ\n");        #endif    }    if (isr & MCF_RTC_ISR_HR)    {        #ifdef DEBUG_PRINT        printf("\nRTC-HR\n");        #endif    }    if (isr & MCF_RTC_ISR_1HZ)    {        #ifdef DEBUG_PRINT        printf("\nRTC-1HZ\n");        #endif    }    if (isr & MCF_RTC_ISR_DAY)    {        #ifdef DEBUG_PRINT        printf("\nRTC-DAY\n");        #endif    }    if (isr & MCF_RTC_ISR_ALM)    {        #ifdef DEBUG_PRINT        printf("\nRTC-ALM\n");        #endif    }    if (isr & MCF_RTC_ISR_MIN)    {        #ifdef DEBUG_PRINT        printf("\nRTC-MIN\n");        #endif    }    if (isr & MCF_RTC_ISR_SW)    {        #ifdef DEBUG_PRINT        printf("\nRTC-SW\n");        #endif    }        rtc_display_tod();       	/* Clear interrupt flag */   	MCF_RTC_ISR |= isr;}/********************************************************************/voidrtc_display_tod (void){    int hr, min, sec;        hr = (MCF_RTC_HOURMIN >> 8) & 0x1F;    min = MCF_RTC_HOURMIN & 0x3F;    sec = MCF_RTC_SECONDS & 0x3F;    	printf("Time: %02d:%02d:%02d\n", hr, min, sec);}/********************************************************************/voidrtc_set_tod (int hr, int min, int sec){    MCF_RTC_SECONDS = sec;    MCF_RTC_HOURMIN = 0        | MCF_RTC_HOURMIN_HOURS(hr)        | MCF_RTC_HOURMIN_MINUTES(min);}/********************************************************************/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?