打开主菜单

谷雨文档中心 β

更改

NBDK-L4:基础实验教程

添加18,270字节2019年2月19日 (二) 11:09
无编辑摘要
== 实验16-RTC实时时钟 ==
实时时钟实验,给大家展示一下STM32L4的实时计时功能,我们提供给大家设置当前时间、日期、以及星期的函数接口,并且提供了一个闹钟配置的函数接口。这样就可以完成一个简单的老式闹钟的功能展示。
 
=== STM32L476 RTC时钟简介 ===
简介:
''<span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span>''
=== 实验准备 ===
# 使用miniUSB线及10pin排线,通过Jlink仿真器连接PC端和开发板。
# 使用miniUSB线,连接PC与开发板USB接口。
# 使用Keil打开基础实验的实验16-RTC实时时钟
# 下载程序,并完成功能测试。
 
=== 实验验证 ===
下载完成后,可以看到TFT屏幕上打印当前的实时时间、日期以及星期,且10s后会触发闹钟(表现为TFT打印"Alarm"、蜂鸣器哔一声)。
 
=== 源码详解 ===
本节中的源码说明,仅针对此例程中的重要功能,详细的源码介绍请大家参照代码后的注释。
 
==== stm32l4xx_hal_conf.h ====
此文件位于“实验16-RTC实时时钟\Inc”路径中,主要用途是选择使能此例程使用到的库文件。
 
此例程我们主要给大家展示STM32L4的实时时钟功能,所以我们宏定义中打开RTC相关的。<syntaxhighlight lang="c" line="1" start="103">
// 使能的宏
#define HAL_MODULE_ENABLED // 芯片
#define HAL_FLASH_MODULE_ENABLED // Flash
#define HAL_PWR_MODULE_ENABLED // 电源
#define HAL_RCC_MODULE_ENABLED // 时钟
#define HAL_CORTEX_MODULE_ENABLED // NVIC
 
#define HAL_GPIO_MODULE_ENABLED // GPIO
#define HAL_DMA_MODULE_ENABLED // DMA
#define HAL_UART_MODULE_ENABLED // UART
#define HAL_SPI_MODULE_ENABLED // SPI
#define HAL_RTC_MODULE_ENABLED // RTC实时时钟
</syntaxhighlight>
 
==== main.c ====
main函数,我们的例程由此处开始执行,首先调用HAL_Init()函数初始化我们的模块,接着调用SystemClock_Config()函数初始化此例程用到的时钟,具体有哪些时钟被初始化,在gyu_util.c部分有详细说明。
 
接下来我们初始化TFT彩屏相关的SPI控制接口,以及GUI图形界面初始化。并且格式化打印一些内容,字体显示为红色的部分。
 
最后我们初始化RTC时钟,并且调用实现好的有关RTC的函数,去配置当前的时钟、日期、星期、以及闹钟。
 
在while()循环当中,持续获取当前的时钟、日期等相关信息,并且打印到TFT彩屏上显示。
 
当检测到闹钟信息,则在TFT彩屏上打印"Alarm"字样指示闹钟,停止蜂鸣器工作,并且让闹钟标识置为0。
 
<syntaxhighlight lang="c++" line="1" start="44">
int main(void)
{
HAL_StatusTypeDef status;
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
HAL_Init();
 
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
// 重置所有外设、flash界面以及系统时钟
// 配置系统时钟(包含振荡器、系统时钟、总线时钟等等)
SystemClock_Config();
// LCD SPI初始化
LCD_GPIO_Init(); // LCD IO控制引脚(例如背光)
MX_SPI1_Init(); // LCD SPI控制引脚
// 图形界面初始化
GUI_Init(); // GUI界面初始化
GUI_Clear(); // 清屏
GUI_SetColor(GUI_Crimson); // 红色字体
GUI_SetBkColor(GUI_Gold); // 金色背景
// 格式化打印如下的内容
GUI_DispStringAt("Time: : :",24,24);
GUI_DispStringAt("Date: 20 / /",24,72);
GUI_DispStringAt("Week: ",24,120);
// 初始化蜂鸣器
Buzzer_Init();
// RTC初始化
MX_RTC_Init();
RTC_TIME_Set(16,12,30); // 设置当前时间 时,分,秒
RTC_DATE_Set(2,18,12,25); // 设置当前日期 星期,年,月,日
RTC_AlarmA_Set(16,12,40,2); // 设置闹钟 时,分,秒,星期
//
while(1)
{
GUI_SetColor(GUI_Aqua);
// 获取时分秒,并且打印到TFT屏
status = HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
if(status == HAL_OK)
{
GUI_DispDecAt(sTime.Hours,96,24,2);
GUI_DispDecAt(sTime.Minutes,132,24,2);
GUI_DispDecAt(sTime.Seconds,168,24,2);
}
// 获取年月日、星期,并且打印到TFT屏
status = HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN);
if(status == HAL_OK)
{
GUI_DispDecAt(sDate.Year,120,72,2);
GUI_DispDecAt(sDate.Month,156,72,2);
GUI_DispDecAt(sDate.Date,192,72,2);
GUI_DispDecAt(sDate.WeekDay,96,120,2);
}
status = HAL_ERROR;
if(rtcAlarm)
{
rtcAlarm = 0;
Buzzer_SET(GPIO_PIN_RESET); // 关闭蜂鸣器
GUI_DispStringAt("AlarmA",24,168); // 显示屏打印闹钟A标志
}
}
}
</syntaxhighlight>
==== gyu_util.c ====
请参照实验01中的介绍。
 
