STM32 introductory study notes: watchdog experiment (Part 2)

Publisher:DreamySunsetLatest update time:2024-03-25 Source: elecfansKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Keywords:STM32 Reference address:STM32 introductory study notes: watchdog experiment (Part 2)

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

STM32-Touch Screen Experiment
void Touch_Init(void);//initialization u8 Read_ADS(u16 *x,u16 *y); //Bidirectional read with discard u8 Read_ADS2(u16 *x,u16 *y); //Bidirectional coordinate read with enhanced filtering u16 ADS_Read_XY(u8 xy); //Coordinate read with filtering (single direction) u16 ADS_Read_AD(u8 CMD); //Read AD conversion value v
[Microcontroller]
STM32 IO operation (based on library functions)
// stm32 study notes For the library functions of the stm32f10x series chips, the GPIO operation functions are declared in stm32f10x_gpio.h and defined in stm32f10x_gpio.c. The IO port operation steps of stm32 based on library functions: 1. Enable the IO port clock, Function interface: void RCC_APB2PeriphClockCmd(uint
[Microcontroller]
stm32 GPIO speed mode understanding
1. Input/output mode (refer to stm32 manual) 2. The difference between several speeds in GPIO output mode: (1). GPIO pin speed: GPIO_Speed_2MHz (10MHz, 50MHz);     Also known as the response speed of the output drive circuit: (The chip has multiple output drive circuits with different response speeds arranged in t
[Microcontroller]
Analysis of bit-band operation of STM32
The 8051 microcontroller can directly read and write a certain IO bit, and the Cortex-M3's bit-banding operation is an enhanced version of the 8051 bit addressing area. After using the bit-banding operation, you can use ordinary load/store instructions to read and write a single bit. 1. Related concepts Bit-band regio
[Microcontroller]
Analysis of bit-band operation of STM32
What do AHB and APB mean in ARM chip stm32
AHB is the abbreviation of Advanced High performance Bus, which is a "system bus". AHB is mainly used for the connection between high-performance modules (such as CPU, DMA and DSP, etc.). The AHB system consists of three parts: master module, slave module and infrastructure. The transmission on the entire AHB bus is
[Microcontroller]
stm32_SPI basic function
void SPI_RCC_config(void) {     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);     RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE); } void SPI_GPIO_config(void) {   GPIO_InitTypeDef GPIO_InitStructure;;  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//SCK  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;  GPIO_InitSt
[Microcontroller]
STM32 study notes 1 IO port learning
The IO port of STM32 can be configured into 8 modes by software: 1. Input floating 2. Input pull-up 3. Input drop-down 4. Analog Input 5. Open drain output 6. Push-pull output 7. Push-pull multiplexing function 8. Open-drain multiplexing function Each IO port can be freely programmed, and the registers of a single IO
[Microcontroller]
Engineer's STM32 MCU Learning Basic Notes (4): Using PWM to implement firefly lights (I)
  Using PWM to realize firefly lamp   Last time I mentioned that I would use the PWM function of the Timer to implement a firefly light. Of course, I still need to find an existing example to modify, and the example I will use this time is here.      Copy a copy to your own practice folder and create a proj
[Analog Electronics]
Engineer's STM32 MCU Learning Basic Notes (4): Using PWM to implement firefly lights (I)
Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号