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

1、key.h

cs 复制代码
#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

2、led.h

cs 复制代码
#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();
#endi

3、key.c

cs 复制代码
#include"key.h"
//按键3的配置
void key3_config()
{
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF8 GPIO输入
    GPIOF->MODER &= (~(0x3<<16));
    //设置PF8产生EXTI事件 EXTI_EXTICRx
    EXTI->EXTICR3 &=(~(0xFF<<0));
    EXTI->EXTICR3 |= (0x5<<0);
    //设置事件触发方式为下降沿触发EXTI_FTSR1
    EXTI->FTSR1 |= (0x1<<8);
    //设置EXTI事件触发不屏蔽EXTI_IMR1
    EXTI->C1IMR1 |= (0x1<<8);
    //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    GICD->ISENABLER[3] |= (0X1<<2); 
    // GICD_ISENABLERx设置中断优先级
    GICD->IPRIORITYR[24] &= (~(0X1F<<19)); 
    //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx
    GICD->ITARGETSR[24]  &= (~(0X3<<16));
    GICD->ITARGETSR[24] |= (0X1<<16);
    //设置中断优先级掩码GICC_PMR
    GICC->PMR |= (0x1F<<3);
    //使能CPU可以处理组0转发的中断GICC_CTLR
    GICC->CTRL |= 0X1;
}
//按键1 按键2以及GICC层的配置
void key_config()
{
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF9 PF7GPIO输入
    //PF9
    GPIOF->MODER &= (~(0x3<<18));
    //pf7
    GPIOF->MODER &= (~(0x3<<14));
    //设置PF9 PF7 产生EXTI事件 EXTI_EXTICRx
    //pf7
    EXTI->EXTICR2 &=(~(0xFF<<24));
    EXTI->EXTICR2 |= (0x5<<24);
    //pf9
    EXTI->EXTICR3 &=(~(0xFF<<8));
    EXTI->EXTICR3 |= (0x5<<8);
    //设置事件触发方式为下降沿触发EXTI_FTSR1
    //pf7
    EXTI->FTSR1 |= (0x1<<7);
    //pf9
    EXTI->FTSR1 |= (0x1<<9);
    //设置EXTI事件触发不屏蔽EXTI_IMR1
    //PF7
    EXTI->C1IMR1 |= (0x1<<7);
    //PF9
    EXTI->C1IMR1 |= (0x1<<9);
    //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    //PF7 97号中断
    GICD->ISENABLER[3] |= (0X1<<1); 
    //PF9 99号中断
    GICD->ISENABLER[3] |= (0X1<<3); 
    // GICD_ISENABLERx设置中断优先级
    //PF7
    GICD->IPRIORITYR[24] &= (~(0X1F<<11)); 
    //PF9
    GICD->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_CTLR
    GICD->CTRL |= 0X1;
    //设置中断优先级掩码GICC_PMR
    GICC->PMR |= (0x1F<<3);
    //使能CPU可以处理组0转发的中断GICC_CTLR
    GICC->CTRL |= 0X1;  
}

4、led.c

cs 复制代码
#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);
	RCC->MP_AHB4LPENSETR |= (0x1<<1);
}
//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));
}

5、do_irq.c

cs 复制代码
#include"key.h"
#include"led.h"
//extern void printf(const char *fmt, ...);

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)
		{
			//PF10
			GPIOF->ODR |= (0x1<<10);
			//PE10 PE8
			GPIOE->ODR |= (0x5<<8);
			num1++;
		}
		//灯灭
		else if(num1 > 0)
		{
			//PF10
			GPIOF->ODR &= ~(0x1<<10);
			//PE10 PE8
			GPIOE->ODR &= ~(0x5<<8);
			num1 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<3);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1<<9);
		break;
	case 97://key2
		//按键2中断处理
		//printf("KEY2 INT\n");
		//蜂鸣器转
		if(num2 == 0)
		{
			//PB6
			GPIOB->ODR |= (0x1<<6);
			num2++;
		}
		//蜂鸣器停
		else if(num2 > 0)
		{
			//PB6
			GPIOB->ODR &= ~(0x1<<6);
			num2 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<1);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1<<7);
		break;
	case 98://key3
		//按键3中断处理
		//printf("KEY3 INT\n");
		//风扇转
		if(num3 == 0)
		{
			//PE9
			GPIOE->ODR |= (0x1<<9);
			num3++;
		}
		//风扇停
		else if(num3 > 0)
		{
			//PE9
			GPIOE->ODR &= ~(0x1<<9);
			num3 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<2);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1<<8);
		break;
	}
	//清除处理完的中断号GICC_EOIR
	GICC->EOIR =irqno;
}

6、main.c

cs 复制代码
#include "key.h"
#include "led.h"

int main()
{
	//中断初始化
	key_config();
	key3_config();
	
	//GPIO串口初始化
	gpio_start();
	while(1){}
	
	return 0;
}
相关推荐
森旺电子2 小时前
51单片机仿真摇号抽奖机源程序 12864液晶显示
单片机·嵌入式硬件·51单片机
不过四级不改名6774 小时前
蓝桥杯嵌入式备赛教程(1、led,2、lcd,3、key)
stm32·嵌入式硬件·蓝桥杯
小A1594 小时前
STM32完全学习——SPI接口的FLASH(DMA模式)
stm32·嵌入式硬件·学习
Rorsion4 小时前
各种电机原理介绍
单片机·嵌入式硬件
善 .7 小时前
单片机的内存是指RAM还是ROM
单片机·嵌入式硬件
超级码农ProMax7 小时前
STM32——“SPI Flash”
stm32·单片机·嵌入式硬件
Asa3198 小时前
stm32点灯Hal库
stm32·单片机·嵌入式硬件
end_SJ10 小时前
初学stm32 --- 外部中断
stm32·单片机·嵌入式硬件
gantengsheng11 小时前
基于51单片机和OLED12864的小游戏《贪吃蛇》
单片机·嵌入式硬件·游戏·51单片机
嵌入式小强工作室11 小时前
stm32 查找进硬件错误方法
stm32·单片机·嵌入式硬件