14.4.2 Window watchdog experiment
Function: As soon as the program is run, LED1 connected to PB5 lights up for 300ms and then turns off, entering an infinite loop. Wait for the WWDG interrupt to arrive. In the interrupt, feed the dog and flip LED2 on PE5. You can see that LED2 flashes continuously, and LED1 only flashes once when it is first started.
(1) Add the following code in the function list area of the wdg.h file in the previous experiment.
void WWDG_Init( u8 tr, u8 wr, u8 fprer ); //Window watchdog initialization
(2) Add the following code at the end of the wdg.c file of the previous experiment.
/****************************************************** **
Name:WWDG_IRQHandler
Function: Window watchdog interrupt service routine
Paramater:None
Return :None
*************************************************** */
void WWDG_IRQHandler()
{
WWDG->CR = 0x7F; //Reset the 7-bit counter
WWDG->SR = 0x00; //Clear the early wake-up interrupt flag bit
LED2 != LED2 ;
}
/****************************************************** **
Name:WWDG_Init
Function: Window watchdog initialization
Paramater:
tr: counter value
wr: window value
fprer: frequency division coefficient
Return :None
*************************************************** */
void WWDG_Init( u8 tr, u8 wr, u8 fprer )
{
RCC->APB1ENR |= 1<<11; //Enable wwdg clock
WWDG->CFR |= fprer<<7; //PCLK1/4096 divided by 2^fprer
WWDG->CFR &= 0xFF80;
WWDG->CFR |= wr; //Set window value
WWDG->CR |= tr&0x7F; //Set counter value
WWDG->CR |= 1<<7; //Enable watchdog
NVIC_Init(2, 3, WWDG_IRQn, 2); //Preemption 2, sub-priority 3, group 2
WWDG->SR = 0x00; //Clear the early wake-up interrupt flag bit
WWDG->CFR |= 1<<9; //Enable early wake-up interrupt
}
Note: Since LED2 is referenced in the interrupt service function, the header file #include "led.h" needs to be added.
(3) Create the led.h file and enter the following code.
#ifndef _LED_H_
#define _LED_H_
#include "sys.h"
/****************************************************** *************************************************** ******
Hardware port
*************************************************** *************************************************** *****/
#define LED1 PBout(5) //Define LED1 port
#define LED2 PEout(5) //Define LED2 port
/****************************************************** *************************************************** ******
function list
*************************************************** *************************************************** *****/
void LED_Init( void ); //LED initialization
#endif
(4) Create the led.c file and enter the following code.
#include "led.h"
/****************************************************** **
Name:LED_Init
Function:LED initialization
Paramater:None
Return :None
*************************************************** */
void LED_Init()
{
RCC->APB2ENR |= 1<<3;
GPIOB->CRL &= 0xFF0FFFFF;
GPIOB->CRL |= 0x00300000;
RCC->APB2ENR |= 1<<6;
GPIOE->CRL &= 0xFF0FFFFF;
GPIOE->CRL |= 0x00300000;
LED1 = 1;
LED2 = 1;
}
(5) Enter the following code in file 1.c.
#include "sys.h"
#include "delay.h"
#include "usart1.h"
#include "led.h"
#include "wdg.h"
/****************************************************** **
Name: main
Function: main function
Parameter:None
Return :None
*************************************************** */
int main()
{
STM32_Clock_Init( 9 ); //STM32 clock initialization
SysTick_Init( 72 ); //SysTick initialization
USART1_Init( 72, 115200 ); //Initialize serial port 1 baud rate 115200
LED_Init(); //LED initialization
LED1 = 0; //Light up DS0
delay_ms(300); //Delay 300ms so that people can see the status of DS0 on
WWDG_Init(0x7F, 0x5F, 3); //The counter value is 7f, the window register is 5f, and the frequency division number is 8
while(1)
{
LED1 = 1; //Turn off LED1
}
}
14.5 Why does STM32 have both window watchdog and independent watchdog?
14.5.1 Conditions for using independent watchdog
(1) The program runs away
(2) An infinite loop occurs
(3) Sleep and hibernation are unreasonable
(4) The external main crystal oscillator is damaged
(5) Need to be reset again and no data will be retained.
14.5.2 Window watchdog usage conditions
(1) There is an error in the software logic
(2) Crash or infinite loop
(3) Software execution does not perform as expected
(4) The software needs to be reset again, but all data will be retained
14.5.3 The difference between the two
(1) The independent watchdog uses an internal dedicated 40kHz low-speed clock
The window watchdog uses the clock of PCLK1
(2) The independent watchdog has no interruption and is directly reset when timeout occurs.
The window watchdog has an interrupt. The timeout can be operated in the interrupt service function or fed to the dog.
(3) Independent watchdogs are generally used to avoid program runaways or infinite loops
The window watchdog is to prevent the program from not executing according to the preset logic.
(4) The independent watchdog is a 12-bit decrement operation
The window watchdog is a 6-bit decrement operation
Previous article:STM32 introductory study notes EEPROM storage experiment 4
Next article:STM32 introductory study notes: capacitive touch experiment (Part 1)
Recommended ReadingLatest update time:2024-11-15 07:20
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- Two new TI boards
- How to observe the PWM output of DSPF2812 through graph in CCS?
- Analysis of the three most commonly used communication protocols in single-chip microcomputer systems
- MSP-EXP430F5529LP Development Board 001-GPIO
- Chip type and model
- Several modes of Bluetooth devices
- Acknowledgements | EEWorld’s 15th anniversary, thank you!
- How to set subdivision of stepper motor driver
- LDO and DC-DC Simple Comparison and Selection
- Answer the questions to win prizes! ADI Application Tour - Industrial Large Machine Health Edition