⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 2410rtc.c

📁 基于嵌入式处理器S3C2410的实时时钟控制源代码
💻 C
字号:
//====================================================================
// File Name : 2410RTC.c
// Function  : S3C2410 RTC Test Program
// Program   : Shin, On Pil (SOP)
// Date      : May 06, 2002
// Version   : 0.0
// History
//   0.0 : Programming start (March 11, 2002) -> SOP
//====================================================================

//#include "includes.h"
#include "2410addr.h"
#include "2410lib.h"
#include "2410RTC.h"

static char *day[] = {"         ", " Sunday  ", " Monday  ", " Tuesday ", "Wednesday", "Thursday ", " Friday  ", "Saturday "};

volatile int isRtcInt, isInit = 2;              //April 01, 2002 SOP
volatile unsigned int sec_tick;

//************************[ Rtc_TimeSet ]*********************************

static unsigned int IntToBCD(unsigned int num)
{
	int a, b;
    num = num % 100;
	a = num % 10;
    num = num - a;
	b = num / 10;
	num = (b<<4) | a;
	return num;
}


//=======================================
//Period = (n + 1) / 128 second
//   n : Tick time count value (1~127)
//=======================================
//********************[ Display_Rtc ]*********************************
void Display_Rtc(void)
{
//    int year,tmp;
    int year,tmp;//,key;                   
    int month,date,weekday,hour,min,sec;


//    Rtc_Init();

    rRTCCON = 0x01;    //No reset, Merge BCD counters, 1/32768, RTC Control enable
    Uart_Printf("按任意键退出。\n\n");

   
    while(!Uart_GetKey()) { 
  //  while(1) {     
        while(1)
        {
            if(rBCDYEAR==0x99) 
                year = 0x1999;
            else 
                year    = 0x2000 + rBCDYEAR;
            month   = rBCDMON;
            weekday = rBCDDAY;
            date    = rBCDDATE;
            hour    = rBCDHOUR;
            min     = rBCDMIN;
            sec     = rBCDSEC;
            
            if(sec!=tmp)                //Same time is not display
            {
                tmp = sec;
                break;
            }         
        }
    
       Uart_Printf("%2x : %2x : %2x  %10s,  %2x/%2x/%4x\n",hour,min,sec,day[weekday],month,date,year);
      // if(Uart_GetKey()==0x1b) break;
    }
    rRTCCON = 0x0; //No reset, Merge BCD counters, 1/32768, RTC Control disable(for power consumption)
}

//***********************[ RndRst_Rtc ]*********************************
//Round boundary 30, 40 or 50 Sec
//For example, when the current time is 23:37:47 and the round baundary is selected to 40 sec,
//the round reset changes the current time is 23:38:00.
void RndRst_Rtc(void)
{
    int year;
    int month,date,weekday,hour,min,sec,tmp;
    unsigned int save_GPFCON;

    save_GPFCON = rGPFCON;

    rEXTINT0 = 0x2;  //Falling edge triggered
    rGPFCON  = 0x2;  //EINT0

    pISR_EINT0 = (unsigned int)EINT0_int;
    rINTMSK    = ~(BIT_EINT0);

    if(isInit==0)
    {
        Rtc_Init();
        isInit = 1;
    }

    rRTCCON = 0x01;    //No reset, Merge BCD counters, 1/32768, RTC Control enable
    Uart_Printf("Press any key to exit.\n\n");
    Uart_Printf("Press EINT0 key to test round reset.\n");
    
    while(!Uart_GetKey())
    {
        while(1)
        {
            if(rBCDYEAR == 0x99) 
                year = 0x1999;
            else 
                year    = 0x2000 + rBCDYEAR;
            month   = rBCDMON;
            weekday = rBCDDAY;
            date    = rBCDDATE;
            hour    = rBCDHOUR;
            min     = rBCDMIN;
            sec     = rBCDSEC;
            
            if(sec!=tmp)
            {
                tmp = sec;
                break;
            }
        }      
        Uart_Printf("%2x : %2x : %2x  %10s,  %2x/%2x/%4x\n",hour,min,sec,day[weekday],month,date,year);
    }
    rRTCCON = 0x0;   //No reset, Merge BCD counters, 1/32768, RTC Control disable
    rGPFCON = save_GPFCON;
}


