STM32 study notes (11)

STM32F103ZET6 window watchdog experiment



Preface

The learning of STM32 can be divided into 3 versions.
1. Register version
2. Library function version
3. HAL library version
Due to personal reasons, I choose the library function version to learn STM32.


Tip: Problems such as software installation will not be explained! ! !

1. Overview of Window Watchdog

1. Concept

Insert picture description here
Insert picture description here
Insert picture description here

2. Principle

Insert picture description here
Insert picture description here
Insert picture description here

2. Related registers

1. Control register (WWDG_CR)

Insert picture description here

2. Configuration register (WWDG_CFR)

Insert picture description here

3. Status register (WWDG_SR)

Insert picture description here

Three, operation steps

1. Window watchdog timeout calculation

Insert picture description here

2. Operation steps

Insert picture description here

Fourth, the program source code

1.wwdg.h

code show as below:

#ifndef __WWDG_H
#define __WWDG_H

#include "sys.h"

void WWDG_Init(u8 tr,u8 wr,u32 fprer );//初始化WWDG
void WWDG_Set_Counter(u8 cnt);//设置WWDG的计数器
void WWDG_NVIC_Init(void);
#endif

2.wwdg.c

code show as below:

#include "wwdg.h"
#include "led.h"

//保存WWDG计数器的设置值,默认为最大. 
u8 WWDG_CNT=0x7f; 

void WWDG_Init(u8 tr,u8 wr,u32 fprer)//tr:计数器值  wr:窗口值 fprer:分频系数
{
    
    
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);//使能看门狗时
	WWDG_CNT=tr&WWDG_CNT;//取tr的0-6位
	WWDG_SetPrescaler(fprer);//设置分频系数
	WWDG_SetWindowValue(wr);//设置上窗口值
	WWDG_Enable(WWDG_CNT);//使能看门狗
	WWDG_ClearFlag();//清除提前唤醒中断标志位
	WWDG_NVIC_Init();//分组
	WWDG_EnableIT();//开启提前唤醒中断
}
void WWDG_NVIC_Init(void)//中断优先级设置
{
    
    
	NVIC_InitTypeDef NVIC_Initstr;
	NVIC_Initstr.NVIC_IRQChannel=WWDG_IRQn;
	NVIC_Initstr.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Initstr.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_Initstr.NVIC_IRQChannelSubPriority=3;
	NVIC_Init(&NVIC_Initstr);
}
void WWDG_Set_Counter(u8 cnt)//喂狗
{
    
    
	WWDG_Enable(cnt);
}
void WWDG_IRQHandler(void)//中断服务函数
{
    
    
	WWDG_Set_Counter(WWDG_CNT);//喂狗
	WWDG_ClearFlag();
	LED1=!LED1;
}

3.main.c

code show as below:

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "beep.h"
#include "key.h"
#include "usart.h"
#include "exti.h"
#include "iwdg.h"
#include "wwdg.h"

int main(void)
{
    
    
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init();
    LED_Init();
	Beep_Init();
	KEY_Init();
	uart_init(115200);
	EXTIX_Init();
	LED2=0;
	delay_ms(300);
	WWDG_Init(0x7F,0x5F,WWDG_Prescaler_8);
	while(1)
	{
    
    
		LED2=1;
	}		
}

5. Experimental results

When there is no dog feeding operation in the interrupt service function, LED1 will flip repeatedly. LED2 turns on after power on, turns off after 300ms, and then turns on again soon, turns off after 300ms, repeatedly
When there is a dog feeding operation in the interrupt service function, LED1 turns over repeatedly. LED2 lights up after power-on and goes out after 300ms.


to sum up

Watchdog is over! ! !

Guess you like

Origin blog.csdn.net/weixin_44935259/article/details/112788352