【AG32VH407开发板】芯片内部RTC测试
[复制链接]
本帖最后由 TL-LED 于 2025-3-18 17:50 编辑
测试芯片内部的RTC数据,将日期和时间串口输出,并在OLED显示屏上显示。下面测试秒中断类型,在秒中断中发出信号量信号,在FreeRTOS线程中,获取信号量,读出RTC的日期和时间信息,显示在OLED显示屏上。
一、RTC介绍
AG32VH407芯片内部的RTC(Real Time CLock) 是个独立的定时器。RTC 模块拥有一个连续计数的计数器, 可进行软件配置, 提供时钟日历的功能。 RTC 还
包含用于管理低功耗模式的自动唤醒单元。只要芯片的备用电源一直供电, 在 mcu 断电情况下 RTC 仍可以独立运行。
RTC 只支持 LSE 作为时钟源(32768) ;
支持 3 种中断类型:
1. 秒中断;
2. 溢出中断;
3. 定时中断;
二、程序部分
2.1、fun_rtc.c
- #include "board.h"
- #include "rtc/fun_rtc.h"
- #include "FreeRTOS.h"
- #include "task.h"
- #include "semphr.h"
- #include "string.h"
- #include "stdio.h"
-
- extern SemaphoreHandle_t semaphore_bin_rtc;
-
- char rtc_date1[50];
- char rtc_date2[50];
-
- void init_rtc(void)
- {
- RTC_Init(board_rtc_source());
- RTC_SetPrescaler(32768);
-
- time_t now = RTC_GetEpochSeconds(2025, 03, 18, 12, 12, 12);
- RTC_SetCounter(now);
-
- INT_EnableIRQ(RTC_IRQn, RTC_PRIORITY);
- RTC_EnableInt(RTC_FLAG_SEC);
- }
-
- void RTC_isr()
- {
- BaseType_t xHigherPriorityTaskRTC;
- if (RTC_IsIntActive(RTC_FLAG_SEC))
- {
- RTC_ClearInt(RTC_FLAG_SEC);
- if(semaphore_bin_rtc != NULL)
- {
- xSemaphoreGiveFromISR(semaphore_bin_rtc, &xHigherPriorityTaskRTC);
- portYIELD_FROM_ISR(xHigherPriorityTaskRTC);
- }
- }
- }
-
- void rtc_disp(void)
- {
- char temp[20];
- time_t now = RTC_GetCounter();
- struct tm *tm = gmtime(&now);
- printf("%d:%02d:%02d-%02d:%02d:%02d\r\n", tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
-
- sprintf(temp, "%d", tm->tm_year+1900);
- strcat(rtc_date1,temp);
- strcat(rtc_date1,"-");
- sprintf(temp, "%02d", tm->tm_mon+1);
- strcat(rtc_date1,temp);
- strcat(rtc_date1,"-");
- sprintf(temp, "%02d", tm->tm_mday);
- strcat(rtc_date1,temp);
-
- sprintf(temp, "%02d", tm->tm_hour);
- strcat(rtc_date2,temp);
- strcat(rtc_date2,":");
- sprintf(temp, "%02d", tm->tm_min);
- strcat(rtc_date2,temp);
- strcat(rtc_date2,":");
- sprintf(temp, "%02d", tm->tm_sec);
- strcat(rtc_date2,temp);
- }
-
-
-
2.2、fun_rtc.h
- #ifndef RTC_H
- #define RTC_H
-
- #define MIN_IRQ_PRIORITY 1
- #define MAX_IRQ_PRIORITY PLIC_MAX_PRIORITY
- #define RTC_PRIORITY (MIN_IRQ_PRIORITY + 6)
-
- extern char rtc_date1[50];
- extern char rtc_date2[50];
-
- void init_rtc(void);
- void rtc_disp(void);
-
- #endif
-
2.3、fun_task.c
在这里创建任务3,用于更新RTC数据,刷新在OLED上显示。
- #include "example.h"
- #include "led/led.h"
- #include "key/key.h"
- #include "timer/mtimer.h"
- #include "lcd/jlx12864g.h"
- #include "oled/oled.h"
- #include "rtc/fun_rtc.h"
-
- #include "FreeRTOS.h"
- #include "task.h"
- #include "queue.h"
- #include "timers.h"
- #include "semphr.h"
-
- #define START_TASK_PRO 1
- #define START_STK_SIZE 512
- TaskHandle_t StartTask_Handler;
-
- #define TASK1_PRIO 4
- #define TASK1_STK_SIZE 128
- static TaskHandle_t Task1Task_Handler = NULL;
-
- #define TASK2_PRIO 3
- #define TASK2_STK_SIZE 256
- static TaskHandle_t Task2Task_Handler = NULL;
-
- #define TASK3_PRIO 5
- #define TASK3_STK_SIZE 256
- static TaskHandle_t Task3Task_Handler = NULL;
-
- SemaphoreHandle_t semaphore_bin_rtc;
-
-
- void start_task(void *pvParameters);
- void task1(void *pvParameters);
- void task2(void *pvParameters);
- void task3(void *pvParameters);
-
- void task_create(void)
- {
-
-
- xTaskCreate((TaskFunction_t )start_task,
- (const char* )"start_task",
- (uint16_t )START_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )START_TASK_PRO,
- (TaskHandle_t* )&StartTask_Handler);
-
- vTaskStartScheduler();
- }
-
- void start_task(void *pvParameters)
- {
- taskENTER_CRITICAL();
-
- semaphore_bin_rtc = xSemaphoreCreateBinary();
-
- xTaskCreate((TaskFunction_t )task1,
- (const char* )"task1",
- (uint16_t )TASK1_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )TASK1_PRIO,
- (TaskHandle_t* )&Task1Task_Handler);
-
- xTaskCreate((TaskFunction_t )task2,
- (const char* )"task2",
- (uint16_t )TASK2_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )TASK2_PRIO,
- (TaskHandle_t* )&Task2Task_Handler);
-
- xTaskCreate((TaskFunction_t )task3,
- (const char* )"task3",
- (uint16_t )TASK3_STK_SIZE,
- (void* )NULL,
- (UBaseType_t )TASK3_PRIO,
- (TaskHandle_t* )&Task3Task_Handler);
-
- vTaskDelete(StartTask_Handler);
- taskEXIT_CRITICAL();
-
- }
-
-
-
- void task1(void *pvParameters)
- {
- while (1)
- {
-
- vTaskDelay(200);
- }
- }
-
-
- void task2(void *pvParameters)
- {
- while (1)
- {
-
- vTaskDelay(500);
- }
- }
-
-
- void task3(void *pvParameters)
- {
- BaseType_t err = pdFALSE;
- init_oled();
- oled_test();
-
- init_rtc();
- while (1)
- {
- if(semaphore_bin_rtc != NULL)
- {
- err = xSemaphoreTake(semaphore_bin_rtc, portMAX_DELAY);
- if(err == pdTRUE)
- {
- rtc_disp();
- oled_showstr(0,4,rtc_date1,2);
- oled_showstr(0,6,rtc_date2,2);
- memset(rtc_date1, 0, sizeof(rtc_date1));
- memset(rtc_date2, 0, sizeof(rtc_date2));
- }
- else if(err == pdFALSE)
- {
- vTaskDelay(10);
- }
- }
- }
- }
-
- void vApplicationTickHook(void)
- {
- return;
- }
-
-
- void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
- StackType_t **ppxIdleTaskStackBuffer,
- uint32_t *pulIdleTaskStackSize)
- {
- static StaticTask_t xIdleTaskTCB;
- static StackType_t uxIdleTaskStack[configMINIMAL_STACK_SIZE];
-
- *ppxIdleTaskTCBBuffer = &xIdleTaskTCB;
- *ppxIdleTaskStackBuffer = uxIdleTaskStack;
- *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
- }
-
- void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer,
- StackType_t **ppxTimerTaskStackBuffer,
- uint32_t *pulTimerTaskStackSize)
- {
- static StaticTask_t xTimerTaskTCB;
- static StackType_t uxTimerTaskStack[configTIMER_TASK_STACK_DEPTH];
-
- *ppxTimerTaskTCBBuffer = &xTimerTaskTCB;
- *ppxTimerTaskStackBuffer = uxTimerTaskStack;
- *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
- }
三、程序运行
编译后,下载code代码,串口输出和OLED显示
3.1、串口输出
3.2、OLED显示日期和时间
鎾斁鍣ㄥ姞杞藉け璐�: 鏈娴嬪埌Flash Player锛岃鍒� 瀹夎
rtc
|