设置按键中断,按键1按下,LED亮,再按一次,灭;按键2按下,蜂鸣器响,再按一次,不响;按键3按下,风扇转,再按一次,风扇停
代码
do_irq.c
#include "key.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
unsigned int num1 = 0, num2 = 0,num3 = 0; //用于记录按键按下次数
void do_irq(void)
{int irqno;//保存中断号irqno=GICC->IAR & 0X3FF;switch(irqno){case 99://key1//按键1中断处理printf("KEY1 INT\n");//灯亮if(num1 == 0){//PF10GPIOF->ODR |= (0x1<<10);//PE10 PE8GPIOE->ODR |= (0x5<<8);num1++;}//灯灭else if(num1 > 0){//PF10GPIOF->ODR &= ~(0x1<<10);//PE10 PE8GPIOE->ODR &= ~(0x5<<8);num1 = 0;}//清除挂起中断标志位GICD_ICPENDRxGICD->ICPENDR[3] |= (0x1<<3);//清除中断触发标志位EXTI_FPR1EXTI->FPR1 |= (0x1<<9);break;case 97://key2//按键2中断处理printf("KEY2 INT\n");//蜂鸣器转if(num2 == 0){//PB6GPIOB->ODR |= (0x1<<6);num2++;}//蜂鸣器停else if(num2 > 0){//PB6GPIOB->ODR &= ~(0x1<<6);num2 = 0;}//清除挂起中断标志位GICD_ICPENDRxGICD->ICPENDR[3] |= (0x1<<1);//清除中断触发标志位EXTI_FPR1EXTI->FPR1 |= (0x1<<7);break;case 98://key3//按键3中断处理printf("KEY3 INT\n");//风扇转if(num3 == 0){//PE9GPIOE->ODR |= (0x1<<9);num3++;}//风扇停else if(num3 > 0){//PE9GPIOE->ODR &= ~(0x1<<9);num3 = 0;}//清除挂起中断标志位GICD_ICPENDRxGICD->ICPENDR[3] |= (0x1<<2);//清除中断触发标志位EXTI_FPR1EXTI->FPR1 |= (0x1<<8);break;}//清除处理完的中断号GICC_EOIRGICC->EOIR =irqno;
}
led.c
#include "led.h"
//延时
void delay(int ms)
{int i,j;for(i = 0;i < ms;i++){for(j = 0;j < 2000;j++);}
}
//给RCC使能
void rcc_start()
{RCC->MP_AHB4LPENSETR |= (0x3<<4);
}
//GPIO初始化
void gpio_start()
{//PE10 PE9 PE8设置为输出模式GPIOE->MODER &= (~(0x3f<<16));GPIOE->MODER |= (0x15<<16);//PF10设置为输出模式GPIOF->MODER &= (~(0x3<<20));GPIOF->MODER |= (0x1<<20);//PB6设置为输出模式GPIOB->MODER &= (~(0x3<<12));GPIOB->MODER |= (0x1<<12);//PE10 PE9 PE8设置为推挽输出GPIOE->OTYPER &= (~(0x7<<8));//PF10设置为推挽输出GPIOF->OTYPER &= (~(0x1<<10));//PB6设置为推挽输出GPIOB->OTYPER &= (~(0x1<<6));//PE10 PE9 PE8设置为低速输出GPIOE->OSPEEDR &= (~(0x3f<<16));//PF10设置为低速输出GPIOF->OSPEEDR &= (~(0x3<<20));//PB6设置为低速输出GPIOB->OSPEEDR &= (~(0x3<<12));//PE10 PE9 PE8设置为无上拉下拉电阻GPIOE->PUPDR &= (~(0x3f<<16));//PF10设置为无上拉下拉电阻GPIOF->PUPDR &= (~(0x3<<20));//PB6设置为无上拉下拉电阻GPIOB->PUPDR &= (~(0x3<<16));
}
led.h
#ifndef __LED_H__
#define __LED_H__
#include "../common/include/stm32mp1xx_exti.h"
#include "../common/include/stm32mp1xx_gpio.h"
#include "../common/include/stm32mp1xx_rcc.h"
#include "../common/include/stm32mp1xx_gic.h"
//延时
void delay(int ms);
//给RCC使能
void rcc_start();
//GPIO初始化
void gpio_start();
#endif
key.c
#include"key.h"
//按键3的配置
void key3_config()
{//RCC使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF8 GPIO输入GPIOF->MODER &= (~(0x3<<16));//设置PF8产生EXTI事件 EXTI_EXTICRxEXTI->EXTICR3 &=(~(0xFF<<0));EXTI->EXTICR3 |= (0x5<<0);//设置事件触发方式为下降沿触发EXTI_FTSR1EXTI->FTSR1 |= (0x1<<8);//设置EXTI事件触发不屏蔽EXTI_IMR1EXTI->C1IMR1 |= (0x1<<8);//使能中断能转发到特定的CPU接口层GICD_ISENABLERxGICD->ISENABLER[3] |= (0X1<<2); // GICD_ISENABLERx设置中断优先级GICD->IPRIORITYR[24] &= (~(0X1F<<19)); //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRxGICD->ITARGETSR[24] &= (~(0X3<<16));GICD->ITARGETSR[24] |= (0X1<<16);//设置中断优先级掩码GICC_PMRGICC->PMR |= (0x1F<<3);//使能CPU可以处理组0转发的中断GICC_CTLRGICC->CTRL |= 0X1;
}
//按键1 按键2以及GICC层的配置
void key_config()
{//RCC使能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF9 PF7GPIO输入//PF9GPIOF->MODER &= (~(0x3<<18));//pf7GPIOF->MODER &= (~(0x3<<14));//设置PF9 PF7 产生EXTI事件 EXTI_EXTICRx//pf7EXTI->EXTICR2 &=(~(0xFF<<24));EXTI->EXTICR2 |= (0x5<<24);//pf9EXTI->EXTICR3 &=(~(0xFF<<8));EXTI->EXTICR3 |= (0x5<<8);//设置事件触发方式为下降沿触发EXTI_FTSR1//pf7EXTI->FTSR1 |= (0x1<<7);//pf9EXTI->FTSR1 |= (0x1<<9);//设置EXTI事件触发不屏蔽EXTI_IMR1//PF7EXTI->C1IMR1 |= (0x1<<7);//PF9EXTI->C1IMR1 |= (0x1<<9);//使能中断能转发到特定的CPU接口层GICD_ISENABLERx//PF7 97号中断GICD->ISENABLER[3] |= (0X1<<1); //PF9 99号中断GICD->ISENABLER[3] |= (0X1<<3); // GICD_ISENABLERx设置中断优先级//PF7GICD->IPRIORITYR[24] &= (~(0X1F<<11)); //PF9GICD->IPRIORITYR[24] &= (~(0X1F<<27)); //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx//pf7 97中断GICD->ITARGETSR[24] &= (~(0X3<<8));GICD->ITARGETSR[24] |= (0X1<<8);//pf9 99中断GICD->ITARGETSR[24] &= (~(0X3<<24));GICD->ITARGETSR[24] |= (0X1<<24);//使能组0转发中断GICD_CTLRGICD->CTRL |= 0X1;//设置中断优先级掩码GICC_PMRGICC->PMR |= (0x1F<<3);//使能CPU可以处理组0转发的中断GICC_CTLRGICC->CTRL |= 0X1;
}
key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "../common/include/stm32mp1xx_exti.h"
#include "../common/include/stm32mp1xx_gpio.h"
#include "../common/include/stm32mp1xx_rcc.h"
#include "../common/include/stm32mp1xx_gic.h"
void key_config();
void key3_config();
#endif
main.c
#include "key.h"
#include "led.h"
int main()
{//中断初始化key_config();key3_config();//GPIO串口初始化gpio_start();while(1){//保证主程序不结束}return 0;
}