基础实验中的其他例程,大部分都是使用的相同的时钟配置函数,有特殊的时钟使用,将会在对应例程的源码详解中做针对性说明。
==== gyu_rtc.c ====
RTC时钟初始化函数,我们配置为24小时模式,禁止RTC输出,并且初始化RTC时钟。
 
<syntaxhighlight lang="c++" line="1" start="50">
void MX_RTC_Init(void)
{
hrtc.Instance = RTC; // 配置为RTC
hrtc.Init.HourFormat = RTC_HOURFORMAT_24; // 24小时模式
hrtc.Init.AsynchPrediv = 127; // 异步预分频器,固定127(0x7F)
hrtc.Init.SynchPrediv = 255; // 同步预分频器,固定255(0xFF)
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE; // 禁止RTC输出
// 初始化RTC时钟
if (HAL_RTC_Init(&hrtc) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
</syntaxhighlight>RTC初始化函数,使能RTC时钟。<syntaxhighlight lang="c++" line="1" start="73">
void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
if(hrtc->Instance==RTC)
{
__HAL_RCC_RTC_ENABLE(); // 使能RTC时钟
}
}
</syntaxhighlight>设置RTC时钟的函数,用于设置当前的时间(分别设置时分秒)。<syntaxhighlight lang="c++" line="1" start="91">
void RTC_TIME_Set(uint8_t hour, uint8_t min, uint8_t sec)
{
RTC_TimeTypeDef sTime; // RTC时间结构体
sTime.Hours = hour; // 时
sTime.Minutes = min; // 分
sTime.Seconds = sec; // 秒
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE; // 不定义AM和PM,因为24小时模式
sTime.StoreOperation = RTC_STOREOPERATION_RESET; //
// 设置时钟
if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
</syntaxhighlight>设置RTC日期、星期的函数,用于设置当前的年月日、以及星期。<syntaxhighlight lang="c++" line="1" start="119">
void RTC_DATE_Set(uint8_t week, uint8_t year, uint8_t monty, uint8_t date)
{
RTC_DateTypeDef sDate; // RTC日期结构体
sDate.WeekDay = week; // 星期
sDate.Year = year; // 年
sDate.Month = monty; // 月
sDate.Date = date; // 日
// 设置日期
if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
</syntaxhighlight>闹钟时间设置的函数,用于设置闹钟触发的时分秒。<syntaxhighlight lang="c++" line="1" start="147">
void RTC_AlarmA_Set(uint8_t hour, uint8_t min, uint8_t sec, uint8_t week)
{
RTC_AlarmTypeDef sAlarm; // RTC闹钟A结构体
sAlarm.AlarmTime.Hours = hour; // 时
sAlarm.AlarmTime.Minutes = min; // 分
sAlarm.AlarmTime.Seconds = sec; // 秒
sAlarm.AlarmTime.SubSeconds = 0; // 亚秒(这边没有作比较,任意设置)
sAlarm.AlarmMask = RTC_ALARMMASK_NONE; // 精确到时分秒、日期或者星期
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL; // 亚秒不作比较
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY; // 比较星期(设置星期或者日期)
sAlarm.AlarmDateWeekDay = week; // 星期
sAlarm.Alarm = RTC_ALARM_A; // 闹钟A
// 开启闹钟中断
if (HAL_RTC_SetAlarm_IT(&hrtc, &sAlarm, RTC_FORMAT_BIN) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 10, 0); // 抢占优先级10,子优先级0
HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn); // 使能中断
}
</syntaxhighlight>闹钟回调函数,当RTC实时时钟计时到达设置的闹钟时间,就会触发此回调,我们在回调中打开蜂鸣器,并且置位闹钟的标识。<syntaxhighlight lang="c++" line="1" start="181">
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
Buzzer_SET(GPIO_PIN_SET); // 蜂鸣器哔一声
rtcAlarm = 1;
}
</syntaxhighlight>
== 实验17-定时器中断 ==
=== STM32L476 定时器简介 ===
 
''<span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span>''
=== 硬件设计 ===
选择STM32L4内部RTC实时时钟。
 
''<span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span><span class="tlid-translation-gender-indicator translation-gender-indicator"></span>''
=== 实验准备 ===
# 使用miniUSB线及10pin排线,通过Jlink仿真器连接PC端和开发板。
510
个编辑