设置按键中断,按键1按下,LED亮,再次按下,灭
按键2按下,蜂鸣器叫,再次按下,停
按键3按下,风扇转,再次按下,停
头文件
#ifndef __CTRL_KEY_H__
#define __CTRL_KEY_H__#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void all_led_init(); //初始化灯
void fan_init(); //初始化风扇
void voice_init(); //初始化蜂鸣器
void key_config(); //初始化中断
#endif
异常处理文件
#include "ctrl_key.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{int irq_num; //保存中断号irq_num = GICC->IAR &0x3ff;switch(irq_num){//key1case 99://按键1中断处理printf("KEY1 INT\n");//灯亮if(GPIOE->ODR &=(0x1<<10)){GPIOE->ODR &= (~(0x1<<10));}else{GPIOE->ODR |=(0x1<<10);}//清除挂起中断标志位GICD->ICPENDR[3] |= (0x1<<3);//清除中断触发标志位EXTI->FPR1 |=(0x1<<9);break;case 97: //key2//按键1中断处理printf("KEY2 INT\n");//蜂鸣器叫if(GPIOB->ODR &=(0x1<<6)){GPIOB->ODR &= (~(0x1<<6));}else{GPIOB->ODR |=(0x1<<6);}//清除挂起中断标志位GICD->ICPENDR[3] |= (0x1<<1);//清除中断触发标志位EXTI->FPR1 |=(0x1<<7);break;case 98: //key3//按键1中断处理printf("KEY3 INT\n");//风扇转if(GPIOE->ODR &= (0x1<<9)){GPIOE->ODR &=(~(0x1<<9));}else{GPIOE->ODR |=(0x1<<9);}//清除挂起中断标志位GICD->ICPENDR[3] |= (0x1<<2);//清除中断触发标志位EXTI->FPR1 |=(0x1<<8);break;}//清除处理完的中断号GICC->EOIR =irq_num;
}
初始化文件
#include "ctrl_key.h"void key_config()
{//RCC使能GPIOF时钟 第5位置1RCC->MP_AHB4ENSETR |=(0x1<<5);//设置PF9、PF7、PF8的GPIOF输入模式 将[14:19]位置0GPIOF->MODER &= (~(0x3f<<14));//设置PF9、PF7、PF8产生EXTI事件//key2EXTI->EXTICR2 &=(~(0xff<<24));EXTI->EXTICR2 |=(0x5<<24);//key3EXTI->EXTICR3 &=(~(0xff));EXTI->EXTICR3 |=(0x05);//key1EXTI->EXTICR3 &=(~(0xff<<8));EXTI->EXTICR3 |=(0x05<<8);//设置事件触发方式为下降沿触发EXTI->FTSR1 |= (0x7<<7);//设置EXTI事件触发不屏蔽EXTI->C1IMR1 |=(0x7<<7);//使能组0转发中断GICD->CTRL |=(0x1);//使能中断能转发到指定的cpu接口层GICD_ISENABLERx//key1GICD->ISENABLER[3] |=(0x1<<3);//key2GICD->ISENABLER[3] |=(0x1<<1);//key3GICD->ISENABLER[3] |=(0x1<<2);//GICD_IPRIORITYRx寄存器 设置中断优先级//key1优先级GICD->IPRIORITYR[24] &= (~(0x1f<<27));//Key2优先级GICD->IPRIORITYR[24] &= (~(0x1f<<11));//key3优先级GICD->IPRIORITYR[24] &= (~(0x1f<<19));//设置当前中断被转发到哪一个cpu处理GICD_ITARGETSRxGICD->ITARGETSR[24] &= (~(0x3<<24));GICD->ITARGETSR[24] |= (0x1<<24);GICD->ITARGETSR[24] &= (~(0x3<<8));GICD->ITARGETSR[24] |= (0x1<<8);GICD->ITARGETSR[24] &= (~(0x3<<16));GICD->ITARGETSR[24] |= (0x1<<16);//使能cpu可以处理组0转发的中断GICC_CTLRGICC->CTRL |=(0x1);//设置中断优先级掩码GICC_PMRGICC->PMR |=(0x1f<<3); //设置最低优先级
}//初始化灯、
void all_led_init()
{//RCC使能RCC->MP_AHB4ENSETR |= (0x3<<4);//设置为输出模式GPIOE->MODER &= (~(0x3<<20));GPIOE->MODER |= (0x1<<20);GPIOF->MODER &= (~(0x3<<20));GPIOF->MODER |= (0x1<<20);GPIOE->MODER &= (~(0x3<<16));GPIOE->MODER |= (0x1<<16);//设置为推挽输出GPIOE->OTYPER &= (~(0x1<<10));GPIOF->OTYPER &= (~(0X1<<10));GPIOE->OTYPER &= (~(0X1<<8));//设置输出速度GPIOE->OSPEEDR &= (~(0X3<<20));GPIOF->OSPEEDR &= (~(0X3<<20));GPIOE->OSPEEDR &= (~(0X3<<16));//不设置上拉、下拉GPIOE->PUPDR &= ~((0X3<<20));GPIOF->PUPDR &= ~((0X3<<20));GPIOE->PUPDR &= ~((0X3<<16));
}//初始化风扇
void fan_init() //PE9
{//RCC使能GPIOERCC->MP_AHB4ENSETR |= (0x1<<4);//设置为输出模式GPIOE->MODER &= (~(0x3<<18));GPIOE->MODER |= (0x1<<18);//设置推挽输出GPIOE->OTYPER &= (~(0x1<<9));
}//初始化蜂鸣器
void voice_init() //PB6
{//RCC使能GPIOBRCC->MP_AHB4ENSETR |=(0x1<<1);//设置输出模式GPIOB->MODER &= (~(0x3<<12));GPIOB->MODER |= (0x1<<12);//设置推挽输出GPIOB->OTYPER &=(~(0x1<<6));}
调试主文件
#include "ctrl_key.h"int main()
{//初始化灯all_led_init();//初始化风扇fan_init();//初始化蜂鸣器voice_init();//中断初始化key_config();while(1){//保证程序不结束}return 0;
}
实验效果: