stm32mp157中断简单应用

设置按键中断,按键1按下,LED亮,再按一次,灭
按键2按下,蜂鸣器响。再按一次,不响
按键3按下,风扇转,再按一次,风扇停

main.c

#include "gpio.h"
#include "key_it.h"

int main()
{
	
    //中断初始化
	key3_it_config();
    key_it_config();
	led_init();
	beep_init();
	fan_init();

    while(1)
    {
        //保证主程序不结束
    }	
	return 0;
}

do_irq.c

#include "key_it.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;

void delay(int ms){
	for(int i=0; i<ms; i++){
		for(int j=0; j<2000; j++){}
	}
}
unsigned int flag_led = 0;
unsigned int flag_beep = 0;
unsigned int flag_fan = 0;

void do_irq(void) 
{
	//保存中断号
	int irqno;
	printf("%d\n", irqno);
	irqno = GICC->IAR & 0x3FF;

	switch(irqno)
	{
	case 99:
		//按键1中断处理
		if(flag_led==0){
			GPIOE->ODR |= (0x1<<10);
			GPIOF->ODR |= (0x1<<10);
			GPIOE->ODR |= (0x1<<8);
			flag_led = 1;
		}else{
			GPIOE->ODR &= (~(0x1<<10));
			GPIOF->ODR &= (~(0x1<<10));
			GPIOE->ODR &= (~(0x1<<8));
			flag_led = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1 << 3);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1 << 9);
		break;
	case 97:
		//按键2中断处理
		if(flag_beep==0){
			GPIOB->ODR |= (0x1<<6);
			flag_beep = 1;
		}else{
			GPIOB->ODR &= (~(0x1<<6));
			flag_beep = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1 << 1);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1 << 7);
			break;
	case 98:
		//按键3中断处理
		if(flag_fan==0){
			GPIOE->ODR |= (0x1<<9);
			flag_fan = 1;
		}else{
			GPIOE->ODR &= (~(0x1<<9));
			flag_fan = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1 << 2);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1 << 8); 
		break;
	}

	//清除处理完的中断号GICC_EOIR
	GICC->EOIR = irqno;
}

key_it.c

#include "key_it.h"

void key3_it_config()
{
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    GPIOF->MODER &= (~(0x3<<16));
    EXTI->EXTICR3 &=(~(0xFF<<0));
    EXTI->EXTICR3 |= (0x5<<0);
    EXTI->FTSR1 |= (0x1<<8);
    EXTI->C1IMR1 |= (0x1<<8);
    GICD->ISENABLER[3] |= (0X1<<2); 
    GICD->IPRIORITYR[24] &= (~(0X1F<<19)); 
    GICD->ITARGETSR[24]  &= (~(0X3<<16));
    GICD->ITARGETSR[24] |= (0X1<<16);
}

void key_it_config()
{
	//RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF9 PF7 PF8GPIO输入
    //PF9
    GPIOF->MODER &= (~(0x3<<18));
    //PF8
    // GPIOF->MODER &= (~(0x3<<16));
    //pf7
    GPIOF->MODER &= (~(0x3<<14));
    //设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx
    //pf7
    EXTI->EXTICR2 &=(~(0xFF<<24));
    EXTI->EXTICR2 |= (0x5<<24);
    //pf8
    // EXTI->EXTICR3 &=(~(0xFF<<0));
    // EXTI->EXTICR3 |= (0x5<<0);
    //pf9
    EXTI->EXTICR3 &=(~(0xFF<<8));
    EXTI->EXTICR3 |= (0x5<<8);
    //设置事件触发方式为下降沿触发EXTI_FTSR1
    //pf7
    EXTI->FTSR1 |= (0x1<<7);
    //pf8
    // EXTI->FTSR1 |= (0x1<<8);
    //pf9
    EXTI->FTSR1 |= (0x1<<9);
    //设置EXTI事件触发不屏蔽EXTI_IMR1
    //PF7
    EXTI->C1IMR1 |= (0x1<<7);
    //PF8
    // EXTI->C1IMR1 |= (0x1<<8);
    //PF9
    EXTI->C1IMR1 |= (0x1<<9);
    //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    //PF7 97号中断
    GICD->ISENABLER[3] |= (0X1<<1); 
    //PF78 98号中断
    // GICD->ISENABLER[3] |= (0X1<<2); 
    //PF9 99号中断
    GICD->ISENABLER[3] |= (0X1<<3); 
    // GICD_ISENABLERx设置中断优先级
    //PF7
    GICD->IPRIORITYR[24] &= (~(0X1F<<11)); 
    //PF8
    // GICD->IPRIORITYR[24] &= (~(0X1F<<19)); 
    //PF9
    GICD->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);
    //使能组0转发中断GICD_CTLR
    GICD->CTRL |= 0X1;
    //设置中断优先级掩码GICC_PMR
    GICC->PMR |= (0x1F<<3);

    //使能CPU可以处理组0转发的中断GICC_CTLR
    GICC->CTRL |= 0X1;
}


