#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('
');
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('
');
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[] = {"
===> Send a frame with 6 Byte data to set RTC
['a']+[year]+[month]+[day]+[hour]+[minute]+[second]
"};
char str[]={"
Time set ok!
Current time set to:
"};
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);
}
程序运行串口打印信息如下图: