STM32 external interrupt and IWDG

Publisher:科技创客Latest update time:2018-09-05 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Experimental platform: Zhengdian Atomic Battleship Development Board

System: Windows 7 64-bit

Compiler: MDK 4.72a

Reference routines: ATK serial port routines, Wildfire ISO serial port routines


External interrupt


There are two main types of interrupts:

1. System abnormal interrupts can also be called kernel abnormalities. Some of these abnormal interrupts cannot be controlled, such as RESET, NMI, and FAULT.

2. External interrupts. These interrupts can be set. Each GPIO can be set as an interrupt.

1. NVIC controller

When you want to use interrupts, you must set up the NVIC

void  NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct);

 

 

NVIC_IRQChannel The interrupt line that needs to be configured

NVIC_IRQChannelCmd enables or disables the interrupt response of the corresponding interrupt vector 

NVIC_IRQChannelPreemptionPriority configures the corresponding interrupt vector preemption priority 

NVIC_IRQChannelSubPriority configures the response priority of the corresponding interrupt vector 

 

Preemption > Response, first determine the preemption and then determine the response.

 

To configure these priority groups, you can use the library function NVIC_PriorityGroupConfig(), which can be entered as

The input parameters are NVIC_PriorityGroup_0 ~ NVIC_PriorityGroup_4, and there are 5 allocation groups.

The relation is: 

 

Group 0: 16 types of tackles

Group 1: 2 steals, 8 responses

Group 2: 4 steals, 4 responses

Group 3: 8 tackles, 2 responses

Group 4: 16 responses, no steals

 

The smaller the number, the higher the priority. This priority group should be set only when many interrupts are to be used. Otherwise, it does not need to be set.

2. EXIT

STM32 has a total of 16 interrupt lines, numbered 0 to 15. The 0th pin of GPIOA to GPIOG belongs to interrupt line 0, all 1st pins belong to interrupt line 1, and so on. However, each interrupt line can only respond to the interrupt of one pin at a time, but it can be time-divided and multiplexed to set different trigger modes.

 

void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct); //initialization structure function

 

typedef struct

{

  uint32_t EXTI_Line; //Interrupt line number EXTI_LineX;   

  EXTIMode_TypeDef EXTI_Mode; //Mode selection interrupt or event

  EXTITrigger_TypeDef EXTI_Trigger; //Trigger mode

  FunctionalState EXTI_LineCmd; //Enable command

                                 

} EXIT_InitTypeDef;

 

 

After filling in these two structures and initializing them, you can use external interrupts. But we also need an interrupt function

Remember to enable the pin's multiplexing function AFIO

The interrupt function has a fixed function name and cannot be changed. We can see the interrupt function from the startup file.

The general format of the external key interrupt function is:

 

void EXTI2_IRQHandler(void) 

if( EXTI_GetITStatus(EXTI_Line2 ) != RESET) //Judge whether an interrupt occurs on a certain line        

        { 

          Interrupt logic... 

          EXTI_ClearITPendingBit( EXTI_Line2 ); // Clear the interrupt flag on LINE     

        }                 

IWDG

The function of the watchdog is to prevent the program from running away or crashing. When IWDG_KR is not written to 0XAAAA regularly, a forced reset signal will be generated to restart the chip. However, IWDG uses an internal RC clock with an unstable frequency, so the watchdog should be fed in advance to prevent an unexpected reset.

void IWDG_WriteAccessCmd(uint16_t IWDG_WriteAccess);

Turn write protection on and off for IWDG

void IWDG_SetPrescaler(uint8_t IWDG_Prescaler);

Frequency division ratio 2^n (n= 2 ~ 16) Maximum 256

void IWDG_SetReload(uint16_t Reload);

Set the maximum reload value to 4095

void IWDG_ReloadCounter(void);

Reload the number of IWDG_RLR register

void IWDG_Enable(void);

Enabling IWDG

FlagStatus IWDG_GetFlagStatus(uint16_t IWDG_FLAG);

Check whether pre-scaling or reloading is in progress

The time is calculated as: Tout=((4×2^prer) ×rlr) /40.

 

 

Part of the code:

void IWDG_Init()

{

IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);

