要求:例如:当按键1按下之后,打印11111111
key.c代码如下:
#include "key.h"
//EXTI控制器初始化
void exit_init()
{
//使能GPIOF组控制器
RCC->MP_AHB4ENSETR |= (0x1<<5);
//通过GPIOF_MODER寄存器设置PF9/PF7/PF8为输入模式
GPIOF->MODER &= (~(0x3<<18));
GPIOF->MODER &= (~(0x3<<14));
GPIOF->MODER &= (~(0x3<<16));
EXTI->EXTICR3 &= (~(0xff<<8));
EXTI->EXTICR3 |= (0x05<<8);
EXTI->EXTICR2 &= (~(0xff<<24));
EXTI->EXTICR2 |= (0x05<<24);
EXTI->EXTICR3 &= (~(0xff<<0));
EXTI->EXTICR3 |= (0x05<<0);
EXTI->C1IMR1 |=(0x1<<9);
EXTI->C1IMR1 |=(0x1<<7);
EXTI->C1IMR1 |=(0x1<<8);
EXTI->FTSR1 |=(0x1<<9);
EXTI->FTSR1 |=(0x1<<7);
EXTI->FTSR1 |=(0x1<<8);
}
//GICD控制器初始化
void gicd_init()
{
//设置GICD层CPU0组使能
GICD->CTRL |= (0x1<<0);
GICD->ISENABLER[3] |= (0x1<<3);
GICD->ISENABLER[3] |= (0x1<<1);
GICD->ISENABLER[3] |= (0x1<<2);
//中断屏蔽寄存器
GICD->IPRIORITYR[24] |= (0x1<<27);
GICD->IPRIORITYR[24] |= (0x2<<11);
GICD->IPRIORITYR[24] |= (0x3<<19);
//中断目标分配寄存器
GICD->ITARGETSR[24] |= (0x1<<24);
GICD->ITARGETSR[24] |= (0x1<<8);
GICD->ITARGETSR[24] |= (0x1<<16);
}
//GICC控制器初始化
void gicc_init()
{
GICC->CTRL |= (0x1<<0);
GICC->PMR |= (0xA<<3);
}
key.h代码如下:
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gpio.h"
//EXTI控制器初始化
void exit_init();
//GICD控制器初始化
void gicd_init();
//GICC控制器初始化
void gicc_init();
#endif
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()
{
exit_init();
gicd_init();
gicc_init();
while(1)
{
}
return 0;
}
do_irq.c
#include "key.h"
extern void printf(const char *fmt, ...);
extern void delay_ms(int ms);
unsigned int i = 0;
void do_irq(void)
{
//获取中断号
int num = GICC->IAR &(0x3ff<<0);
//判断中断号
if(num==99)
{
delay_ms(500);
printf("11111111\n");
EXTI->FPR1 |= (0x1<<9);
GICD->ICPENDR[3] |= (0x1<<3);
}
else if(num==97)
{
delay_ms(500);
printf("22222222\n");
EXTI->FPR1 |= (0x1<<7);
GICD->ICPENDR[3] |= (0x1<<1);
}
else if(num==98)
{
delay_ms(500);
printf("33333333\n");
EXTI->FPR1 |= (0x1<<8);
GICD->ICPENDR[3] |= (0x1<<2);
}
//99:KEY1按键按下
//清楚中断挂起标志位
GICC->EOIR &= (~(0x3ff<<0));
GICC->EOIR |= num;
}
结果如下:
![](https://i-blog.csdnimg.cn/blog_migrate/b8bd130b5d12cdb1b045872c6da04959.png)