作业 11.30 按键中断

头文件:key.h

#ifndef __KEY_H__
#define __KEY_H__

#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"

//KEY1--->PF9
//初始化RCC和GPIO章节
void hal_gpio_key1(void);

//初始化EXTI章节
void hal_key1_exti_it(void);

//初始化GICD章节
void hal_key1_gicd_it(void);

//初始化GICC章节
void hal_key1_gicc_it(void);
#endif

功能文件:key.c

#include "key.h"
//KEY1--->PF9
//初始化RCC和GPIO章节
void hal_gpio_key1(void)
{
	//1.使能GPIOF组时钟 MP_AHB4ENSETR[5] = 1
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	//2.设置PF9引脚为输入模式 MODER[19:18] = 00
	GPIOF->MODER &= (~(0x3 << 18));
}

//初始化EXTI章节
void hal_key1_exti_it(void)
{
	//1.设置PF9引脚与EXTI9进行连接 EXTICR3[15:8] = 0x05
	EXTI->EXTICR3 &= (~(0xff << 8));
	EXTI->EXTICR3 |= (0x05 << 8);
	//2.设置PF9引脚为下降沿触发方式  FTSR1[9] = 1
	EXTI->FTSR1 |= (0x1 << 9);
	//3.设置中断不屏蔽 C1IMR1[9] = 1
	EXTI->C1IMR1 |= (0x1 << 9);
}

//初始化GICD章节
void hal_key1_gicd_it(void)
{
	//1.设置GICD层全局中断使能寄存器 CTRL[0] = 1
	GICD->CTRL |= (0x1 << 0);
	//2.设置GICD层中断使能寄存器 ISENABLER3[3] = 1
	GICD->ISENABLER[3] |= (0x1 << 3);
	//3.设置GICD层中断优先级寄存器 IPRIORITYR24[31:27] 
	GICD->IPRIORITYR[24] &= (~(0x1f << 27));
	//4.设置GICD层中断目标分配寄存器 ITARGETSR24[25:24] = 01
	GICD->ITARGETSR[24] &= (~(0x3 << 24));
	GICD->ITARGETSR[24] |= (0x1 << 24);
}

//初始化GICC章节
void hal_key1_gicc_it(void)
{
	//1.设置GICC层全局中断使能寄存器 CTRL[0] = 1
	GICC->CTRL |= (0x1 << 0);
	//2.设置中断屏蔽寄存器 PMR[7:3]
	GICC->PMR &= (~(0x1f << 3));
	GICC->PMR |= (0x1f << 3);
	
}

主函数:main.c

#include "key.h"

extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
	int i,j;
	for(i = 0; i < ms;i++)
		for (j = 0; j < 1800; j++);
}

int main()
{
	hal_gpio_key1();
	hal_key1_exti_it();
	hal_key1_gicd_it();
	hal_key1_gicc_it();
	while(1)
	{
	}
	return 0;
}

现象: 

作业 11.30 按键中断_第1张图片

你可能感兴趣的:(单片机,stm32,嵌入式硬件)