main.c
cpp
#include "delay.h"
#include "led.h"
#include "beep.h"
#include "motor.h"
#include "fan.h"
#include "key.h"
int main()
{
LED1_Init();
LED2_Init();
LED3_Init();
BEEP_Init();
Motor_Init();
Fan_Init();
key1_Init();
key2_Init();
key3_Init();
while (1)
{
}
return 0;
}
key.c
cpp
#include "key.h"
void key1_Init(void)
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0X1<<5);
//将GPIOF_PIN9设置为输入
GPIOF->MODER &= (~(0X3<<18));
//让GPIOF_PIN9产生EXTI9事件
EXTI->EXTICR3 &= (~(0XFF<<8));
EXTI->EXTICR3 |= (0x5<<8);
//使用下降沿检测EXTI9事件
EXTI->FTSR1 |= (0X1<<9);
//取消EXTI9事件的屏蔽
EXTI->C1IMR1 |= (0X1<<9);
//使能99号中断
GICD->ISENABLER[3] |= (0X1<<3);
//设置99号中断的中断优先级
GICD->IPRIORITYR[24] &= (~(0X1F<<27));
//设置99号中断的目标处理器
GICD->ITARGETSR[24] &= (~(0X3<<24));
GICD->ITARGETSR[24] |= (0X1<<24);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= (0X1);
//设置中断优先级的掩码
GICC->PMR |= (0X1F<<3);
//全局使能组0中断被转发给处理器
GICC->CTRL |= (0X1);
}
void key2_Init(void)
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0X1<<5);
//将GPIOF_PIN7设置为输入
GPIOF->MODER &= (~(0X3<<14));
//让GPIOF_PIN7产生EXTI7事件
EXTI->EXTICR2 &= (~(0XFF<<24));
EXTI->EXTICR2 |= (0x5<<24);
//使用下降沿检测EXTI7事件
EXTI->FTSR1 |= (0X1<<7);
//取消EXTI7事件的屏蔽
EXTI->C1IMR1 |= (0X1<<7);
//使能97号中断
GICD->ISENABLER[3] |= (0X1<<1);
//设置97号中断的中断优先级
GICD->IPRIORITYR[24] &= (~(0X1F<<11));
//设置97号中断的目标处理器
GICD->ITARGETSR[24] &= (~(0X3<<8));
GICD->ITARGETSR[24] |= (0X1<<8);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= (0X1);
//设置中断优先级的掩码
GICC->PMR |= (0X1F<<3);
//全局使能组0中断被转发给处理器
GICC->CTRL |= (0X1);
}
void key3_Init(void)
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0X1<<5);
//将GPIOF_PIN8设置为输入
GPIOF->MODER &= (~(0X3<<16));
//让GPIOF_PIN8产生EXTI8事件
EXTI->EXTICR3 &= (~(0XFF));
EXTI->EXTICR3 |= (0x5);
//使用下降沿检测EXTI8事件
EXTI->FTSR1 |= (0X1<<8);
//取消EXTI8事件的屏蔽
EXTI->C1IMR1 |= (0X1<<8);
//使能98号中断
GICD->ISENABLER[3] |= (0X1<<2);
//设置98号中断的中断优先级
GICD->IPRIORITYR[24] &= (~(0X1F<<19));
//设置98号中断的目标处理器
GICD->ITARGETSR[24] &= (~(0X3<<16));
GICD->ITARGETSR[24] |= (0X1<<16);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= (0X1);
//设置中断优先级的掩码
GICC->PMR |= (0X1F<<3);
//全局使能组0中断被转发给处理器
GICC->CTRL |= (0X1);
}
key.h
cpp
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void key1_Init(void);
void key2_Init(void);
void key3_Init(void);
#endif
do_irq.c
cpp
#include "key.h"
#include "led.h"
#include "beep.h"
#include "fan.h"
#include "motor.h"
#include "delay.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
static int flag1=1;
static int flag2=1;
static int flag3=1;
//获取中断号
unsigned int irqnumb=(GICC->IAR&0X3FF);
switch (irqnumb)
{
case 99:{
//进行中断处理
if (flag1==1)
{
LED1_ON();
LED2_ON();
LED3_ON();
printf("LED1打开\n");
printf("LED2打开\n");
printf("LED3打开\n");
flag1=0;
}
else if (flag1==0)
{
LED1_OFF();
LED2_OFF();
LED3_OFF();
printf("LED1关闭\n");
printf("LED2关闭\n");
printf("LED3关闭\n");
flag1=1;
}
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0X1<<3);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<9);
break;
}
case 98:{
//进行中断处理
if (flag2==1)
{
BEEP_Open();
printf("BEEP打开\n");
flag2=0;
}
else if (flag2==0)
{
BEEP_Close();
printf("BEEP关闭\n");
flag2=1;
}
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0X1<<2);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<8);
break;
}
case 97:{
//进行中断处理
if (flag3==1)
{
Fan_Open();
Motor_Open();
printf("FAN打开\n");
printf("Motor打开\n");
flag3=0;
}
else if (flag3==0)
{
Fan_Close();
Motor_Close();
printf("FAN关闭\n");
printf("Motor关闭\n");
flag3=1;
}
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0X1<<1);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<7);
break;
}
}
//清除IAR保存的中断号
GICC->EOIR |= irqnumb;
}