STM32 independent watchdog

Publisher:大树下的大白菜yLatest update time:2015-08-21 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Experimental phenomena:

At first, LED1 is on and LED2 is off. If KEY1 is pressed at regular intervals, LED2 will flash continuously because the independent watchdog resets the system. If KEY1 is pressed intermittently, LED2 will not flash, indicating that it has not been reset.

 

experiment platform:

Color screen development board based on STM32F103C8T6

 

Steps:

1) Write 0X5555 to IWDG_KR.
Through this step, we cancel the write protection of IWDG_PR and IWDG_RLR, so that these two registers can be operated later.
Set the values ​​of IWDG_PR and IWDG_RLR.
These two steps set the watchdog frequency division coefficient and the reload value. From this, we can know the feeding time of the watchdog (that is
, the watchdog overflow time), which is calculated as follows:
Tout=((4×2^prer) ×rlr)/40
, where Tout is the watchdog overflow time (in ms); prer is the watchdog clock prescaler value (IWDG_PR value),
ranging from 0 to 7; rlr is the watchdog reload value (IWDG_RLR value);
for example, if we set the prer value to 4 and the rlr value to 625, we can get Tout=64×625/40=1000ms, so
the watchdog overflow time is 1s. As long as you write 0XAAAA to IWDG_KR once within one second, the watchdog will not be reset (of course, writing multiple times is also possible). Here we need to remind everyone that the watchdog clock is not an accurate
40Khz, so it is best not to feed the dog too late, otherwise, the watchdog may be reset.

2) Write 0XAAAA to IWDG_KR.
This command will cause STM32 to reload the value of IWDG_RLR into the watchdog counter. This command can also be used
to feed the watchdog.
3) Write 0XCCCC to IWDG_KR.
This command starts the STM32 watchdog. Note that once IWDG is enabled, it cannot be turned off! If you want to turn it off
, you can only restart it, and you cannot turn on IWDG after restarting, otherwise the problem will remain. So here I remind you that if you don’t
use IWDG, don’t turn it on to avoid trouble.
Through the above 3 steps, we can start the STM32 watchdog. After enabling the watchdog,
you must feed the watchdog at intervals in the program, otherwise it will cause the program to reset. Using this, we use an LED light to indicate
whether the program is restarted to verify the independent watchdog of STM32.

Part of the program code:

     watchdong.h

#ifndef WATCHDOG_H
#define WATCHDOG_H
void WatchDog_Init(u8, u16);//Declare the watchdog initialization function
void WatchDog_Feed(void);//Declare the dog feeding function
#endif

    watchdong.c

#include "common.h"
#include "watchdog.h"
//Watchdog initialization, parameters: prer-frequency division, reld-counter reload value
void WatchDog_Init(u8 prer, u16 reld)
{
   IWDG->KR=0x5555; //Allow access to PR and RLR registers
   IWDG->PR=prer;   //Set frequency division
   IWDG->RLR=reld; //Set counter initial value
   IWDG->KR=0xaaaa;  //Initial initial value
   IWDG->KR=0xcccc;   //Start watchdog timer
}

//Feed the dog
void WatchDog_Feed(void)
{
 IWDG->KR=0xaaaa;
}
This code has two functions. void IWDG_Init(u8 prer, u16 rlr) is the independent watchdog initialization function, which is to
initialize the independent watchdog according to the steps described above. This function has two parameters, which are used to set the pre-scaling number and the reload
register value. Through these two parameters, you can roughly know the time period of the watchdog reset. The calculation method
is described in detail above, so I won’t say more here.
void IWDG_Feed(void) function, this function is used to feed the dog, because the STM32 dog feeding only needs to write
0XAAAA to the key value register,

    Main function
#include
#include"common.h"
#include"led.h"
#include"key.h"
#include"watchdog.h"
int main(void)
{
 u8 temp2;
 Stm32_Clock_Init(9);    //System clock setting
 delay_init(72);     //Delay initialization
 LED_Init();     //LED initialization
 KEY_Init();
 WatchDog_Init(4,625);  //Watchdog initialization
 LED1=1;
 LED2=0;
 while(1)
 {
   temp2=KEY_Scan(); //Read key value
   if(temp2==1)
    WatchDog_Feed();  //Feed the dog
  
 }
}

Keywords:STM32 Reference address:STM32 independent watchdog

Previous article:STM32 UART4 UART5
Next article:STM32 recognition rate of 1.8V peripheral devices (without level conversion)

Recommended ReadingLatest update time:2024-11-16 20:56

STM32 GPIO input and output mode configuration
When I was reading the data sheet recently, I found that there are 8 types of configurations for the STM32 GPIO input and output modes (4 for input and 4 for output): (1) GPIO_Mode_AIN analog input (2) GPIO_Mode_IN_FLOATING floating input (3) GPIO_Mode_IPD pull-down input (4) GPIO_Mode_IPU pull-up input (5) GPIO_M
[Microcontroller]
STM32 Memory - About STM32 Memory
When we complete an experiment and feel proud of it, we can't help but have an uneasy thought: have we understood a little bit of the details involved? Especially, when all our things depend on the compilation environment or firmware, or other people's programs, and we just copy and modify them, and even use them. Whe
[Microcontroller]
STM32 Memory - About STM32 Memory
Design of a portable carbon dioxide monitor based on STM32
    The detection methods of CO2 concentration can be roughly divided into chemical methods and physical methods. The detection methods of CO2 concentration include titration, thermal catalysis, gas sensing, and electrochemical methods. These methods are chemical methods, which generally have problems such as high pric
[Medical Electronics]
Design of a portable carbon dioxide monitor based on STM32
STM32 general T2, T3, T4, T5 timer detailed explanation
Next timer initialization configuration  1 void TIM3_Configuration(void)//1MS  2 {  3     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;  4           5 TIM_DeInit(TIM3); //Turn on the clock and then off  6 /* Set TIM2CLK to 72MHZ */  7     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3 , ENABLE);  8       9         10
[Microcontroller]
Simulated serial port based on STM32 series (non-blocking)
STM32 microcontrollers generally have at least 3 serial ports and at most 5 serial ports, but for my project, 5 hardware serial ports are not enough. As for the reason why it is not enough, well, it is because the project is customized later, and any serial port cannot be saved. There is no way, I can only think of
[Microcontroller]
Simulated serial port based on STM32 series (non-blocking)
STM32 learning: the difference between NOR flash and NAND flash, the difference between RAM and ROM
ROM and RAM both refer to semiconductor memory. ROM is the abbreviation of Read Only Memory, and RAM is the abbreviation of Random Access Memory. ROM can still retain data when the system stops powering on, while RAM usually loses data after power failure. A typical RAM is the computer's memory.   There are two major
[Microcontroller]
STM32 Learning: Context-M3 Introduction
Cortex-M3 Overview (1 Introduction      The Cortex-M3 is a 32-bit processor core. The internal data path is 32-bit, the registers are 32-bit, and the memory interface is also 32-bit. The CM3 uses a Harvard architecture with independent instruction and data buses, which allows instruction fetching and data access to ru
[Microcontroller]
Solar-LED Street Light Solution Based on STM32 MCU
As fossil energy is decreasing and the problem of global warming caused by excessive greenhouse gas emissions is becoming more and more important, people are actively developing various types of renewable energy on the one hand, and advocating green environmental protection technologies for energy conservation and e
[Microcontroller]
Solar-LED Street Light Solution Based on STM32 MCU
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号