搞好环境,现在就开始测试外设了,像之前一样,新建一个工程,在图形配置工具中,配置一下,真的很简单,我的配置如图,增加了一个串口配置,用于打印,
接下来就是写一下代码,这个也比较简单,主函数如下
int main ( void )
{
int old_second=0;
/* Initialize all modules */
SYS_Initialize ( NULL );
struct tm sys_time;
//15-01-2018 12:00:00 Monday
sys_time.tm_hour = 23; /* hour [0,23] */
sys_time.tm_sec = 00; /* seconds [0,61] */
sys_time.tm_min = 26; /* minutes [0,59] */
sys_time.tm_mon = 11; /* month of year [0,11] */
sys_time.tm_year = 2022; /* years since 1900 */
sys_time.tm_mday = 10; /* day of month [1,31] */
sys_time.tm_wday = 6; /* day of week [0,6] (Sunday = 0) */
/* tm_yday - day of year [0,365] */
/* tm_isdst - daylight savings flag */
printf("\n\r---------------------------------------------------------");
printf("\n\r RTC Demo ");
printf("\n\r---------------------------------------------------------\n\r");
RTC_RTCCTimeSet(&sys_time);
while ( true )
{
RTC_RTCCTimeGet(&sys_time);
if(old_second!=sys_time.tm_sec)
{
printf("System time is: %02d:%02d:%02d\r\n",sys_time.tm_hour, sys_time.tm_min, sys_time.tm_sec);
old_second=sys_time.tm_sec;
}
/* Maintain state machines of all polled MPLAB Harmony modules. */
SYS_Tasks ( );
}
/* Execution should not come here during normal operation */
return ( EXIT_FAILURE );
}
值得注意的是我并没有用他自带的函数,RTC_RTCCTimeSet(&sys_time); RTC_RTCCTimeGet(&sys_time);,这两个都是我自已加的,在例程中可以找到
bool RTC_RTCCTimeSet (struct tm * initialTime )
{
/*
* Add 1900 to the tm_year member and the adjust for the RTC reference year
* Set YEAR(according to Reference Year), MONTH and DAY
*set Hour Minute and Second
*/
RTC_REGS->MODE2.RTC_CLOCK = (uint32_t)(((TM_STRUCT_REFERENCE_YEAR + (uint32_t)initialTime->tm_year) - REFERENCE_YEAR) << RTC_MODE2_CLOCK_YEAR_Pos |
((ADJUST_MONTH((uint32_t)initialTime->tm_mon)) << RTC_MODE2_CLOCK_MONTH_Pos) |
((uint32_t)initialTime->tm_mday << RTC_MODE2_CLOCK_DAY_Pos) |
((uint32_t)initialTime->tm_hour << RTC_MODE2_CLOCK_HOUR_Pos) |
((uint32_t)initialTime->tm_min << RTC_MODE2_CLOCK_MINUTE_Pos) |
((uint32_t)initialTime->tm_sec << RTC_MODE2_CLOCK_SECOND_Pos));
while((RTC_REGS->MODE2.RTC_SYNCBUSY & RTC_MODE2_SYNCBUSY_CLOCK_Msk) == RTC_MODE2_SYNCBUSY_CLOCK_Msk)
{
/* Synchronization after writing value to CLOCK Register */
}
return true;
}
void RTC_RTCCTimeGet ( struct tm * currentTime )
{
uint32_t dataClockCalendar = 0U;
uint32_t timeMask = 0U;
while((RTC_REGS->MODE2.RTC_SYNCBUSY & RTC_MODE2_SYNCBUSY_CLOCKSYNC_Msk) == RTC_MODE2_SYNCBUSY_CLOCKSYNC_Msk)
{
/* Synchronization before reading value from CLOCK Register */
}
dataClockCalendar = RTC_REGS->MODE2.RTC_CLOCK;
timeMask = (dataClockCalendar & RTC_MODE2_CLOCK_HOUR_Msk) >> RTC_MODE2_CLOCK_HOUR_Pos;
currentTime->tm_hour = (int)timeMask;
timeMask = (dataClockCalendar & RTC_MODE2_CLOCK_MINUTE_Msk) >> RTC_MODE2_CLOCK_MINUTE_Pos;
currentTime->tm_min = (int)timeMask;
timeMask = (dataClockCalendar & RTC_MODE2_CLOCK_SECOND_Msk) >> RTC_MODE2_CLOCK_SECOND_Pos;
currentTime->tm_sec = (int)timeMask;
timeMask = ADJUST_TM_STRUCT_MONTH(((dataClockCalendar & RTC_MODE2_CLOCK_MONTH_Msk) >> RTC_MODE2_CLOCK_MONTH_Pos));
currentTime->tm_mon = (int)timeMask;
timeMask = (((dataClockCalendar & RTC_MODE2_CLOCK_YEAR_Msk)>> RTC_MODE2_CLOCK_YEAR_Pos) + REFERENCE_YEAR) - TM_STRUCT_REFERENCE_YEAR;
currentTime->tm_year = (int)timeMask;
timeMask = (dataClockCalendar & RTC_MODE2_CLOCK_DAY_Msk) >> RTC_MODE2_CLOCK_DAY_Pos;
currentTime->tm_mday = (int)timeMask;
}
还有需要定义一个结构体
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
long __tm_gmtoff;
const char *__tm_zone;
};
至此就可以编译下载了,
经过一个晚上到中午的测试,误差很大,相差有两分钟了。
|