Recently, when debugging the system, I found that the microcontroller was always resetting, so I thought of using a program to determine where the reset signal of a microcontroller came from. I found that there are 9 reset sources for STM8 microcontrollers.
●External reset generated by NRST pin
● Power-On Reset (POR)
● Brown-out Reset (BOR)
● Independent watchdog reset
● Window watchdog reset
● Software reset
● SWIM reset
● Illegal opcode reset
● EMS reset: A reset that occurs when some critical registers are corrupted or incorrectly loaded
However, only 5 of these 9 reset sources can be seen through the reset register
Reset Status Register (RST_SR)
Bits 7:5 are reserved and must remain zero.
Bit 4 EMCF: EMC reset flag
Bit 3 SWIMF: SWIM reset flag
Bit 2 ILLOPF: Illegal opcode reset flag
Bit 1 IWDGF: Independent watchdog reset flag
Bit 0 WWDGF: Windowed watchdog reset flag
Since the MCU does not use a watchdog, these two watchdog resets are not used. The SWIM reset is used when burning programs or emulating. Then there is only one EMC reset and illegal opcode reset left to judge. But what I want to see more is whether the MCU is reset by power failure caused by unstable power supply or NRST reset caused by interference with the reset pin of the MCU. However, there is no flag bit in the register that can judge these two reset sources. So can I find a way to judge these two reset sources by myself?
First, let's analyze the difference between power-off reset and NRST reset. Power-off reset means that the microcontroller is completely powered off, and everything in the memory will be lost after the reset. The memory data is not lost during NRST reset, and the contents in the memory are still the data stored before the reset. So based on this feature, can we store a flag at a certain address in the memory? Read this flag after reset. If this flag is the same as the stored one, it means that the microcontroller is not powered off, and it is a NRST reset. If the read value is not the stored value, it means that the microcontroller is powered off, and the contents stored in the memory are lost, which is a power-off reset. In this way, we can distinguish whether it is a power-off reset or a NRST reset.
I wrote a program to test it, and the test results were in line with expectations.
The test code is as follows:
#include "iostm8s103F3.h"
#include "main.h"
#include "uart.h"
#include "stdio.h"
void SysClkInit( void )
{
CLK_SWR = 0xe1; //HSI is the main clock source 16MHz CPU clock frequency
CLK_CKDIVR = 0x00; //CPU clock divided by 0, system clock divided by 0
}
void main( void )
{
__asm( "sim" ); //Disable interrupts
SysClkInit();
Uart1_Heat( 9600 );
__asm( "rim" ); // Enable interrupt
//Reset source judgment
if( ( * ( u16* )( 0x000102 ) ) == 0x55 ) //Judge whether the value stored in the address 0x000100 is 0x55
{
printf( " key reset!rn" ); //The value is equal to 0x55, which means it is written by the program and belongs to key reset
}
else //If the value of this address is not equal to 0x55, it means that the microcontroller has just been powered on
{
printf( " power reset!rn" ); //It is a power-on reset and then writes the specified value to the specified address. If the microcontroller is not powered off, this value will remain unchanged
* ( u16* )( 0x000102 ) = 0x55;
}
if( RST_SR_WWDGF )
{
printf( "wwdg reset!rn" );
}
if( RST_SR_IWDGF )
{
printf( "iwdg reset!rn" );
}
if( RST_SR_ILLOPF )
{
printf( "illop reset!rn" );
}
if( RST_SR_SWIMF )
{
printf( "swim reset!rn" );
}
if( RST_SR_EMCF )
{
printf( "emcf reset!rn" );
}
//WWDG_CR = 0x80; //Start independent watchdog
// IWDG_KR = 0xCC; // Start window watchdog
while( 1 )
{
}
}
After each power-on, first determine whether the value of the address 0x000102 is 0x55. If not, it means that the microcontroller has just been powered on and the value in the memory is random. So change the value in the address 0x000102 to 0x55. If the value of this address is 0x55 after the next microcontroller reset, it means that the microcontroller is not powered off. It is a reset caused by the reset pin. The address 0x000102 must be guaranteed not to be used by the program, otherwise the value will be rewritten during the program execution, and the value read after the next microcontroller reset will not be 0x55.
For the other reset sources, just read the registers directly.
Previous article:STM8S——Clock control(CLK)
Next article:STM8S——Analog/digital converter (ADC)
- Popular Resources
- Popular amplifiers
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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- [EEWorld invites you to play disassembly] Anker Anker power bank 10000mAh 22.5W
- 【Silicon Labs Development Kit Review】+Detection of Light Sensing and Hall Sensing
- PCB package design of electronic devices
- 【RT-Thread Reading Notes】9. RT-Thread Study Chapters 14-16 Reading Notes
- Why are there Flash and EEPROM in microcontrollers?
- MCU solves RFID smart tag/smart card system design problems
- TI official website millimeter wave radar application demonstration
- Looking for a displacement sensor, impact sensor, acceleration sensor and GPS kit to assemble into a system
- Industrial Automation Equipment Recruitment
- C5000 ringtone keeps bugging