打开主菜单

谷雨文档中心 β

更改

NBDK-L4:基础实验教程

删除89字节2019年2月20日 (三) 10:06
无编辑摘要
</syntaxhighlight>
== 实验18-独立看门狗 ==
独立看门狗实验,这个实验大家可以配合窗口看门狗实验一起使用。独立看门狗实验,STM32L4内部自带两个看门狗,分别叫做“独立看门狗(IWDG)”以及“窗口看门狗(WWDG)”。这两个看门狗的使用,将分别在本例程以及下一个例程给大家讲解使用的方式。
=== 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>''以下部分为TIM2 / TIM3 / TIM4 / TIM5这四个定时器的介绍。 1.通用定时器简介: 通用定时器由一个由可编程预分频器驱动的16位或32位自动重载计数器组成。它们可用于各种目的,包括测量输入信号的脉冲长度(输入捕获)或生成输出波形(输出比较和PWM)。 使用定时器预分频器和RCC时钟控制器预分频器,可以将脉冲长度和波形周期从几微秒调制到几毫秒。定时器完全独立,不共享任何资源。 2.通用定时器功能 •16位(TIM3,TIM4)或32位(TIM2和TIM5)上,下,上/下自动重载计数器。 •16位可编程预分频器,用于分频(也“在运行中”)计数器时钟 频率由1到65535之间的任何因子组成。 •最多4个独立频道: - 输入捕获 - 输出比较 - PWM生成(边缘和中心对齐模式) - 单脉冲模式输出 •同步电路,用外部信号控制定时器并互连 几个计时器。 •以下事件的中断/ DMA生成: - 更新:计数器溢出/下溢,计数器初始化(通过软件或 内部/外部触发器) - 触发事件(计数器启动,停止,初始化或通过内部/外部触发计数) - 输入捕获 - 输出比较 •支持增量(正交)编码器和霍尔传感器电路进行定位 目的独立看门狗的作用:
•外部时钟或逐周期电流管理的触发输入独立看门狗外设检测并解决由于软件故障引起的故障,并在计数器达到给定超时值时触发系统复位。
3.通用定时器中断触发的条件也就是说当程序跑飞的时候,独立看门狗会复位系统。
配置定时器自动装载值,以及分频系数,使能定时器的中断。当计数值向上或者向下溢出时,定时器中断触发。独立看门狗的时钟:
计算公式为:Tout=(ARR+1)(PSC+1)/TIMxCLK (us)独立看门狗(IWDG)由其自己的专用低速时钟(LSI)提供时钟,因此即使主时钟发生故障也能保持活动状态。
也就是说独立看门狗完全是独立运行,不受其他部分程序影响。
''<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内部TIM2定时器,展示使用的是开发板上的LED指示灯D3。选择STM32L4内部独立看门狗,展示使用的是开发板上的LED指示灯D3、以及用于喂狗的按键引脚S2(BTN2 PC2引脚)。
[[文件:NBDK-SCH-LED.png|边框|居中|无框|386x386像素]]
[[文件:NBDK-SCH-BUTTON.png|边框|居中|无框|434x434像素]]
''<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端和开发板。
# 使用miniUSB线,连接PC与开发板USB接口。
# 使用Keil打开基础实验的实验17使用Keil打开基础实验的实验18-定时器中断。独立看门狗。
# 下载程序,并完成功能测试。
=== 实验验证 ===
下载完成后,可以看到LED周期闪烁(1s周期,也就是点亮1s,熄灭1s)。下载完成后,可以看到LED周期闪烁,这代表我们的STM32运行程序一直在被复位。当我们将按键S2(RIGHT)按键持续按下时,这个时候LED停止闪烁(保持常亮的状态),说明程序没有再被复位。
=== 源码详解 ===
此文件位于“实验16-RTC实时时钟\Inc”路径中,主要用途是选择使能此例程使用到的库文件。
此例程我们主要给大家展示STM32L4的定时器的中断功能,所以我们宏定义中打开TIM相关的。此例程我们主要给大家展示STM32L4的独立看门狗功能,所以我们宏定义中打开IWDG相关的。<syntaxhighlight lang="c" line="1" start="103">
// 使能的宏
#define HAL_MODULE_ENABLED // 芯片
#define HAL_UART_MODULE_ENABLED // UART
#define HAL_SPI_MODULE_ENABLED // SPI
#define HAL_TIM_MODULE_ENABLED HAL_IWDG_MODULE_ENABLED // TIM定时器独立看门狗
</syntaxhighlight>
main函数,我们的例程由此处开始执行,首先调用HAL_Init()函数初始化我们的模块,接着调用SystemClock_Config()函数初始化此例程用到的时钟,具体有哪些时钟被初始化,在gyu_util.c部分有详细说明。
接下来我们分别初始化了LED及TIM2定时器。接下来我们初始化了LED,并且初始化了IWDG配置、以及控制IWDG喂狗的S2按键引脚。 然后我们设置LED点亮,在LED点亮之前我们延时500ms,用来展示程序是否被独立看门狗重启。 在while()循环中,我们持续监测IWDG控制喂狗的按键引脚电平,如果为低电平(按键被按下),则调用喂狗的函数(喂狗之后,独立看门狗不会再复位程序)。
<syntaxhighlight lang="c++" line="1" start="32">
// 重置所有外设、flash界面以及系统时钟
HAL_Init();
// 配置系统时钟(包含振荡器、系统时钟、总线时钟等等)
SystemClock_Config();
// 初始化LED引脚 LED_Init();
// 初始化tim2定时器初始化独立看门狗 MX_TIM2_Init MX_IWDG_Init(); // 初始化独立看门狗用于触发喂狗的S2按键引脚 IWDG_FeedIO_Init(); // 延时500ms,展示软件是否重启(重启则LED闪烁,不重启则LED常亮) HAL_Delay(500); LED_SET(GPIO_PIN_SET);
//
while (1)
{
// 如果按键RIGHT按下,检测为低电平,则喂狗 if(!IWDG_FeedIO_Read()) { IWDG_Feed(); // 喂狗 }
}
}
基础实验中的其他例程,大部分都是使用的相同的时钟配置函数,有特殊的时钟使用,将会在对应例程的源码详解中做针对性说明。
==== gyu_tim2itgyu_iwdg.c ====TIM2定时器初始化函数,我们配置TIM2时钟不分频(也就是系统时钟80MHZ),设置TIM2预分频为10000-1(也就是TIM2时钟为80M看门狗初始化函数,我们配置IWDG 32分频(也就是32KHz /10000 32 = 8000Hz),设置自动装载值为8000(也就是说一次装载满的时间为80001KHz = 1us),配置重载计数值为1000(也就是装载满一次需要1000*(1/8000Hz) 1us = 1s)。
这边有总结好的公式:Tout或者大家使用公式计算:Tout=(ARR+14*(2^prer)(PSC+1*rlr)/TIMxCLK 32 (usms)。ARR代表预分频,PSC代表自动装载值。。此例程prer = 3,rlr = 1000。
<syntaxhighlight lang="c++" line="1" start="4737">void MX_TIM2_InitMX_IWDG_Init(void)
{
htim2hiwdg.Instance = TIM2IWDG; // 配置为TIM2配置为IWDG htim2hiwdg.Init.Prescaler = 10000-1IWDG_PRESCALER_32; // ARR+1 32分频,prer = 100003 htim2hiwdg.Init.CounterMode Window = TIM_COUNTERMODE_UPIWDG_WINDOW_DISABLE; // 向上计数 htim2hiwdg.Init.Period Reload = 8000-11000; // PSC+1 重载计数1000,rlr = 8000 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // 不分频,80MHz // 初始化TIM2,出错则进入错误处理函数1000 if (HAL_TIM_Base_InitHAL_IWDG_Init(&htim2hiwdg) != HAL_OK) // 初始化IWDG,出错则进入错误处理函数
{
_Error_Handler(__FILE__, __LINE__);
}
HAL_TIM_Base_Start_IT(&htim2); // 开启TIM2,并且使能中断
}
</syntaxhighlight>TIM2初始化函数,使能定时器TIM2时钟,并且使能中断。独立看门狗喂狗函数,也就是清除计数值。<syntaxhighlight lang="c++" line="1" start="7257">void HAL_TIM_Base_MspInitIWDG_Feed(TIM_HandleTypeDef *htimvoid)
{
ifHAL_IWDG_Refresh(htim->Instance == TIM2) { __HAL_RCC_TIM2_CLK_ENABLE(); //使能定时器TIM2 HAL_NVIC_SetPriority(TIM2_IRQn, 10, 0); //设置中断优先级10,子优先级0 HAL_NVIC_EnableIRQ(TIM2_IRQn&hiwdg); //使能ITM3中断 }喂狗
}
</syntaxhighlight>定时器中断回调函数,当计数值溢出时,触发此回调,我们判断是否是TIM2触发,如果是,则翻转LED的状态。初始化按键S2(PC2引脚)为一般上拉输入。<syntaxhighlight lang="c++" line="1" start="72">void IWDG_FeedIO_Init(void){ GPIO_InitTypeDef GPIO_InitStructure; // 定义引脚参数结构体
在此实验中的表现是,LED灯1s改变一次状态(闪烁)。 __HAL_RCC_GPIOC_CLK_ENABLE(); // 使能GPIOC时钟 GPIO_InitStructure.Pin= GPIO_PIN_2; // 引脚编号为2 GPIO_InitStructure.Mode = GPIO_MODE_INPUT; // 输入 GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW; // 低频率 GPIO_InitStructure.Pull = GPIO_PULLUP; // 上拉 HAL_GPIO_Init(GPIOC, &GPIO_InitStructure); // 初始化PC2}</syntaxhighlight>检测当前S2引脚电平,并且返回当前电平状态。<syntaxhighlight lang="c++" line="1" start="9094">uint8_t IWDG_FeedIO_Read(void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim == (&htim2)) { LED_TOGGLEreturn HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_2); // 翻转LED状态,1s翻转一次 }
}
</syntaxhighlight>
510
个编辑