上一篇咱们从用户手册对RTC有了一个大致的了解,现在就开端经过程序编写来学习RTC的运用和操作了。这儿先总结一下:实时时钟是一组用于丈量时刻的计数器,假如运用电池供电,在体系掉电今后它也能够正常运转以记载体系的时刻。LPC1788时钟选用内部的32K振荡器输出1HZ的时钟信号做为RTC的时钟源。
RTC的寄存器比较简单,首要有时钟计数器寄存器包含秒SEC 分MIN 小时HOUR 日期(月)DOM 星期DOW 日期(年)DOY 月MONTH 年YEAR, 这些寄存器为R/W 能够从中读出详细的时刻信息。其间的秒计数由1HZ时钟驱动。报警寄存器组中的值将和时刻计数器寄存器中的值比较,假如所有为屏蔽的报警寄存器都与他们对应的时刻计数器相匹配,那么将产生一次中止。报警屏蔽在报警屏蔽寄存器AMR中设置。中止设置在中止方位寄存器ILR中设置。RTC中止不只能够在报警寄存器和时刻计数器匹配时产生,咱们也能够装备计数器增量中止寄存器CIIR,使计数器每添加1就产生一次中止。RTC的操控在时钟操控寄存器CCR中,咱们能够使能或制止时钟,以及复位等。
下面这段代码便是RTC寄存器结构体,里边包含了关于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;
RTC初始化
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; // 时钟操控寄存器(操控使能、复位、是否校准)
RTCx->CIIR = 0x00;// 计数增量中止寄存器 (操控按秒或分等改变一次产生中止)
RTCx->AMR = 0xFF; // 报警中止寄存器
RTCx->CALIBRATION = 0x00; //校准寄存器,看时刻走多长然后校准一次(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
}
设置RTC时钟的初始值
void RTC_Default_Set(void)
{
//Set current time
RTC_SetTime (LPC_RTC, RTC_TIMETYPE_SECOND, 0);//参数:(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);
}
}
运转成果如下