【国民技术N32WB031_STB开发板评测】带日历的温湿计
<div class='showpostmsg'><ol><li><a href="https://bbs.eeworld.com.cn/thread-1241062-1-1.html">【新提醒】N32WB03x系列蓝牙芯片 产品简介 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241020-1-1.html">【新提醒】N32WB031 API函数使用说明 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241013-1-1.html">【新提醒】【国民技术N32WB031_STB开发板评测】资源的下载 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241678-1-1.html">【国民技术N32WB031_STB开发板评测】创建MDK模版 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241679-1-1.html">【国民技术N32WB031_STB开发板评测】GPIO之按键与LED灯 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241731-1-1.html">【新提醒】【国民技术N32WB031_STB开发板评测】串口+LOG分级打印</a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241815-1-1.html">【国民技术N32WB031_STB开发板评测】OLED驱动 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241869-1-1.html">【国民技术N32WB031_STB开发板评测】驱动HS3003 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241820-1-1.html">【国民技术N32WB031_STB开发板评测】SPI驱动墨水屏 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1241870-1-1.html">【国民技术N32WB031_STB开发板评测】点用 PA4后下载不程序 </a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1242031-1-1.html">【国民技术N32WB031_STB开发板评测】解决SWDCLK、SWDIO配置为普通IO后不能下载固件</a></li>
<li><a href="https://bbs.eeworld.com.cn/thread-1242198-1-1.html#pid3228991">【国民技术N32WB031_STB开发板评测】墨水屏温湿度计 </a></li>
</ol>
<hr />
<p>在温湿计上,增加RTC功能,实现日期时间的功能</p>
<p>1、引入rtc:</p>
<p> 2、新建rtc.c/rtc.h</p>
<p>内容如下:</p>
<pre>
<code>#include "rtc.h"
#include "n32wb03x_rtc.h"
#include "n32wb03x_rcc.h"
#include "log.h"
#include "main.h"
RTC_DateType RTC_DateStructure;
RTC_DateType RTC_DateDefault;
RTC_TimeType RTC_TimeStructure;
RTC_TimeType RTC_TimeDefault;
RTC_InitType RTC_InitStructure;
RTC_AlarmType RTC_AlarmStructure;
uint32_t SynchPrediv, AsynchPrediv;
void rtc_int(void)
{
RTC_DateAndTimeDefaultVale();
/* RTC clock source select 1:LSE 2:LSI*/
RTC_CLKSourceConfig(2);
RTC_PrescalerConfig();
log_info("\r\n RTC configured....");
/* Adjust time by values entered by the user on the hyperterminal */
/* Disable the RTC Wakeup Interrupt and delay more than 100us before set data and time */
RTC_ConfigInt(RTC_INT_WUT, DISABLE);
RTC_EnableWakeUp(DISABLE);
RTC_DateRegulate();
RTC_TimeRegulate();
/* wake up clock select */
WakeUpClockSelect(5);
/* wake up timer value */
RTC_EnableWakeUp(DISABLE);
RTC_SetWakeUpCounter(60);
EXTI9_RTCWKUP_Configuration(ENABLE);
/* Enable the RTC Wakeup Interrupt */
RTC_ConfigInt(RTC_INT_WUT, ENABLE);
RTC_EnableWakeUp(ENABLE);
}
/**
* @brief Wake up clock config.
*/
void WakeUpClockSelect(uint8_t WKUPClkSrcSel)
{
/* Configure the RTC WakeUp Clock source: CK_SPRE (1Hz) */
if (WKUPClkSrcSel == 0x01)
RTC_ConfigWakeUpClock(RTC_WKUPCLK_RTCCLK_DIV16);
else if (WKUPClkSrcSel == 0x02)
RTC_ConfigWakeUpClock(RTC_WKUPCLK_RTCCLK_DIV8);
else if (WKUPClkSrcSel == 0x03)
RTC_ConfigWakeUpClock(RTC_WKUPCLK_RTCCLK_DIV4);
else if (WKUPClkSrcSel == 0x04)
RTC_ConfigWakeUpClock(RTC_WKUPCLK_RTCCLK_DIV2);
else if (WKUPClkSrcSel == 0x05)
RTC_ConfigWakeUpClock(RTC_WKUPCLK_CK_SPRE_16BITS);
else if (WKUPClkSrcSel == 0x06)
RTC_ConfigWakeUpClock(RTC_WKUPCLK_CK_SPRE_17BITS);
}
/**
* @briefConfig RTC wake up Interrupt.
*/
void EXTI9_RTCWKUP_Configuration(FunctionalState Cmd)
{
EXTI_InitType EXTI_InitStructure;
NVIC_InitType NVIC_InitStructure;
EXTI_ClrITPendBit(EXTI_LINE9);
EXTI_InitStructure.EXTI_Line = EXTI_LINE9;
#ifdef __TEST_SEVONPEND_WFE_NVIC_DIS__
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Event;
#else
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
#endif
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_InitPeripheral(&EXTI_InitStructure);
/* Enable the RTC WakeUp Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = Cmd;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @briefDisplay the current Date on the Hyperterminal.
*/
void RTC_DateShow(void)
{
/* Get the current Date */
RTC_GetDate(RTC_FORMAT_BIN, &RTC_DateStructure);
log_info("\n\r //=========== Current Date Display ==============// \n\r");
log_info("\n\r The current date (WeekDay-Date-Month-Year) is :%0.2d-%0.2d-%0.2d-%0.2d \n\r",
RTC_DateStructure.WeekDay,
RTC_DateStructure.Date,
RTC_DateStructure.Month,
RTC_DateStructure.Year);
}
/**
* @briefDisplay the current time on the Hyperterminal.
*/
void RTC_TimeShow(void)
{
/* Get the current Time and Date */
RTC_GetTime(RTC_FORMAT_BIN, &RTC_TimeStructure);
log_info("\n\r //============ Current Time Display ===============// \n\r");
log_info("\n\r The current time (Hour-Minute-Second) is :%0.2d:%0.2d:%0.2d \n\r",
RTC_TimeStructure.Hours,
RTC_TimeStructure.Minutes,
RTC_TimeStructure.Seconds);
/* Unfreeze the RTC DAT Register */
(void)RTC->DATE;
}
/**
* @briefRTC initalize default value.
*/
void RTC_DateAndTimeDefaultVale(void)
{
// Date
RTC_DateDefault.WeekDay = 3;
RTC_DateDefault.Date = 6;
RTC_DateDefault.Month = 5;
RTC_DateDefault.Year = 23;
// Time
RTC_TimeDefault.H12 = RTC_AM_H12;
RTC_TimeDefault.Hours = 17;
RTC_TimeDefault.Minutes = 25;
RTC_TimeDefault.Seconds = 1;
}
/**
* @briefRTC date regulate with the default value.
*/
ErrorStatus RTC_DateRegulate(void)
{
uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
log_info("\n\r //=============Date Settings================// \n\r");
log_info("\n\r Please Set WeekDay (01-07) \n\r");
tmp_hh = RTC_DateDefault.WeekDay;
if (tmp_hh == 0xff)
{
return ERROR;
}
else
{
RTC_DateStructure.WeekDay = tmp_hh;
}
log_info(": %0.2d\n\r", tmp_hh);
tmp_hh = 0xFF;
log_info("\n\r Please Set Date (01-31) \n\r");
tmp_hh = RTC_DateDefault.Date;
if (tmp_hh == 0xff)
{
return ERROR;
}
else
{
RTC_DateStructure.Date = tmp_hh;
}
log_info(": %0.2d\n\r", tmp_hh);
log_info("\n\r Please Set Month (01-12)\n\r");
tmp_mm = RTC_DateDefault.Month;
if (tmp_mm == 0xff)
{
return ERROR;
}
else
{
RTC_DateStructure.Month = tmp_mm;
}
log_info(": %0.2d\n\r", tmp_mm);
log_info("\n\r Please Set Year (00-99)\n\r");
tmp_ss = RTC_DateDefault.Year;
if (tmp_ss == 0xff)
{
return ERROR;
}
else
{
RTC_DateStructure.Year = tmp_ss;
}
log_info(": %0.2d\n\r", tmp_ss);
/* Configure the RTC date register */
if (RTC_SetDate(RTC_FORMAT_BIN, &RTC_DateStructure) == ERROR)
{
log_info("\n\r>> !! RTC Set Date failed. !! <<\n\r");
return ERROR;
}
else
{
log_info("\n\r>> !! RTC Set Date success. !! <<\n\r");
RTC_DateShow();
return SUCCESS;
}
}
/**
* @briefRTC time regulate with the default value.
*/
ErrorStatus RTC_TimeRegulate(void)
{
uint32_t tmp_hh = 0xFF, tmp_mm = 0xFF, tmp_ss = 0xFF;
log_info("\n\r //==============Time Settings=================// \n\r");
RTC_TimeStructure.H12 = RTC_TimeDefault.H12;
log_info("\n\r Please Set Hours \n\r");
tmp_hh = RTC_TimeDefault.Hours;
if (tmp_hh == 0xff)
{
return ERROR;
}
else
{
RTC_TimeStructure.Hours = tmp_hh;
}
log_info(": %0.2d\n\r", tmp_hh);
log_info("\n\r Please Set Minutes \n\r");
tmp_mm = RTC_TimeDefault.Minutes;
if (tmp_mm == 0xff)
{
return ERROR;
}
else
{
RTC_TimeStructure.Minutes = tmp_mm;
}
log_info(": %0.2d\n\r", tmp_mm);
log_info("\n\r Please Set Seconds \n\r");
tmp_ss = RTC_TimeDefault.Seconds;
if (tmp_ss == 0xff)
{
return ERROR;
}
else
{
RTC_TimeStructure.Seconds = tmp_ss;
}
log_info(": %0.2d\n\r", tmp_ss);
/* Configure the RTC time register */
if (RTC_ConfigTime(RTC_FORMAT_BIN, &RTC_TimeStructure) == ERROR)
{
log_info("\n\r>> !! RTC Set Time failed. !! <<\n\r");
return ERROR;
}
else
{
log_info("\n\r>> !! RTC Set Time success. !! <<\n\r");
RTC_TimeShow();
return SUCCESS;
}
}
/**
* @briefRTC prescaler config.
*/
static void RTC_PrescalerConfig(void)
{
/* Configure the RTC data register and RTC prescaler */
RTC_InitStructure.RTC_AsynchPrediv = AsynchPrediv;
RTC_InitStructure.RTC_SynchPrediv= SynchPrediv;
RTC_InitStructure.RTC_HourFormat = RTC_24HOUR_FORMAT;
/* Check on RTC init */
if (RTC_Init(&RTC_InitStructure) == ERROR)
{
log_info("\r\n //******* RTC Prescaler Config failed **********// \r\n");
}
}
/**
* @briefConfigures RTC.
* Configure the RTC peripheral by selecting the clock source
*/
void RTC_CLKSourceConfig(uint8_t ClkSrc)
{
assert_param(IS_CLKSRC_VALUE(ClkSrc));
/* Enable the PWR clock */
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_PWR, ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE);
/* Disable RTC clock */
RCC_EnableRtcClk(DISABLE);
if (ClkSrc == 0x01)
{
log_info("\r\n RTC_ClkSrc Is Set LSE! \r\n");
#if (_TEST_LSE_BYPASS_)
RCC_ConfigLse(RCC_LSE_BYPASS);
#else
RCC_ConfigLse(RCC_LSE_ENABLE);
#endif
while (RCC_GetFlagStatus(RCC_LSCTRL_FLAG_LSERD) == RESET)
{
}
RCC_ConfigLSXSEL(RCC_RTCCLK_SRC_LSE);
RCC_EnableLsi(DISABLE);
SynchPrediv= 0xFF; // 32.768KHz
AsynchPrediv = 0x7F; // value range: 0-7F
}
else if (ClkSrc == 0x02)
{
log_info("\r\n RTC_ClkSrc Is Set LSI! \r\n");
RCC_EnableLsi(ENABLE);
while (RCC_GetFlagStatus(RCC_LSCTRL_FLAG_LSIRD) == RESET)
{
}
RCC_ConfigLSXSEL(RCC_RTCCLK_SRC_LSI);
SynchPrediv= 0xFF; // 32.768KHz
AsynchPrediv = 0x7F;// value range: 0-7F
}
else
{
log_info("\r\n RTC_ClkSrc Value is error! \r\n");
}
/* Enable the RTC Clock */
RCC_EnableRtcClk(ENABLE);
RTC_WaitForSynchro();
}</code></pre>
<p>rtc.h:</p>
<pre>
<code>#ifndef __RTC_H
#define __RTC_H
#include "n32wb03x.h"
void RTC_CLKSourceConfig(uint8_t ClkSrc);
ErrorStatus RTC_TimeRegulate(void);
ErrorStatus RTC_DateRegulate(void);
void RTC_DateAndTimeDefaultVale(void);
static void RTC_PrescalerConfig(void);
void EXTI9_RTCWKUP_Configuration(FunctionalState Cmd);
void RTC_TimeShow(void);
void RTC_DateShow(void);
void WakeUpClockSelect(uint8_t WKUPClkSrcSel);
void rtc_int(void);
#endif</code></pre>
<p>3、在主程序中添加日历显示代码:</p>
<pre>
<code> while (1)
{
if(rtc_status == 1)
{
rtc_status = 0;
RTC_TimeShow();
RTC_GetTime(RTC_FORMAT_BIN, &RTC_TimeStructure);
RTC_GetDate(RTC_FORMAT_BIN, &RTC_DateStructure);
//log_debug("debug\r\n");
LedBlink(LED1_PORT, LED1_PIN);
Humiture_HS3003_init();
//log_info("LED FLASH\r\n");
Humiture_HS3003_ReadData_Raw(&humi,&temp);
EPD_HW_Init(); //Electronic paper initialization
EPD_HW_Init_GUI(); //EPD init GUI
Paint_Clear(WHITE);
sprintf(show_str,"温度%02d.%02d℃",(int)temp, (int)(temp*100)%100);
Paint_DrawString_CN(40,0,show_str, &Font24CN, WHITE, BLACK);
sprintf(show_str,"湿度%02d.%02d%%",(int)humi, (int)(humi*100)%100);
Paint_DrawString_CN(40,40,show_str, &Font24CN, WHITE, BLACK);
sprintf(show_str, "%02d-%02d-%02d %02d:%02d:%02d",
RTC_DateStructure.Year,
RTC_DateStructure.Month,
RTC_DateStructure.Date,
RTC_TimeStructure.Hours,
RTC_TimeStructure.Minutes,
RTC_TimeStructure.Seconds);
Paint_DrawString_EN(0,100, show_str, &Font24, WHITE, BLACK);
EPD_Display(BlackImage); //display image
EPD_DeepSleep();//EPD_DeepSleep,Sleep instruction is necessary, please do not delete!!!
}</code></pre>
<p>4、RTC添加中断,中断时间到了,更新标志:</p>
<pre>
<code>void RTC_IRQHandler(void)
{
if (RTC_GetITStatus(RTC_INT_WUT) != RESET)
{
//printf("\r\n I am in rtc_wkup!!! \r\n");
//RTC_TimeShow();
rtc_status = 1;
RTC_ClrIntPendingBit(RTC_INT_WUT);
EXTI_ClrITPendBit(EXTI_LINE9);
}
}</code></pre>
<p>【实验效果】</p>
<p>定时60秒钟更新一次显示。</p>
<p> </p>
</div><script> var loginstr = '<div class="locked">查看本帖全部内容,请<a href="javascript:;" style="color:#e60000" class="loginf">登录</a>或者<a href="https://bbs.eeworld.com.cn/member.php?mod=register_eeworld.php&action=wechat" style="color:#e60000" target="_blank">注册</a></div>';
if(parseInt(discuz_uid)==0){
(function($){
var postHeight = getTextHeight(400);
$(".showpostmsg").html($(".showpostmsg").html());
$(".showpostmsg").after(loginstr);
$(".showpostmsg").css({height:postHeight,overflow:"hidden"});
})(jQuery);
} </script><script type="text/javascript">(function(d,c){var a=d.createElement("script"),m=d.getElementsByTagName("script"),eewurl="//counter.eeworld.com.cn/pv/count/";a.src=eewurl+c;m.parentNode.insertBefore(a,m)})(document,523)</script> <p>国民技术N32WB031_STB开发板评测系列总结,收藏</p>
Jacktang 发表于 2023-5-6 22:59
国民技术N32WB031_STB开发板评测系列总结,收藏
<p>感谢大佬关注。国民技术这块蓝牙芯片,目前感觉是非常好的一款。</p>
<p>墨水屏看着就是舒服,点赞收藏 </p>
<p>没有体验体验蓝牙吗? </p>
秦天qintian0303 发表于 2023-5-8 17:05
没有体验体验蓝牙吗?
<p>正在学习之中呀,蓝牙的例程看了好久,还没有整明白呀。</p>
秦天qintian0303 发表于 2023-5-8 17:05
没有体验体验蓝牙吗?
<p>我是在想是不是整个蓝牙在线设置时间的。现在还有低功耗唤醒没有解决好。</p>
页:
[1]