在厂家的例程中,提供了RTC的使用示例,但它还是有改进的地方,输出的速度太快且大部分数据无实际意义。 经改进,才能输出如下的效果,见图1所示。 图1 秒间隔时间显示 为了实现电子时钟的功能,在例程的基础上通过添加OLED屏,并进行适当的调整即可。 由于OLED屏的驱动要用到us级的延时,因此在definitions.h文件中要添加#include"peripheral/systick/plib_systick.h"。 在文件initialization.c中要添加语句SYSTICK_TimerInitialize(),修改后的内容如下: void SYS_Initialize ( void* data )
{
NVMCTRL_Initialize( );
STDIO_BufferModeSet();
PORT_Initialize();
CLOCK_Initialize();
SERCOM2_USART_Initialize();
RTC_Initialize();
NVIC_Initialize();
SYSTICK_TimerInitialize();
}
此外,还要添加文件plib_systick.c和plib_systick.h。 为了获得us级延时,需添加在us级的延时文件中相应的延时函数,其内容如下: void SYSTICK_Delayus ( uint32_t delay)
{
uint32_t tickStart, delayTicks;
if( (SysTick->CTRL & (SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk)) == (SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk))
{
tickStart=systick.tickCounter;
delayTicks=delay/SYSTICK_INTERRUPT_PERIOD_IN_US;
while((systick.tickCounter-tickStart)<delayTicks)
{
}
}
}
此外,在文件plib_systick.h中也要添加void SYSTICK_Delayus ( uint32_t delay)。 实现图2所示效果的主程序为: int main ( void )
{
int p;
SYS_Initialize ( NULL );
struct tm sys_time;
struct tm alarm_time;
alarm_triggered = false;
alarm_time.tm_hour = 12;
alarm_time.tm_sec = 20;
alarm_time.tm_min = 00;
alarm_time.tm_mon = 11;
alarm_time.tm_year = 122;
alarm_time.tm_mday = 16;
alarm_time.tm_wday = 5;
PORT_REGS->GROUP[2].PORT_DIRSET = 1<<18;
PORT_REGS->GROUP[2].PORT_DIRSET = 1<<20;
PORT_REGS->GROUP[2].PORT_DIRSET = 1<<21;
OLED_Init();
OLED_Clear();
OLED_ShowString(20,0,"ATSAMD51 EVK",16);
OLED_ShowString(20,2,"RTC test",16);
OLED_ShowString(20,5,"jinglixixi",16);
SYSTICK_DelayMs(800);
RTC_RTCCCallbackRegister(RTC_Callback, (uintptr_t) NULL);
RTC_RTCCTimeSet(&sys_time);
RTC_RTCCAlarm0Set(&alarm_time, RTC_ALARM_MASK_HHMMSS);
RTC_RTCCTimeGet(&sys_time);
p=sys_time.tm_sec;
OLED_ShowString(20,5," : : ",16);
while ( true )
{
RTC_RTCCTimeGet(&sys_time);
if(p!=sys_time.tm_sec)
{
OLED_ShowNum(20,5,sys_time.tm_hour,2,16);
OLED_ShowNum(44,5,sys_time.tm_min,2,16);
OLED_ShowNum(68,5,sys_time.tm_sec,2,16);
p=sys_time.tm_sec;
if(p%2==0) PORT_REGS->GROUP[2].PORT_OUT |= 1<<18;
else PORT_REGS->GROUP[2].PORT_OUT &= ~(1<<18);
}
if(alarm_triggered == true)
{
printf("\n\rAlarm Triggered !!!!!!!!\n\r");
alarm_triggered = false;
}
}
return ( EXIT_FAILURE );
}
图2计时效果
视频演示:
|