key.h
#include "key.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
int irqno;//保存中断号
irqno=GICC->IAR & 0X3FF;
switch(irqno)
{
case 99://key1
if(GPIOE->ODR & (0x1<<10) != 0)
GPIOE->ODR &= (~(0x1<<10));
else
GPIOE->ODR |=(0x1<<10);
GICD->ICPENDR[3] |= (0x1<<3);
EXTI->FPR1 |= (0x1<<9);
break;
case 97://key2
GPIOB->ODR |= (0x1<<6);
GICD->ICPENDR[3] |= (0x1<<1);
EXTI->FPR1 |= (0x1<<7);
break;
case 98://key3
GPIOE->ODR |= (0x1<<9);
GICD->ICPENDR[3] |= (0x1<<2);
EXTI->FPR1 |= (0x1<<8);
}
//清除处理完的中断号GICC_EOIR
GICC->EOIR =irqno;
}
key.h
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void mykey();
void key3_init();
void key12_init();
#endif
key.c
#include "key.h"
void mykey()
{
RCC->MP_AHB4ENSETR |= (0X3<<4);
RCC->MP_AHB4ENSETR |= (0x1<<1);
//led1灯初始
GPIOE->MODER &= (~(0X3<<20));
GPIOE->MODER |= (0x1<<20);
GPIOE->OTYPER &= (~(0X1<<10));
GPIOE->OSPEEDR &= (~(0X3<<20));
GPIOE->PUPDR &= (~(0X3<<20));
GPIOE->ODR |=(0x1<<10);
//蜂鸣器
GPIOB->MODER &= (~(0x1<<12));
GPIOB->MODER |= (0x1<<12);
GPIOB->OTYPER &= (~(0x1<<6));
GPIOB->OSPEEDR |= (~(0x3<<12));
GPIOB->PUPDR &= (~(0x3<<12));
GPIOB->ODR |= (~(0x1<<6));
//风扇
GPIOE->MODER &= (~(0x1<<18));
GPIOE->MODER |= (0x1<<18);
GPIOE->OTYPER &= (~(0x1<<9));
GPIOE->OSPEEDR |= (~(0x3<<18));
GPIOE->PUPDR &= (~(0x3<<18));
GPIOE->ODR &= (~(0x1<<9));
}
void key3_init()
{
//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 key12_init()
{
RCC->MP_AHB4ENSETR |= (0x1<<5);
GPIOF->MODER &= (~(0x3<<18));
GPIOF->MODER &= (~(0x3<<14));
EXTI->EXTICR2 &=(~(0xFF<<24));
EXTI->EXTICR2 |= (0x5<<24);
EXTI->EXTICR3 &=(~(0xFF<<8));
EXTI->EXTICR3 |= (0x5<<8);
EXTI->FTSR1 |= (0x1<<7);
EXTI->FTSR1 |= (0x1<<9);
EXTI->C1IMR1 |= (0x1<<7);
EXTI->C1IMR1 |= (0x1<<9);
GICD->ISENABLER[3] |= (0X1<<1);
GICD->ISENABLER[3] |= (0X1<<3);
GICD->IPRIORITYR[24] &= (~(0X1F<<11));
GICD->IPRIORITYR[24] &= (~(0X1F<<27));
GICD->ITARGETSR[24] &= (~(0X3<<8));
GICD->ITARGETSR[24] |= (0X1<<8);
GICD->ITARGETSR[24] &= (~(0X3<<24));
GICD->ITARGETSR[24] |= (0X1<<24);
GICD->CTRL |= 0X1;
GICC->PMR |= (0x1F<<3);
GICC->CTRL |= 0X1;
}
main.c
#include "key.h"
int main()
{
mykey();
key12_init();
key3_init();
for(;;);
return 0;
}