void led_init(){
	//将gpioe和GPIOF的时钟使能
	RCC->MP_AHB4ENSETR |= (0x3<<4);
	//将三盏灯对应的gpio口的模式设置为输出
	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->MODER |= (0x1<<10);
	GPIOF->MODER |= (0x1<<10);
	GPIOE->MODER |= (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 beep_init(){
	//将gpiob的时钟使能
	RCC->MP_AHB4ENSETR |= (0x1<<1);
 
	//讲蜂鸣器对应的PB6口的模式设置为输出模式
	GPIOB->MODER &= (~(0x3<<12));
	GPIOB->MODER |= (0x1<<12);
 
	//将输出模式设置为推挽输出
	GPIOB->MODER |= (0x1<<6);
	//讲输出设置为低速
	GPIOB->OSPEEDR &= (~(0x3<<12));
	//设置无上拉下拉电阻
	GPIOB->PUPDR &= (~(0x3<<12));
}
 
void fan_init(){
	//将gpiob的时钟使能
	RCC->MP_AHB4ENSETR |= (0x1<<4);
 
	//讲风扇对应的PE9口的模式设置为输出模式
	GPIOE->MODER &= (~(0x3<<18));
	GPIOE->MODER |= (0x1<<18);
 
	//将输出模式设置为推挽输出
	GPIOE->MODER |= (0x1<<9);
	//讲输出设置为低速
	GPIOE->OSPEEDR &= (~(0x3<<18));
	//设置无上拉下拉电阻
	GPIOE->PUPDR &= (~(0x3<<18));
}

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();
void key3_it_config();
void led_init();
void beep_init();
void fan_init();
#endif

效果图

相关推荐
芋头莎莎4 小时前
STM32 51单片机设计半导体制冷片温控设计
stm32·嵌入式硬件·51单片机
ღ 金龍戲水 ღ5 小时前
蓝桥杯竞赛单片机组备赛【经验帖】
经验分享·单片机·蓝桥杯
搬砖的小码农_Sky5 小时前
单片机和FPGA有什么区别?
单片机·嵌入式硬件·fpga开发
折途7 小时前
拆解一下用了两年的三十多块的剃须刀
嵌入式硬件
BT-BOX8 小时前
STM32仿真proteus位带操作和keil增加头文件C文件
c语言·stm32·proteus
7yewh10 小时前
嵌入式硬件实战提升篇(一)-泰山派RK3566制作多功能小手机
linux·arm开发·驱动开发·嵌入式硬件·物联网·智能手机·硬件架构
@晓凡11 小时前
STM32编程遇到的问题随笔【一】
stm32·单片机·嵌入式硬件
虾球xz12 小时前
游戏引擎学习第11天
stm32·学习·游戏引擎
DevinLGT12 小时前
6Pin Type-C Pin脚定义:【图文讲解】
人工智能·单片机·嵌入式硬件
小A15913 小时前
STM32完全学习——系统时钟设置
stm32·嵌入式硬件·学习