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

📄 rfid_rtc.c

📁 找的一个用U2270B开发的射频卡项目文件
💻 C
字号:
// $Id: rfid_Rtc.c,v 1.3 2006/10/18 18:15:12 tprescott Exp $
/*****************************************************************************
Project : rfid_Rtc.c
Date    : 9/08/2006
Author  : Toby Prescott                   
Company : Atmel                           
Comments: AVR Studio GCC

Revisions:
	v1.0 - Started written for CodeVision 
	v2.6 - Clean for WinAVR
*****************************************************************************/

//  Include files
#include "rfid_Rtc.h"

time t;				// Create time variable      

unsigned char TBL_CLOCK_12[] =   // table used when displaying 12H clock  
{12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};

unsigned char clockformat = CLOCK_12;    // set initial clock format to 12H
//unsigned char timer0Flag = 0;

/******************************************************************************
*   Function name:  rtc_Init
*   Purpose:        Start Timer/Counter2 in asynchronous operation using a
*                   32.768kHz crystal.
*******************************************************************************/
void rtc_Init(void)
{
    t.hour    = 12;
    t.minute  = 0;
    t.second  = 0;
    t.month   = 1;
    t.day     = 12;
    t.year    = 06;

	Timer_delay_ms(1000);
	cli(); 
    #if defined (__AVR_ATmega128__)//mega128 
    TIMSK &= 0xFC;     		//Disable TC0 interrupt
    ASSR |= 0x08;           	//set Timer/Counter0 to be asynchronous from the CPU clock 
                                //with a second external clock(32,768kHz)driving it.  
    TCNT0 = 0x00;
    TCCR0 = 0x05;               //prescale the timer to be clock source / 128 to make it
                                //exactly 1 second for every overflow to occur
    while(ASSR&0x07);           //Wait until TC0 is updated
    TIMSK |= 0x01;        	//set 8-bit Timer/Counter0 Overflow Interrupt Enable                             
    #elif defined (__AVR_AT90USB1287__)//usb1287 
    TIMSK2 &= 0xF8;     		//Disable TC2 interrupts
    ASSR |= 0x20;           	//set Timer/Counter2 to be asynchronous from the CPU clock 
                                //with a second external clock(32,768kHz)driving it.  
    TCNT2 = 0x00;
    TCCR2A = 0x00;               
    TCCR2B = 0x05;               //prescale the timer to be clock source / 128 to make it
                                //exactly 1 second for every overflow to occur
    while(ASSR&0x1F);           //Wait until TC2 is updated
    TIFR2 = 0x00;
    TIMSK2 |= 0x01;        	//set 8-bit Timer/Counter2 Overflow Interrupt Enable                             
    #endif
	sei();
                      
}

/******************************************************************************
*   Timer/Counter0 Overflow Interrupt Routine
*   Purpose: Increment the real-time clock
*            The interrupt occurs once a second (running from the 32kHz crystal)
*******************************************************************************/
#if defined (__AVR_ATmega128__)//mega128 
ISR(TIMER0_OVF_vect)
#elif defined (__AVR_AT90USB1287__)//usb1287 
ISR(TIMER2_OVF_vect)
#endif
{   
//    timer0Flag = 1;
    if (++t.second==60)			// Check for seconds overflow
    {
        t.second=0;
        if (++t.minute==60)    		// Check for minutes overflow
        {
            t.minute=0;
            if (++t.hour==24)		// Check for hours overflow
            {
                t.hour=0;
                if (++t.day>=32)	// Check for days overflow (odd Months)
                {
                    t.month++;
                    t.day=1;
                }
                else if (t.day>=31)    	// Check for days overflow (even Months)
                {                    
                    if ((t.month==4) || (t.month==6) || (t.month==9) || (t.month==11)) 
                    {
                        t.month++;
                        t.day=1;
                    }
                }
                else if (t.day>=30)	// Check for days overflow (Feb)
                {
                    if(t.month==2)
                    {
                       t.month++;
                       t.day=1;
                    }
                }              
                else if (t.day>=29)	// Check for leap year (Feb) 
                {
                    if((t.month==2) && (rtc_NotLeap(t.year)))
                    {
                        t.month++;
                        t.day=1;
                    }                
                }                          
                if (t.month==13)	// Check for months overflow
                {
                    t.month=1;
                    t.year++;
                }
            }
        }
    }     
}

/******************************************************************************
*   Function name: rtc_NotLeap 
*   Purpose: Checks for a leap year
*******************************************************************************/ 
unsigned char rtc_NotLeap(unsigned int year)      //check for leap year
{
    if (!(year%100))
        return (unsigned char)(year%400);
    else
        return (unsigned char)(year%4);
} 

/******************************************************************************
*   Function name: rtc_GetClock
*   Purpose: Will give the current clock value. Pass in the variable to be 
*            returned H/M/S.
*******************************************************************************/ 
unsigned char rtc_GetClock(unsigned char cPos)
{
	if(cPos == HOUR){return t.hour;}
	else if(cPos == MINUTE){return t.minute;}
	else if(cPos == SECOND){return t.second;}
	else return 0;
}

/******************************************************************************
*   Function name: rtc_SetClock
*   Purpose: Allows the Clock to be set. Pass in the variable to be set H/M/S
*            and if increment or decrement. Includes error checking
*******************************************************************************/ 
void rtc_SetClock(unsigned char cPos, unsigned char value)
{
	switch(cPos){
                case HOUR:       
                        if(value > 0 && value < 25)
                        {
                                t.hour = value;
                        }
                        break;

                case MINUTE:             
                        if(value > 0 && value < 61)
                        {
                                t.minute = value;
                        }
                        break;
 
                case SECOND:              
                        if(value > 0 && value < 61)
                        {
                                t.second = value;
                        }
                        break;
                }
}

/******************************************************************************
*   Function name: rtc_SetClockFormat
*   Purpose: Pass in either 12h or 24h clock format
*******************************************************************************/ 
void rtc_SetClockFormat(unsigned char cFormat)
{
	clockformat = cFormat;    // Set the Format
}

/******************************************************************************
*   Function name: rtc_GetDate
*   Purpose: Will give the current date value. Pass in the variable to be 
*            returned M/D/Y.
*******************************************************************************/ 
unsigned char rtc_GetDate(unsigned char cPos)
{
	if(cPos == MONTH){return t.month;}
	else if(cPos == DAY){return t.day;}
	else if(cPos == YEAR){return t.year;}
	else return 0;
}


/******************************************************************************
*   Function name: rtc_SetDate
*   Purpose: Allows the Date to be set. Pass in the variable to be set M/D/Y
*            and if increment or decrement. Includes some error checking.
*******************************************************************************/ 
void rtc_SetDate(unsigned char cPos, unsigned char value)
{
	switch(cPos){
        case MONTH:       
        	if (value < 13){t.month=value;}
                break;

        case DAY:             
       		if(value < 32){t.day = value;}
                break;
 
        case YEAR:              
                if(value <= 99){t.year=value;}
                break;
 	}
 }




 

⌨️ 快捷键说明

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