ARM學(xué)習(xí)筆記--RTC編程(二)
RTC的寄存器比較簡單,主要有時鐘計數(shù)器寄存器包括秒SEC 分MIN 小時HOUR 日期(月)DOM 星期DOW 日期(年)DOY 月MONTH 年YEAR, 這些寄存器為R/W 可以從中讀出具體的時間信息。其中的秒計數(shù)由1HZ時鐘驅(qū)動。報警寄存器組中的值將和時間計數(shù)器寄存器中的值比較,如果所有為屏蔽的報警寄存器都與他們對應(yīng)的時間計數(shù)器相匹配,那么將產(chǎn)生一次中斷。報警屏蔽在報警屏蔽寄存器AMR中設(shè)置。中斷設(shè)置在中斷位置寄存器ILR中設(shè)置。RTC中斷不僅可以在報警寄存器和時間計數(shù)器匹配時產(chǎn)生,我們也可以配置計數(shù)器增量中斷寄存器CIIR,使計數(shù)器每增加1就產(chǎn)生一次中斷。RTC的控制在時鐘控制寄存器CCR中,我們可以使能或禁止時鐘,以及復(fù)位等。
本文引用地址:http://m.butianyuan.cn/article/201611/317286.htm下面這段代碼就是RTC寄存器結(jié)構(gòu)體,里面包含了關(guān)于RTC的寄存器的一個列舉:/*------------- Real-Time Clock (RTC) ----------------------------------------*/
typedef struct
{
__IO uint8_t ILR;
uint8_t RESERVED0[7];
__IO uint8_t CCR;
uint8_t RESERVED1[3];
__IO uint8_t CIIR;
uint8_t RESERVED2[3];
__IO uint8_t AMR;
uint8_t RESERVED3[3];
__I uint32_t CTIME0;
__I uint32_t CTIME1;
__I uint32_t CTIME2;
__IO uint8_t SEC;
uint8_t RESERVED4[3];
__IO uint8_t MIN;
uint8_t RESERVED5[3];
__IO uint8_t HOUR;
uint8_t RESERVED6[3];
__IO uint8_t DOM;
uint8_t RESERVED7[3];
__IO uint8_t DOW;
uint8_t RESERVED8[3];
__IO uint16_t DOY;
uint16_t RESERVED9;
__IO uint8_t MONTH;
uint8_t RESERVED10[3];
__IO uint16_t YEAR;
uint16_t RESERVED11;
__IO uint32_t CALIBRATION;
__IO uint32_t GPREG0;
__IO uint32_t GPREG1;
__IO uint32_t GPREG2;
__IO uint32_t GPREG3;
__IO uint32_t GPREG4;
__IO uint8_t RTC_AUXEN;
uint8_t RESERVED12[3];
__IO uint8_t RTC_AUX;
uint8_t RESERVED13[3];
__IO uint8_t ALSEC;
uint8_t RESERVED14[3];
__IO uint8_t ALMIN;
uint8_t RESERVED15[3];
__IO uint8_t ALHOUR;
uint8_t RESERVED16[3];
__IO uint8_t ALDOM;
uint8_t RESERVED17[3];
__IO uint8_t ALDOW;
uint8_t RESERVED18[3];
__IO uint16_t ALDOY;
uint16_t RESERVED19;
__IO uint8_t ALMON;
uint8_t RESERVED20[3];
__IO uint16_t ALYEAR;
uint16_t RESERVED21;
__IO uint32_t ERSTATUS;
__IO uint32_t ERCONTROL;
__IO uint32_t ERCOUNTERS;
uint32_t RESERVED22;
__IO uint32_t ERFIRSTSTAMP0;
__IO uint32_t ERFIRSTSTAMP1;
__IO uint32_t ERFIRSTSTAMP2;
uint32_t RESERVED23;
__IO uint32_t ERLASTSTAMP0;
__IO uint32_t ERLASTSTAMP1;
__IO uint32_t ERLASTSTAMP2;
} LPC_RTC_TypeDef;
void RTC_Init (LPC_RTC_TypeDef *RTCx)
{
/* Set up clock and power for RTC module */
CLKPWR_ConfigPPWR (CLKPWR_PCONP_PCRTC, ENABLE); //開啟RTC功能,配置寄存器PCONP
// Clear all register to be default
RTCx->ILR = 0x00; // 中斷地址寄存器,增量中斷、報警中斷
RTCx->CCR = 0x00; // 時鐘控制寄存器(控制使能、復(fù)位、是否校準(zhǔn))
RTCx->CIIR = 0x00;// 計數(shù)增量中斷寄存器 (控制按秒或分等變化一次發(fā)生中斷)
RTCx->AMR = 0xFF; // 報警中斷寄存器
RTCx->CALIBRATION = 0x00; //校準(zhǔn)寄存器,看時間走多長然后校準(zhǔn)一次(1s或2s)
}
void RTC_ResetClockTickCounter(LPC_RTC_TypeDef *RTCx)
{
RTCx->CCR |= RTC_CCR_CTCRST;
RTCx->CCR &= (~RTC_CCR_CTCRST) & RTC_CCR_BITMASK; // RTC_CCR_BITMASK:1101
}
void RTC_Default_Set(void)
{
//Set current time
RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0);//參數(shù):(LPC_RTC_TypeDef *RTCx, uint32_t Timetype, uint32_t TimeValue)
RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MINUTE, 30);
RTC_SetTime (LPC_RTC, RTC_TIMETYPE_HOUR, 11);
RTC_SetTime (LPC_RTC, RTC_TIMETYPE_DAYOFMONTH, 31);
RTC_SetTime (LPC_RTC, RTC_TIMETYPE_MONTH, 8);
RTC_SetTime (LPC_RTC, RTC_TIMETYPE_YEAR, 12);
}
RTC_CntIncrIntConfig (LPC_RTC, RTC_TIMETYPE_SECOND, ENABLE);
/* Enable RTC interrupt */
NVIC_EnableIRQ(RTC_IRQn);
void RTC_IRQHandler(void)
{
uint32_t secval;
/* This is increment counter interrupt*/
if (RTC_GetIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE))
{
secval = RTC_GetTime (LPC_RTC, RTC_TIMETYPE_SECOND);
/* Send debug information */
_DBG ("Current time: ");
_DBD(RTC_GetTime (LPC_RTC, RTC_TIMETYPE_YEAR)); _DBG ("年");
_DBD(RTC_GetTime (LPC_RTC, RTC_TIMETYPE_MONTH)); _DBG ("月");
_DBD(RTC_GetTime (LPC_RTC, RTC_TIMETYPE_DAYOFMONTH));_DBG ("日");
_DBD(RTC_GetTime (LPC_RTC, RTC_TIMETYPE_HOUR)); _DBG (":");
_DBD(RTC_GetTime (LPC_RTC, RTC_TIMETYPE_MINUTE));_DBG (":");
_DBD(RTC_GetTime (LPC_RTC, RTC_TIMETYPE_SECOND));
_DBG_("");
// Clear pending interrupt
RTC_ClearIntPending(LPC_RTC, RTC_INT_COUNTER_INCREASE);
}
}
評論