本文来源电子发烧友社区,作者:jf_25674040, 帖子地址:
https://bbs.elecfans.com/jishu_2283209_1_1.html
1. 配置rtc,设置成秒中断,2秒闪烁一次;
-
void RTC_IRQhandler(void)//RTC中断函数
-
{
-
RTC_MtiMECMP->mtimecmp += 1000000;
-
s_time_cnt++;
-
GPIO_Toggle(GPIOA,PIN12);
-
}
复制代码
-
void RTC_Init_case1(void)
-
{
-
uint32_t mask=0x1f<<10;
-
-
uint32_t temp = CMU->CLK_DIV;
-
temp &= ~mask;
-
temp |= 31<<10;
-
CMU->CLK_DIV = temp;
-
-
RTC_MTIME->mtime = 0;
-
RTC_MTIMECMP->mtimecmp = 1000000;
-
}
复制代码
2. 配置rtc主函数,同时借用了risc-v编译器中的time库来将秒中断计数值转换为日历。
-
#include "headfile.h"
-
#include
-
-
int main(void)
-
{
-
-
///----System Init ---------------------------------------------------------------------------------------------
-
CLIC_Init();//系统中断配置
-
System_Clock_Init();//系统时钟初始化
-
RTC_Init_case1();
-
Interrupt_Level(MTIMER_int_ID, INT_LEVEL2 );//CLIC设置中断抢占级别
-
Interrupt_Enable(MTIMER_int_ID);//CLIC使能中断
-
SYS_Interrupt_Enable();//CLIC开总中断
-
UART_Init_case1(UART1);//非中断模式
-
time_t raw_time;
-
struct tm *timeinfo;
-
time(&raw_time);
-
timeinfo = localtime(&raw_time);
-
timeinfo->tm_year= 2022 - 1900;
-
timeinfo->tm_mon = 5 -1;
-
timeinfo->tm_mday = 26;
-
timeinfo->tm_hour = 20;
-
timeinfo->tm_min = 15;
-
timeinfo->tm_sec = 15;
-
raw_time = mktime(timeinfo);
-
time_t last_time = s_time_cnt;
-
char buffer[80];
-
while(1)
-
{
-
Delay32M_ms(1000);
-
raw_time += (s_time_cnt -last_time);
-
last_time = s_time_cnt;
-
timeinfo = localtime(&raw_time);
-
raw_time = mktime(timeinfo);
-
strftime(buffer, sizeof(buffer), "%c", timeinfo );
-
printf(buffer);
-
printf("n")
-
}
-
}
复制代码
3. Makefile沿用前面的,同时注意
-
void RTC_IRQhandler(void) __attribute__((interrupt("SiFive-CLIC-preemptible")));
复制代码
在linux编辑中更改为
-
void RTC_IRQhandler(void) __attribute__((interrupt()));
复制代码
4. 效果展示