src/key_it.c
#include "key_it.h"
#include "led.h"
void key_it_config()
{//RCC使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF9 PF7 PF8GPIO输入//PF9GPIOF->MODER &= (~(0x3<<18));//PF8GPIOF->MODER &= (~(0x3<<16));//PF7GPIOF->MODER &= (~(0x3<<14));//设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx//PF7EXTI->EXTICR2 &= (~(0xFF<<24));EXTI->EXTICR2 |= (0x5<<24);//PF8EXTI->EXTICR3 &= (~(0xFF<<0));EXTI->EXTICR3 |= (0x5<<0);//PF9EXTI->EXTICR3 &= (~(0xFF<<8));EXTI->EXTICR3 |= (0x5<<8);//设置事件触发方式为下降沿触发EXTI_FTSR1//PF7EXTI->FTSR1 |= (0x1<<7);//PF8EXTI->FTSR1 |= (0x1<<8);//PF9EXTI->FTSR1 |= (0x1<<9);//设置EXTI事件触发不屏蔽EXTI_IMR1//PF7EXTI->C1IMR1 |= (0x1<<7);//PF8EXTI->C1IMR1 |= (0x1<<8);//PF9EXTI->C1IMR1 |= (0x1<<9);//使能组0转发中断GICD_CTLRGICC->CTRL |= 0x1;//使能中断能转发到待定的CPU接口层GICD_ISENABLERx//PF7 97中断GICD->ISENABLER[3] |= (0x1<<1);//PF7 98中断GICD->ISENABLER[3] |= (0x1<<2);//PF7 99中断GICD->ISENABLER[3] |= (0x1<<3);//GICD_ISENABLERx设置中断优先级//PF7GICD->IPRIORITYR[24] &= (~(0x1F<<11));//PF8GICD->IPRIORITYR[24] &= (~(0x1F<<19));//PF9GICD->IPRIORITYR[24] &= (~(0x1F<<27));//设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx//PF7 97中断GICD->ITARGETSR[24] &= (~(0x3<<8));GICD->ITARGETSR[24] |= (0x1<<8);//PF8 98中断GICD->ITARGETSR[24] &= (~(0x3<<16));GICD->ITARGETSR[24] |= (0x1<<16);//PF9 99中断GICD->ITARGETSR[24] &= (~(0x3<<24));GICD->ITARGETSR[24] |= (0x1<<24);//使能CPU可以处理组0转发的中断GICC_CTLRGICD->CTRL |= 0x1;//设置中断优先级掩码GICC_CTLRGICC->PMR |= (0x1F<<3);
}
//延时函数
/*void delay(int ms)
{int i,j;for(i=0;i<ms;i++){for(j=0;j<2000;j++);}
}*/
//灯 GPIO初始化
void all_led_init()
{//RCC使能RCC->MP_AHB4ENSETR |= (0x3<<4);//设置PE10//PE10GPIOE->MODER &=(~(0x3f<<16));GPIOE->MODER |=(0x15<<16);//PB6GPIOB->MODER &=(~(0x3<<12));GPIOB->MODER |=(0x1<<12);//设置推挽输出//PE10GPIOE->OTYPER &=(~(0x1<<10));//PB6GPIOB->OTYPER &=(~(0x1<<6));//设置2个管脚低速输出//PE10GPIOE->OSPEEDR &=(~(0x3<<20));//PB6GPIOB->OSPEEDR &=(~(0x3<<12));//PE10GPIOE->PUPDR &=(~(0x3<<20));//PB6GPIOB->PUPDR &=(~(0x3<<12));
}
src/do_irq.c
#include "key_it.h"
#include "myuart.h"
#include "led.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;int num1=0,num2=0,num3=0;
void do_irq(void)
{int irqno;//保存中断号irqno = GICC->IAR&0X3FF;switch(irqno){case 99://key1//按键1中断处理printf("KEY INT1\n");//风扇转if(num3==0){GPIOE->ODR |=(0x1<<9);num3++;}//风扇停else if(num3>0){GPIOE->ODR &=(~(0x1<<9));num3=0;}//清除挂起中断标志位GICD_ICPENDRxGICD->ICPENDR[3] |= (0x1<<3);//清除中断触发标志位EXTI_FRR1EXTI->FPR1 |= (0x1<<9);break;case 97://key2//按键2中断处理printf("KEY INT2\n");//蜂鸣器响if(num2==0){GPIOB->ODR |=(0x1<<6);num2++;}//蜂鸣器停else if(num2>0){GPIOB->ODR &=(~(0x1<<6));num2=0;}//清除挂起中断标志位GICD_ICPENDRxGICD->ICPENDR[3] |= (0x1<<1);//清除中断触发标志位EXTI_FRR1EXTI->FPR1 |= (0x1<<7);break;case 98://key3//按键3中断处理printf("KEY INT3\n");//LED1亮if(num1==0){GPIOE->ODR |=(0x1<<10);num1++;}else if(num1>0){GPIOE->ODR &=(~(0x1<<10));num1=0;}//清除挂起中断标志位GICD_ICPENDRxGICD->ICPENDR[3] |= (0x1<<2);//清除中断触发标志位EXTI_FRR1EXTI->FPR1 |= (0x1<<8);break;}//清除处理完的中断号GICC_EOIRGICC->EOIR=irqno;
}
include/led.h
#ifndef __LED_H__
#define __LED_H__
//创建寄存器组织结构体
/*
typedef struct
{unsigned int moder;unsigned int otyper;unsigned int ospeedr;unsigned int pupdr;unsigned int idr;unsigned int odr;
}gpio_t;
#define GPIOE (*(gpio_t)0x50006000)
#define GPIOE (*(gpio_t)0x50007000)
#define RCC (*(unsigned int *)0x50000A28)
*/
void deley(int ms);
void all_led_init();
#endif
include/key_it.h
#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void key_it_config();
#endif
main.c
#include "myuart.h"
#include "key_it.h"
#include "led.h"
int main()
{//char c;//char *s;uart4_init();//串口初始化//中断初始化key_it_config();//初始化all_led_init();while(1){//保证主程序不结束//串口数据收发//接受一个字符//c=getchar();//发送字符//putchar(c+1);//接受一个字符串//s=my_gets();//发送一个字符串//my_puts(s);}return 0;
}
结果: