通过PC标定开发板时间及更新间隔。
PC通过串口发送日期时间(格式自己指定,比如2022-03-05 22:33:23/5),开发板接收到时间后设置rtc. 并用定时器中断,每隔5s打印当前时间。
实验过程
1、编译代码,生成RTC.bin文件
2、连接开发板。查看端口。
3、打开putty
4、通电
5、打开NuWriter
烧录
6、拔掉帽 USB->NAND
7、选择reset
8、打印时间
main.c(编译代码,生成RTC.bin文件)
#include <stdio.h>
#include "nuc970.h"
#include "sys.h"
#include "rtc.h"
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
UINT32 volatile g_u32TICK = 0;
BOOL volatile g_bAlarm = FALSE;
VOID Smpl_RTC_Powerdown_Wakeup(void);
VOID Smpl_RTC_Powerdown_Wakeup_Relative(void);
VOID Smpl_RTC_PowerOff_Control(UINT32 u32Mode);
#define HW_POWER_OFF 0
#define SW_POWER_OFF 1
/*---------------------------------------------------------------------------------------------------------*/
/* RTC Tick Callback function */
/*---------------------------------------------------------------------------------------------------------*/
VOID RTC_TickISR(void)
{
RTC_TIME_DATA_T sCurTime;
/* Get the currnet time */
RTC_Read(RTC_CURRENT_TIME, &sCurTime);
if(g_u32TICK%5==0&&g_u32TICK>1)
sysprintf(" Current Time:%d/%02d/%02d %02d:%02d:%02d\n",sCurTime.u32Year,sCurTime.u32cMonth,sCurTime.u32cDay,sCurTime.u32cHour,sCurTime.u32cMinute,sCurTime.u32cSecond);
RTC_EnableClock(TRUE);
g_u32TICK++;
}
/*---------------------------------------------------------------------------------------------------------*/
/* RTC Alarm Callback function */
/*---------------------------------------------------------------------------------------------------------*/
VOID RTC_AlarmISR(void)
{
sysprintf(" Abolute Alarm!!\n");
g_bAlarm = TRUE;
}
VOID RTC_Releative_AlarmISR(void)
{
sysprintf(" Relative Alarm!!\n");
g_bAlarm = TRUE;
}
int main()
{
//UINT32 u32Item, u32ExtFreq;
UINT32 u32Item;
RTC_TIME_DATA_T sInitTime;
//UINT32 u32ClockSource;
sysDisableCache();
sysFlushCache(I_D_CACHE);
sysEnableCache(CACHE_WRITE_BACK);
sysInitializeUART();
RTC_EnableClock(TRUE);
/* RTC Initialize */
RTC_Init();
/* Time Setting */
sInitTime.u32Year = 2022;
sInitTime.u32cMonth = 3;
sInitTime.u32cDay = 17;
sInitTime.u32cHour = 20;
sInitTime.u32cMinute = 30;
sInitTime.u32cSecond = 0;
sInitTime.u32cDayOfWeek = RTC_TUESDAY;
sInitTime.u8cClockDisplay = RTC_CLOCK_24;
/* Initialization the RTC timer */
if(RTC_Open(&sInitTime) !=E_RTC_SUCCESS)
sysprintf("Open Fail!!\n");
sysSetLocalInterrupt(ENABLE_IRQ);
sysprintf("Do RTC Calibration \n");
RTC_Ioctl(0,RTC_IOC_SET_FREQUENCY, 0,0);
do
{
sysprintf("=======================================\n");
sysprintf(" RTC Demo Code \n");
sysprintf("=======================================\n");
sysprintf(" (Clock from External 32K Hz) \n");
sysprintf("=======================================\n");
sysprintf("Select Demo Item:\n");
sysprintf("0. Time Display Test\n");
sysprintf("Q. Exit\n");
u32Item = sysGetChar();
switch(u32Item)
{
case '0':
{
RTC_TICK_T sTick;
sysprintf("\n0. RTC Time Display Test (Exit after 5 seconds)\n");
/* Set Tick property */
sTick.ucMode = RTC_TICK_1_SEC;
sTick.pfnTickCallBack = RTC_TickISR;
/* Set Tick setting */
RTC_Ioctl(0,RTC_IOC_SET_TICK_MODE, (UINT32)&sTick,0);
/* Enable RTC Tick Interrupt and install tick call back function */
RTC_Ioctl(0,RTC_IOC_ENABLE_INT, (UINT32)RTC_TICK_INT,0);
g_u32TICK = 0;
RTC_EnableClock(TRUE);
while(g_u32TICK < 26);
RTC_EnableClock(FALSE);
/* Disable RTC Tick Interrupt */
RTC_Ioctl(0,RTC_IOC_DISABLE_INT, (UINT32)RTC_TICK_INT,0);
break;
}
default:
sysprintf("Wrong Item\n");
break;
}
}while((u32Item!= 'q') || (u32Item!= 'Q'));
/* Disable RTC */
RTC_Close();
/* Disable I & F bit */
sysSetLocalInterrupt(DISABLE_IRQ);
}