IWDG_SetPrescaler(4);

IWDG_SetReload(625);

IWDG_ReloadCounter();

IWDG_Enable();

}

 

 

void EXITX_Init()

{

EXIT_InitTypeDef EXIT_InitStruct;

NVIC_InitTypeDef NVIC_InitStruct;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

GPIO_EXTILineConfig(GPIO_PortSourceGPIOE,GPIO_PinSource2);

EXIT_InitStruct.EXTI_Line = EXTI_Line2;

EXIT_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;

EXIT_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;

EXIT_InitStruct.EXTI_LineCmd = ENABLE;

NVIC_InitStruct.NVIC_IRQChannel = EXTI2_IRQn;

NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;

NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;

NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

EXIT_Init(&EXIT_InitStruct);

NVIC_Init(&NVIC_InitStruct);

}

 

void EXTI2_IRQHandler()

{

delay_ms(10);

if(KEY2 == 0)

{

IWDG_ReloadCounter();

}

EXTI_ClearITPendingBit(EXTI_Line2);

}


Keywords:STM32 Reference address:STM32 external interrupt and IWDG

Previous article:STM32 IWDG usage
Next article:stm32 independent watchdog IWDG

Recommended ReadingLatest update time:2024-11-15 15:16

A first look at the USB peripherals of STM32
These days I have been considering whether to use USB or Ethernet for the project, but the platform we are using, STM32F103ZET6, does not seem to have the Enternet peripheral, so let's consider USB first. One of the major features of USB peripherals is plug-and-play, which is possible because the USB protocol stipulat
[Microcontroller]
STM32 study notes: basic timing functions of general timers
1. Introduction to STM32 Timer There are a total of 11 timers in STM32, including 2 advanced control timers, 4 ordinary timers and 2 basic timers, as well as 2 watchdog timers and 1 system tick clock. Today we mainly learn about 8 timers.  Timer TIM1 and TIM8 are advanced timers that can generate 3 pairs of PWM com
[Microcontroller]
STM32 study notes: basic timing functions of general timers
Steps to create an STM32 project in IAR (register version)
First of all, it is based on register programming. 1. Download the STM32 firmware library from the official website. I used 3.5 2. Create an STM32 folder (not for storing projects) to store the basic files required to create STM32 projects. When you create a project again in the future, just copy the contents of thi
[Microcontroller]
Summary of STM32 program transplantation skills
1. The project replaces different STM32 chips eg: stm32f103rct6 ---- stm32f103c8t6: 1.1. Modify the chip Click the magic wand, and in the menu bar that appears, select the chip in the Device option. 1.2. Modify the startup file Here is an example of changing RCT6 to C8T6. Because the flash capacity is different
[Microcontroller]
Summary of STM32 program transplantation skills
STM32 IO configuration and mode
When configuring the IO of STM32, you must first turn on the clock, that is, RCC. You can configure registers through the library or directly control the registers. There are several registers, but basically there are a few commonly used ones, GPIOx->CRL, GPIO->CRH, GPIOx->IDR, GPIOx->ODR. CRL configures the lower 8-b
[Microcontroller]
Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep (STOP) mode
By using the sleep mode of the stm32 microcontroller, the microcontroller can be put into sleep intermittently to achieve low power consumption. It often fails to wake up after entering sleep mode, resulting in the next program not being able to be burned in. The usual solution is: the general development board or mic
[Microcontroller]
Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep (STOP) mode
Summary of problems encountered in debugging STM32 time
When adding a new time in the program, the register cannot be written during initialization. The reason is that the relevant initialization is not performed and a register is not enabled.   The procedure is as follows: void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) {   //GPIO_InitTypeDef GPIO_InitStruct;     
[Microcontroller]
STMicroelectronics mass-produces STM32 F0 series entry-level MCUs
STMicroelectronics (ST) announced at a media conference in Beijing on May 15 that it has started mass production of the entry-level STM32 F0 series 32-bit MCU based on the ARM Cortex-M0 processor core. It is suitable for applications such as industrial controllers, home automation, printers and white appliances.   The
[Analog Electronics]
STMicroelectronics mass-produces STM32 F0 series entry-level MCUs
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号