The development board I use here is the MiniSTM32 from Zhengdian Atom, and the chip model is STM32F103RCT6. This is easy, so you can do that very easily!
***REMENBER STM32 is you! :)
We will give a popular explanation here. For detailed content, you can refer to the STM32 manual later.
Here we first introduce the concept of interruption. An example of interruption is that I am eating now, and someone knocks on the door. Then I have to put down my bowl and chopsticks to open the door, and then come back to continue eating. Why do we need interruption? Because we definitely don't want to always check if there is anyone at the door when we are eating, which will take up my eating time! This is of great significance in the program!
So we think what to do if there are many interruptions? For example, when I am eating, someone knocks on the door, but the water is turned on again. These two things happen almost at the same time. What should I do instead? At this time, the concept of priority comes into play. I will do the most important thing first!
Here we use the example of switch controlling LED to illustrate some usage of the three contents of GPIO EXTI NVIC!
The LED light interface we use is PA8, and the switch we use is PC5, which is the pull-up mode.
The example here is that the switch uses the EXTI interrupt to control the light on and off (note that our code here is continuous):
//@Time:2018/2/16
//@author:junwencui
//@wether: sun
//@location: yunnan
void key_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //It is better to enable multiplexing here
The above is the beginning of the configuration function. First, we define the structure, and then we need to turn on the clock. It should be noted that in KEIL, turning on the clock needs to be placed after the definition of the structure, otherwise an error will be reported!
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Pull-up input mode is used here
GPIO_Init(GPIOC,&GPIO_InitStructure); //Initialize structure definition
The mode GPIO_Mode_IPU here is used when using pull-up input, of course you can also use
GPIO_Mode_IPD Pull-down input
The difference here is your circuit hardware!
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource5); //map EXTI to PC5 here
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
Here, because PC5 is defined, the corresponding EXTI_Line5 is simply used in interrupt mode. Because PC5 is connected to a pull-up resistor, it is of course triggered when the level drops! Use EXTI_Trigger_Falling
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; //Here, specify the external interrupt as EXTI5~9
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //Set the preemption priority to 2
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0; //Set the response priority to 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // Enable NVIC
NVIC_Init(&NVIC_InitStructure); //Initialize the structure
}
The external interrupt here uses EXTI_Line5, but the stm32 library function defines interrupt lines 5 to 9, all using EXTI9_5_IRQn. This is actually not a big deal, because we still have to make judgments inside the interrupt function later! The priority of interrupts follows the rule: first look at the preemption priority, then look at the response priority. When the preemption priorities are the same and they occur at the same time, look at the response priority. The smaller the value, the higher the level! So when there are multiple interrupts, we need to carefully arrange these priorities.
Of course we can also define our priority group, example: NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//The preemption priority can be 0~3, and the response priority can be 0~3;
This is the priority group selection range, you must not exceed the range!
The above is the content of EXTI interrupt configuration! Next, let's look at the interrupt function:
static u8 led_red_flag =0; //This is the flag bit
void EXTI9_5_IRQHandler(void){
delay_ms(8); //debounce
if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) && !led_red_flag){ //Read the status of Pin_5
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
led_red_flag =1;
}else if(!GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5) && led_red_flag){
GPIO_SetBits(GPIOA,GPIO_Pin_8);
led_red_flag =0;
}
EXTI_ClearITPendingBit(EXTI_Line5);
}
The interrupt function here is fixed. We can check it in stm32f10x_it.h. From the name, EXTI9_5_IRQHandler means interrupt line 5~9, which is consistent with the above content! The debounce here is a common operation to prevent interference! In the interrupt function, there will be a statement to clear the interrupt flag bit, which is EXTI_ClearITPendingBit(EXTI_Line5);
This is crucial and will never be missed!
void led_Congfig(void){
GPIO_InitTypeDef GPIO_InitStructure; //PA8
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Use push-pull output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
}
Here is the function to configure the LED, using push-pull output and a working speed of 50Mhz!
int main(){
led_Congfig();
key_Config();
while(1){
}
return 0;
}
Here is our main function! Note that while(1{} here cannot be omitted, otherwise the program will exit directly without interruption!!!
Previous article:STM32 beginner's LED button interrupt
Next article:STM32 Practice 3. Timer controls LED flashing (Timer 1)
- 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
- The difference between polymer tantalum capacitors and ordinary tantalum capacitors
- MSP430FR2355 LaunchPad Development Kit
- PlainDAQ data logger based on Raspberry Pi Pico
- EEWORLD University----Live Replay: Microchip Security Series 21 - Secure Boot and Message Authentication for CAN FD in ADAS and IVI Systems Using TA100-VAO
- FAQ: September 6th TTI&TE "The development and latest application of sensors in industrial motors" live broadcast room Q&A
- 【RT-Thread Software Package Application Works】Photo Album
- Summary of Spectrum Analysis Algorithms
- What is an adder? What is an inverting adder?
- ±1% Tolerance 47kΩ Linear Thermistor in 0402 Package Option
- [Repost] Unpacking a hand warmer and power bank bought on a certain website for 6 yuan