STM32 study notes (9)

STM32F103ZET6 external interrupt experiment



Preface

The learning of STM32 can be divided into 3 versions.
1. Register version
2. Library function version
3. HAL library version
Due to personal reasons, I choose the library function version to learn STM32.


Tip: Problems such as software installation will not be explained! ! !

1. Schematic

Insert picture description here

Insert picture description here

Insert picture description here

Two, external interrupt configuration steps

Insert picture description here

Three, program source code

1.exti.h

code show as below:

#ifndef __EXTI_H
#define __EXTI_H

void EXTIX_Init(void);//步骤1-5

#endif

2.exti.c

code show as below:

#include "exti.h"
#include "led.h"
#include "key.h"
#include "usart.h"
#include "beep.h"
#include "delay.h"

void EXTIX_Init()
{
    
    
	EXTI_InitTypeDef EXTI_Initstr;
	NVIC_InitTypeDef NVIC_Initstr;
	KEY_Init();//初始化IO
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);//开启IO口时钟复用
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);//设置IO口与中断线的映射关系
	
	EXTI_Initstr.EXTI_Line=EXTI_Line4;
	EXTI_Initstr.EXTI_LineCmd=ENABLE;
	EXTI_Initstr.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_Initstr.EXTI_Trigger=EXTI_Trigger_Falling;
	EXTI_Init(&EXTI_Initstr);//初始化线上中断,设置触发条件等
	
	NVIC_Initstr.NVIC_IRQChannel=EXTI4_IRQn;
	NVIC_Initstr.NVIC_IRQChannelCmd=ENABLE;
	NVIC_Initstr.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_Initstr.NVIC_IRQChannelSubPriority=2;
	NVIC_Init(&NVIC_Initstr);//配置中断分组
}
void EXTI4_IRQHandler(void)
{
    
    
	delay_ms(10);
	if(KEY0==0)
	{
    
    
		LED1=!LED1;
	}
	EXTI_ClearITPendingBit(EXTI_Line4);
}

3.main.c

code show as below:

#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "beep.h"
#include "key.h"
#include "usart.h"
#include "exti.h"


int main(void)
{
    
    
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	delay_init();
    LED_Init();
	Beep_Init();
	KEY_Init();
	uart_init(115200);
	EXTIX_Init();
	while(1)
	{
    
    
		printf("OK\r\n");
		delay_ms(1000);
	}
		
}

4. Experimental results

When the KEY0 button is pressed, the LED1 turns on and off. (Interrupt service function)


to sum up

Time flies so fast. The first semester of junior year has passed. Even though the winter vacation is on, I still feel that I am very busy every day. One is to study stm32, and the other is to prepare for postgraduate entrance exams. I feel that junior and freshman, sophomore really It's different. I don't have the desire to play basketball at the slightest point. I am afraid that my time will pass by. In short, I hope that the postgraduate entrance examination can be successfully landed in 2022! Come on

Guess you like

Origin blog.csdn.net/weixin_44935259/article/details/112726478