三个按键的中断
do_irq.c
#include "mykey.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{//获取中断号unsigned int irqno = (GICC->IAR&0x3ff);switch (irqno){case 99://中断处理逻辑printf("KEY1_INTC\n");//清除GICD的中断标志GICD->ICPENDR[3] |= (0x1<<3);//清除EXTI中的中断触发标志EXTI->FPR1 |= (0x1<<9);break;case 97://中断处理逻辑printf("KEY2_INTC\n");//清除GICD的中断标志GICD->ICPENDR[3] |= (0x1<<1);//清除EXTI中的中断触发标志EXTI->FPR1 |= (0x1<<7);break;case 98://中断处理逻辑printf("KEY3_INTC\n");//清除GICD的中断标志GICD->ICPENDR[3] |= (0x1<<2);//清除EXTI中的中断触发标志EXTI->FPR1 |= (0x1<<8);break;}//清除IAR寄存器中保存的中断号GICC->EOIR = irqno;
}
main.c
#include "mykey.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<2000;j++){}}}int main(){//按键中断初始化
key1_init_config();
key2_init_config();
key3_init_config();while(1)
{printf("main func\n");delay_ms(1000);}return 0; }
mykey.c
#include "mykey.h"void key1_init_config()
{// 使能GPIOF外设时钟RCC->MP_AHB4ENSETR |= (0x1<<5);// 设置PF9为输入GPIOF->MODER &= (~(0X3<<18));// 设置PF9产生EXTI9事件EXTI->EXTICR3 &= (~(0xff<<8));EXTI->EXTICR3 |= (0x05<<8);// 设置下降沿检测EXTI9事件EXTI->FTSR1 |= (0x1<<9);// 取消EXTI9事件的屏蔽EXTI->C1IMR1 |= (0x1<<9);// 全局使能99号中断GICD->ISENABLER[3] |= (0x1<<3);// 设置99号中断的优先级GICD->IPRIORITYR[24] &= (~(0x1f<<27));//最高优先级// 设置99号中断的目标处理器GICD->ITARGETSR[24] |= (0x1<<24);// 允许组0中断被转发到GICC层GICD->CTRL |= 0x1;// 设置中断优先级掩码GICC->PMR |= (0x1f<<3);// 允许组0中断被转发给处理器GICC->CTRL |= 0x1;}
void key2_init_config()
{// 使能GPIOF外设时钟RCC->MP_AHB4ENSETR |= (0x1<<5);// 设置PF7为输入GPIOF->MODER &= (~(0X3<<14));// 设置PF7产生EXTI7事件EXTI->EXTICR2 &= (~(0xff<<24));EXTI->EXTICR2 |= (0x05<<24);// 设置下降沿检测EXTI7事件EXTI->FTSR1 |= (0x1<<7);// 取消EXTI7事件的屏蔽EXTI->C1IMR1 |= (0x1<<7);// 全局使能97号中断GICD->ISENABLER[3] |= (0x1<<1);// 设置97号中断的优先级GICD->IPRIORITYR[24] &= (~(0x1f<<11));//最高优先级// 设置99号中断的目标处理器GICD->ITARGETSR[24] |= (0x1<<8);// 允许组0中断被转发到GICC层GICD->CTRL |= 0x1;// 设置中断优先级掩码GICC->PMR |= (0x1f<<3);// 允许组0中断被转发给处理器GICC->CTRL |= 0x1;
}void key3_init_config()
{// 使能GPIOF外设时钟RCC->MP_AHB4ENSETR |= (0x1<<5);// 设置PF8为输入GPIOF->MODER &= (~(0X3<<16));// 设置PF8产生EXTI8事件EXTI->EXTICR3 &= (~(0xff));EXTI->EXTICR3 |= (0x05);// 设置下降沿检测EXTI8事件EXTI->FTSR1 |= (0x1<<8);// 取消EXTI8事件的屏蔽EXTI->C1IMR1 |= (0x1<<8);// 全局使能98号中断GICD->ISENABLER[3] |= (0x1<<2);// 设置97号中断的优先级GICD->IPRIORITYR[24] &= (~(0x1f<<19));//最高优先级// 设置99号中断的目标处理器GICD->ITARGETSR[24] |= (0x1<<16);// 允许组0中断被转发到GICC层GICD->CTRL |= 0x1;// 设置中断优先级掩码GICC->PMR |= (0x1f<<3);// 允许组0中断被转发给处理器GICC->CTRL |= 0x1;
}