【AB32VG1开发板测评】RTC电子时钟
本帖最后由 jinglixixi 于 2021-9-15 00:15 编辑<p style="text-align:justify">AB32VG1内部配有RTC计时器,使用它来计时可免除计时中的进制转换问题,将RTC与OLED屏相结合可轻松地实现电子时钟的功能。</p>
<p>以RTC实现电子时钟功能的步骤如下:</p>
<p>1. 创建一个基于AB32VG1开发板的RT-Thread新项目;</p>
<p>2. 在左侧资源管理器中点击“RT-Thread Setting”,然后选取“更多配置”;</p>
<p>3. 选取“硬件”选项卡,按图1所示进行设置;</p>
<p>图1功能配置</p>
<p>4. 按“Ctrl + S”进行保存,以使RT Thread的配置生效;</p>
<p>5. 在左侧资源管理器的application下,新建一个文件rtc_test.c,其内容为:</p>
<pre>
<code class="language-cpp">#include <rtthread.h>
#include <rtdevice.h>
static int rtc_sample(void)
{
rt_err_t ret = RT_EOK;
time_t now;
/* 设置日期 */
ret = set_date(2021, 06, 02);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failed\n");
return ret;
}
/* 设置时间 */
ret = set_time(11, 15, 50);
if (ret != RT_EOK)
{
rt_kprintf("set RTC time failed\n");
return ret;
}
/* 延时3秒 */
rt_thread_mdelay(3000);
/* 获取时间 */
now = time(RT_NULL);
rt_kprintf("%s\n", ctime(&now));
return ret;
}
/* 在 APP 级自动执行该函数 */
INIT_APP_EXPORT(rtc_sample);</code></pre>
<p>6. 对程序进行编译和下载,其测试结果如图4所示,即按下tab键可查看所支持的指令,若键入date指令,则可查看时间值。</p>
<p>图2完成编译</p>
<p>图3完成下载</p>
<p>图4 测试结果</p>
<p>图5查看指令</p>
<p>若将主程序修改为以下内容:</p>
<pre>
<code class="language-cpp">#include <rtthread.h>
#include "board.h"
#include <rtdevice.h>
int rtc_sample(void)
{
rt_err_t ret = RT_EOK;
/* 设置日期 */
ret = set_date(2021, 06, 02);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failed\n");
}
return ret;
/* 设置时间 */
ret = set_time(11, 15, 50);
if (ret != RT_EOK)
{
rt_kprintf("set RTC time failed\n");
}
return ret;
}
int main(void)
{
time_t now;
uint8_t pin = rt_pin_get("PE.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_kprintf("Hello, world\n");
rtc_sample();
/* 延时3秒 */
rt_thread_mdelay(3000);
while (1)
{
rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(pin, PIN_HIGH);
rt_thread_mdelay(500);
/* 获取时间 */
now = time(RT_NULL);
rt_kprintf("%s\n", ctime(&now));
}
}</code></pre>
<p>在编译下载后,其运行结果则如图6所示。</p>
<p>图6 测试结果</p>
<p>在添加OLED屏显示后,其关键程序如下:</p>
<pre>
<code class="language-cpp">void OLED_ShowStringk(uint8_t x,uint8_t y,uint8_t *chr,uint8_t Char_Size)
{
unsigned char j=0;
uint8_t b;
while (j<24)
{
if(j==11)
{
y=2;
b=x;
x=0;
}
if(j==20)
{
y=0;
x=b;
}
OLED_ShowChar(x,y,chr,Char_Size);
x+=8;
j++;
}
}
static int rtc_sample(void)
{
rt_err_t ret = RT_EOK;
ret = set_date(2021, 9, 12);
if (ret != RT_EOK)
{
rt_kprintf("set RTC date failed\n");
return ret;
}
/* 设置时间 */
ret = set_time(21, 23, 50);
if (ret != RT_EOK)
{
rt_kprintf("set RTC time failed\n");
return ret;
}
return ret;
}
int main(void)
{
time_t now;
uint8_t pin = rt_pin_get("PE.1");
rt_pin_mode(pin, PIN_MODE_OUTPUT);
rt_kprintf("Hello, world\n");
rtc_sample();
pin_scl = rt_pin_get("PE.3");
pin_sda = rt_pin_get("PE.2");
rt_pin_mode(pin_scl, PIN_MODE_OUTPUT);
rt_pin_mode(pin_sda, PIN_MODE_OUTPUT);
OLED_Init();
OLED_Clear();
OLED_ShowString(0,0,"AB32VG1 RISC-V",16);
OLED_ShowString(0,2,"OLED DISPLAY",16);
/* 延时1秒 */
rt_thread_mdelay(1000);
OLED_Clear();
while (1)
{
rt_pin_write(pin, PIN_LOW);
rt_thread_mdelay(500);
rt_pin_write(pin, PIN_HIGH);
rt_thread_mdelay(500);
now = time(RT_NULL);
OLED_ShowStringk(0,0,ctime(&now),16);
}
}</code></pre>
<p>图7 运行效果</p>
<p>RTC电子时钟介绍详细</p>
Jacktang 发表于 2021-9-15 07:39
RTC电子时钟介绍详细
<p>感谢支持!!!</p>
页:
[1]