(转)Cortex-M3 (NXP LPC1788)之RTC
实时时钟是一组用于测量时间的计数器,如果使用电池供电,在系统掉电以后它也可以正常运行以记录系统的时间。LPC1788时钟采用内部的32K振荡器输出1HZ的时钟信号做为RTC的时钟源。
RTC的寄存器比较简单,主要有时钟计数器寄存器包括秒SEC 分MIN 小时HOUR 日期(月)DOM 星期DOW 日期(年)DOY 月MONTH 年YEAR, 这些寄存器为R/W 可以从中读出具体的时间信息。其中的秒计数由1HZ时钟驱动。报警寄存器组中的值将和时间计数器寄存器中的值比较,如果所有为屏蔽的报警寄存器都与他们对应的时间计数器相匹配,那么将产生一次中断。报警屏蔽在报警屏蔽寄存器AMR中设置。中断设置在中断位置寄存器ILR中设置。RTC中断不仅可以在报警寄存器和时间计数器匹配时产生,我们也可以配置计数器增量中断寄存器CIIR,使计数器每增加1就产生一次中断。RTC的控制在时钟控制寄存器CCR中,我们可以使能或禁止时钟,以及复位等。
在下面的程序中,首先PC端使用串口软件发送一串固定格式的时间信息给开发板,开发板收到字符‘a’表示后面跟着的是时间信息,设置了初始时间后,我们配置CCIR使1秒产生一次中断,配置报警寄存器组合报价屏蔽寄存器,使秒计数为30的时候产生中断。在RTC的中断函数中,如果是计数中断,就让接LED的GPIO输出反向电平,根据设置LED灯将1S闪烁。 如果是报警中断,就通过串口在PC打印时间信息。
注意:为了程序的简洁,省去了之前介绍了的系统时钟配置和串口的配置。具体的信息可查询之前的文章。
- #include "LPC1788_REG.h"
- #include "uart.h"
- #define rILR (*(volatile unsigned*)0x40024000)
- #define rCCR (*(volatile unsigned*)0x40024008)
- #define rCIIR (*(volatile unsigned*)0x4002400C)
- #define rAMR (*(volatile unsigned*)0x40024010)
- #define rCALIBRATION (*(volatile unsigned*)0x40024040)
- #define rYEAR (*(volatile unsigned*)0x4002403C)
- #define rMONTH (*(volatile unsigned*)0x40024038)
- #define rDOM (*(volatile unsigned*)0x4002402C)
- #define rHOUR (*(volatile unsigned*)0x40024028)
- #define rMIN (*(volatile unsigned*)0x40024024)
- #define rSEC (*(volatile unsigned*)0x40024020)
- #define rALSEC (*(volatile unsigned*)0x40024060)
- #define rCTIME0 (*(volatile unsigned*)0x40024014)
- #define rCTIME1 (*(volatile unsigned*)0x40024018)
- #define rCTIME2 (*(volatile unsigned*)0x4002401C)
- unsigned char flag_setTime=1;
- unsigned char flag_receiveStatus=0;
- unsigned char timeData[14],cnt;
- void Set_Data()
- {
- rCCR &= ~(0x1<<0);
- rYEAR = (timeData[0]-'0')*1000 + (timeData[1]-'0')*100 + (timeData[2]-'0')*10 + (timeData[3]-'0');
- rMONTH = (timeData[4]-'0')*10 + (timeData[5]-'0');
- rDOM = (timeData[6]-'0')*10 + (timeData[7]-'0');
- rHOUR = (timeData[8]-'0')*10 + (timeData[9]-'0');
- rMIN = (timeData[10]-'0')*10 + (timeData[11]-'0');
- rSEC = (timeData[12]-'0')*10 + (timeData[13]-'0');
- }
- void Display_Data()
- {
- Uart2SendC('\n');
- Uart2SendC(rYEAR/1000+'0');
- Uart2SendC(rYEAR%1000/100+'0');
- Uart2SendC(rYEAR%100/10+'0');
- Uart2SendC(rYEAR%10+'0');
- Uart2SendC('-');
- Uart2SendC(rMONTH/10+'0');
- Uart2SendC(rMONTH%10+'0');
- Uart2SendC('-');
- Uart2SendC(rDOM/10+'0');
- Uart2SendC(rDOM%10+'0');
- Uart2SendC('\n');
- Uart2SendC(rHOUR/10+'0');
- Uart2SendC(rHOUR%10+'0');
- Uart2SendC(':');
- Uart2SendC(rMIN/10+'0');
- Uart2SendC(rMIN%10+'0');
- Uart2SendC(':');
- Uart2SendC(rSEC/10+'0');
- Uart2SendC(rSEC%10+'0');
- }
- void UART2_IRQHandler()
- {
- unsigned int intId;
- char tmp_char;
- intId = rU2IIR&0xf;
- if(intId == 0xc || intId == 0x4) //RDA或者CTI中断
- {
- rU2LCR &= ~(0x1<<7); //DLAB=0
- tmp_char = rU2RBR&0xff;
- rU2THR = tmp_char;
- }
- if(tmp_char == 'a' && flag_receiveStatus == 0)
- {
- flag_receiveStatus = 1;
- cnt = 0;
- }
- elseif(flag_receiveStatus == 1)
- {
- timeData[cnt]=tmp_char;
- cnt++;
- if(cnt == 14)
- {
- Set_Data();
- cnt = 0;
- flag_receiveStatus = 0;
- flag_setTime=0;
- }
- }
- }
- void RTC_IRQHandler()
- {
- unsigned char IntStatus;
- IntStatus = rILR;
- if(IntStatus & 0x1) //计数中断
- {
- rFIO1PIN = ~rFIO1PIN;
- rILR = IntStatus;
- }
- elseif (IntStatus & (0x1<<1)) //报警中断
- {
- Display_Data();
- rILR = IntStatus;
- }
- }
- void Init_RTC()
- {
- rILR = 0;
- rCCR = 0;
- rCIIR = 0;
- rAMR = 0xff;
- rCALIBRATION = 0;
- rCCR |= 0x1<<1; //CTC Reset
- rCCR &= ~(0x1<<1);
- }
- int main(void)
- {
- char menu[] = {"\n\r===> Send a frame with 6 Byte data to set RTC \n['a']+[year]+[month]+[day]+[hour]+[minute]+[second]\n"};
- char str[]={"\r\nTime set ok! \r\nCurrent time set to:\r\n"};
- rFIO1DIR |= (1<<18); //GPIO1.18 -> OUTPUT
- Init_Uart2();
- Uart2SendS(menu);
- while(flag_setTime);
- Uart2SendS(str);
- Display_Data();
- rCCR |= 0x1;
- rCCR |= 0x1<<4;
- rCIIR |= 0x1; //秒值增加产生一次中断
- rAMR &= ~(0x1<<0); //秒值与报警寄存器比较
- rALSEC = 30; //秒值为30的时候产生一个报警
- rISER0 |= 0x1<<17; //使能RTC中断
- while(1);
- }
#include "LPC1788_REG.h" #include "uart.h" #define rILR (*(volatile unsigned*)0x40024000) #define rCCR (*(volatile unsigned*)0x40024008) #define rCIIR (*(volatile unsigned*)0x4002400C) #define rAMR (*(volatile unsigned*)0x40024010) #define rCALIBRATION (*(volatile unsigned*)0x40024040) #define rYEAR (*(volatile unsigned*)0x4002403C) #define rMONTH (*(volatile unsigned*)0x40024038) #define rDOM (*(volatile unsigned*)0x4002402C) #define rHOUR (*(volatile unsigned*)0x40024028) #define rMIN (*(volatile unsigned*)0x40024024) #define rSEC (*(volatile unsigned*)0x40024020) #define rALSEC (*(volatile unsigned*)0x40024060) #define rCTIME0 (*(volatile unsigned*)0x40024014) #define rCTIME1 (*(volatile unsigned*)0x40024018) #define rCTIME2 (*(volatile unsigned*)0x4002401C) unsigned char flag_setTime=1; unsigned char flag_receiveStatus=0; unsigned char timeData[14],cnt; void Set_Data() { rCCR &= ~(0x1<<0); rYEAR = (timeData[0]-'0')*1000 + (timeData[1]-'0')*100 + (timeData[2]-'0')*10 + (timeData[3]-'0'); rMONTH = (timeData[4]-'0')*10 + (timeData[5]-'0'); rDOM = (timeData[6]-'0')*10 + (timeData[7]-'0'); rHOUR = (timeData[8]-'0')*10 + (timeData[9]-'0'); rMIN = (timeData[10]-'0')*10 + (timeData[11]-'0'); rSEC = (timeData[12]-'0')*10 + (timeData[13]-'0'); } void Display_Data() { Uart2SendC('\n'); Uart2SendC(rYEAR/1000+'0'); Uart2SendC(rYEAR%1000/100+'0'); Uart2SendC(rYEAR%100/10+'0'); Uart2SendC(rYEAR%10+'0'); Uart2SendC('-'); Uart2SendC(rMONTH/10+'0'); Uart2SendC(rMONTH%10+'0'); Uart2SendC('-'); Uart2SendC(rDOM/10+'0'); Uart2SendC(rDOM%10+'0'); Uart2SendC('\n'); Uart2SendC(rHOUR/10+'0'); Uart2SendC(rHOUR%10+'0'); Uart2SendC(':'); Uart2SendC(rMIN/10+'0'); Uart2SendC(rMIN%10+'0'); Uart2SendC(':'); Uart2SendC(rSEC/10+'0'); Uart2SendC(rSEC%10+'0'); } void UART2_IRQHandler() { unsigned int intId; char tmp_char; intId = rU2IIR&0xf; if(intId == 0xc || intId == 0x4) //RDA或者CTI中断 { rU2LCR &= ~(0x1<<7); //DLAB=0 tmp_char = rU2RBR&0xff; rU2THR = tmp_char; } if(tmp_char == 'a' && flag_receiveStatus == 0) { flag_receiveStatus = 1; cnt = 0; } else if(flag_receiveStatus == 1) { timeData[cnt]=tmp_char; cnt++; if(cnt == 14) { Set_Data(); cnt = 0; flag_receiveStatus = 0; flag_setTime=0; } } } void RTC_IRQHandler() { unsigned char IntStatus; IntStatus = rILR; if(IntStatus & 0x1) //计数中断 { rFIO1PIN = ~rFIO1PIN; rILR = IntStatus; } else if (IntStatus & (0x1<<1)) //报警中断 { Display_Data(); rILR = IntStatus; } } void Init_RTC() { rILR = 0; rCCR = 0; rCIIR = 0; rAMR = 0xff; rCALIBRATION = 0; rCCR |= 0x1<<1; //CTC Reset rCCR &= ~(0x1<<1); } int main(void) { char menu[] = {"\n\r===> Send a frame with 6 Byte data to set RTC \n['a']+[year]+[month]+[day]+[hour]+[minute]+[second]\n"}; char str[]={"\r\nTime set ok! \r\nCurrent time set to:\r\n"}; rFIO1DIR |= (1<<18); //GPIO1.18 -> OUTPUT Init_Uart2(); Uart2SendS(menu); while(flag_setTime); Uart2SendS(str); Display_Data(); rCCR |= 0x1; rCCR |= 0x1<<4; rCIIR |= 0x1; //秒值增加产生一次中断 rAMR &= ~(0x1<<0); //秒值与报警寄存器比较 rALSEC = 30; //秒值为30的时候产生一个报警 rISER0 |= 0x1<<17; //使能RTC中断 while(1); }
程序运行串口打印信息如下图:
posted on 2012-09-26 19:15 tdyizhen1314 阅读(5592) 评论(0) 编辑 收藏 举报