全部博文(92)
分类: 嵌入式
2010-03-25 15:39:53
RTC
• 时钟数据采用BCD 编码
• 时钟数据有:秒、分、时、日、月、年、星期
• 能够对闰年的年月日进行自动处理
• 具有告警功能,当系统处于关机状态时,能产生告警中断
• 具有独立的电源输入
• 提供毫秒级时钟中断,该中断可用于作为嵌入式操作系统的内核时钟
两个中断处理程序:节拍中断(TICK TIME)和报警中断(ALARM TIME)
void __irq RTC_tickHandler(void);->INT_TICK
void __irq RTC_alarmHandler(void);->INT->RTC
we can use RTC_tickHandler to generate interrupt every (1+n)/128 second(n<=127)
we can use RTC_alarmHandler to generate interrupt in a certain time or every certain time (interrupt in every minute in 10 second)
在写来两个中断之前,先要将RTC设置好:
void RTC_setdate(st_date *p_date)
{
rRTCCON = 0x01; //RTC读写使能,选择BCD时钟、计数器,无复位,1/32768
rBCDYEAR = p_date->year;
rBCDMON = p_date->month;
rBCDDAY = p_date->day;
rBCDDATE = p_date->week_day;
rBCDHOUR = p_date->hour;
rBCDMIN = p_date->minute;
rBCDSEC = p_date->second;
rRTCCON = 0x00; //RTC读写禁止,选择BCD时钟、计数器,无复位,1/32768
}
节拍中断(TICK TIME):
初始化函数:
void RTC_tickIRQ_init(U8 tick)
{
pISR_TICK = (unsigned)RTC_tickHandler; //中断函数的入口地址
EnableIrq(BIT_TICK);//open RTC TICK INTERRUPT
rRTCCON = 0x00; //屏蔽RTC读写
rTICNT = (tick&0x7f)|0x80; //设置tick计数器 和中断使能 (1+n)/128, 0~6位设置时间0~127,7位置1则使能。
}
中断处理函数:
void __irq RTC_tickHandler(void)
{
rGPFDAT = ~(rGPFDAT^0xe); //刷新LED1,只要一个灯亮,我想: rGPFDAT = rGPFDAT ^ 0x1;
led_index ++;
ClearPending(BIT_TICK);
}
报警中断(ALARM TIME)
初始化函数:
void RTC_alarm_setdate(st_date *p_date,U8 mode)
{
rRTCCON = 0x01;
rALMYEAR = p_date->year;
rALMMON = p_date->month;
rALMDAY = p_date->day;
rALMHOUR = p_date->hour;
rALMMIN = p_date->minute;
rALMSEC = p_date->second;
rRTCALM = mode;
rRTCCON = 0x00;
pISR_RTC = (unsigned)RTC_alarmHandler;
EnableIrq(BIT_RTC); //open RTC alarm INTERRUPT
}
中断处理函数:
void __irq RTC_alarmHandler(void)
{
rGPFDAT = (rGPFDAT^0x2); //刷新LED2
alarmflag = 1;
ClearPending(BIT_RTC);
}