//************************[ Rtc_Init ]*********************************
void Rtc_Init(void)
{
    rRTCCON  = rRTCCON  & ~(0xf)  | 0x1;            //No reset, Merge BCD counters, 1/32768, RTC Control enable
    
    rBCDYEAR = rBCDYEAR & ~(0xff) | TESTYEAR;
    rBCDMON  = rBCDMON  & ~(0x1f) | TESTMONTH;
    rBCDDATE = rBCDDATE & ~(0x3f) | TESTDATE;         
    rBCDDAY  = rBCDDAY  & ~(0x7)  | TESTDAY;       //SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7
    rBCDHOUR = rBCDHOUR & ~(0x3f) | TESTHOUR;
    rBCDMIN  = rBCDMIN  & ~(0x7f) | TESTMIN;
    rBCDSEC  = rBCDSEC  & ~(0x7f) | TESTSEC;
    
    rRTCCON  = 0x0;             //No reset, Merge BCD counters, 1/32768, RTC Control disable    
}


 void Rtc_TimeSet(void)
{
    int syear,smonth,sdate,shour,smin,ssec,sday;

    Uart_Printf("\nYear (Two digit the latest)[0x??] : ");
    syear = Uart_GetIntNum();
    
    Uart_Printf("\nMonth [0x??] : ");
    smonth = Uart_GetIntNum();
    
    Uart_Printf("\nDate  [0x??] : ");
    sdate = Uart_GetIntNum();
          
    Uart_Printf("\n1:Sunday  2:Monday  3:Tuesday  4:Wednesday  5:Thursday  6:Friday  7:Saturday");
    Uart_Printf("\nDay of the week : ");
    sday = Uart_GetIntNum();
    
    Uart_Printf("\nHour [0x??] : ");
    shour = Uart_GetIntNum();
    
    Uart_Printf("\nMinute [0x??] : ");
    smin = Uart_GetIntNum();
    
    Uart_Printf("\nSecond [0x??] : ");
    ssec = Uart_GetIntNum();
    
    rRTCCON  = 0x1;
	if(rRTCCON == 0x01);

    rBCDYEAR =IntToBCD(syear);
    rBCDMON  =IntToBCD(smonth);
    rBCDDATE = IntToBCD(sdate);
    rBCDDAY  = IntToBCD(sday);           //SUN:1 MON:2 TUE:3 WED:4 THU:5 FRI:6 SAT:7

    rBCDHOUR = IntToBCD(shour);
    rBCDMIN  = IntToBCD(smin);
    rBCDSEC  = IntToBCD(ssec);

    
    rRTCCON  = 0x0;
}

void RunRTCTest(void)
{
	char key;
    unsigned int year, month, date, weekday, hour, min, sec;
    
    Uart_Printf("\n\n实时时钟实验\n");
    
    //设置时间
	Uart_Printf("\n是否设置时间? [Y/N]:");
    key=Uart_Getch();
	if(key=='Y' || key=='y')
	{
		Rtc_TimeSet();
		
	}
	
	do
	{
	    rRTCCON = 0x01;
	    if(rRTCCON == 0x01);
        if(rBCDYEAR==0x99) 
            year = 0x1999;
        else 
        	year    = 0x2000 + rBCDYEAR;
        
        month   = rBCDMON;
        weekday = rBCDDAY;
        date    = rBCDDATE;
        hour    = rBCDHOUR;
        min     = rBCDMIN;
        sec     = rBCDSEC;
	    rRTCCON = 0x0;
		Uart_Printf("\r%04x-%02x-%02x %9s %02x:%02x:%02x", year, month, date, day[weekday], hour, min, sec);
	    Uart_Printf("\n时间设置结束,按任意键显示。\n");
	}while(!Uart_Getch());
_rtcend:
   ;
}


void __irq EINT0_int(void)
{
    rSRCPND = BIT_EINT0;   
    rINTPND = BIT_EINT0;
    rINTPND;

    rRTCRST = (1<<3) | 3;   //Round second reset enable, over than 30 sec
}

//-----------------------------------------------------------------------
void __irq Rtc_Int(void)
{
    rSRCPND = BIT_RTC;   
    rINTPND = BIT_RTC;
    rINTPND;

    Uart_Printf("RTC Alarm Interrupt O.K.\n");
    isRtcInt = 1;  
}

//---------------------------------------------------------------------
void __irq Rtc_Tick(void)
{
    rSRCPND = BIT_TICK;   
    rINTPND = BIT_TICK;
    rINTPND;

    Uart_Printf("\b\b\b\b\b\b\b%03d sec",sec_tick++);
}

⌨️ 快捷键说明

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