实现三个按键中断代码
按键1中断控制LED1
按键2中断控制LED2
按键3中断控制LED3
key_it.h:
cpp
#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_exti.h"
#include"stm32mp1xx_gic.h"
void key1_config();
void key2_config();
void key3_config();
void do_irq(void);
#endif
led.h:
cpp
#ifndef __LED_H__
#define __LED_H__
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_rcc.h"
#include"stm32mp1xx_exti.h"
#include"stm32mp1xx_gic.h"
// typedef struct{
// unsigned int MODER;
// unsigned int OTYPER;
// unsigned int OSPEEDR;
// unsigned int PUPDR;
// unsigned int IDR;
// unsigned int ODR;
// unsigned int BSRR;
// }gpio_t;
// #define GPIOE ((gpio_t *)0x50006000)
// #define GPIOF ((gpio_t *)0x50007000)
void led1_init();
void led2_init();
void led3_init();
void led1_ctl(int flag);
void led2_ctl(int flag);
void led3_ctl(int flag);
#endif
do_irq.c:
cpp
#include"key_it.h"
#include"led.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
//先获取中断号
unsigned int irqno=(GICC->IAR &0x3ff);
switch(irqno)
{
case 99://按键1中断
//进行中断处理
printf("do_key1_int\n");
if((GPIOE->ODR &=(0x1<<10)) == 0 )
{
led1_ctl(1);
}else
led1_ctl(0);
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0x1<<3);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<9);
break;
case 97://按键2中断
//进行中断处理
printf("do_key2_int\n");
if((GPIOF->ODR &=(0x1<<10)) == 0 )
{
led2_ctl(1);
}else
led2_ctl(0);
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0x1<<1);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<7);
break;
case 98://按键3中断
//进行中断处理
printf("do_key3_int\n");
if((GPIOE->ODR &=(0x1<<8)) == 0 )
{
led3_ctl(1);
}else
led3_ctl(0);
//清除GICD中的中断排队标志
GICD->ICPENDR[3] |= (0x1<<2);
//清除EXTI中的事件挂起标志
EXTI->FPR1 |= (0x1<<8);
break;
}
//清除IAR保存的中断号
GICC->EOIR = irqno;
}
key_it.c:
cpp
#include"key_it.h"
//按键1中断的配置
void key1_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF9设置为输入
GPIOF->MODER &= (~(0x3<<18));
//设置PF9生成EXTI9事件
EXTI->EXTICR3 &= (~(0xff<<8));
EXTI->EXTICR3 |= (0x05<<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] |= (0x1<<24);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= 0X1;
//设置中断的优先级掩码
GICC->PMR |= (0x1f<<3);
//全局使能组0中断被转发给处理器
GICC->CTRL |= (0x1);
}
//按键2中断的配置
void key2_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF7设置为输入
GPIOF->MODER &= (~(0x3<<14));
//设置PF7生成EXTI7事件
EXTI->EXTICR2 &= (~(0xff<<24));
EXTI->EXTICR2 |= (0x05<<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] |= (0x1<<8);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= 0X1;
//设置中断的优先级掩码
GICC->PMR |= (0x1f<<3);
//全局使能组0中断被转发给处理器
GICC->CTRL |= (0x1);
}
//按键3中断的配置
void key3_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//将PF8设置为输入
GPIOF->MODER &= (~(0x3<<16));
//设置PF8生成EXTI8事件
EXTI->EXTICR3 &= (~0xff);
EXTI->EXTICR3 |= (0x05);
//设置下降沿检测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] |= (0x1<<16);
//全局使能组0中断被转发到GICC层
GICD->CTRL |= 0X1;
//设置中断的优先级掩码
GICC->PMR |= (0x1f<<3);
//全局使能组0中断被转发给处理器
GICC->CTRL |= (0x1);
}
led.c:
cpp
#include "led.h"
void led1_init()
{
// 设置PE10为输出模式
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |= (0x1 << 20);
// 设置PE10为推挽输出
GPIOE->OTYPER &= (~(0x1 << 10));
// 设置PE10为低速输出
GPIOE->OSPEEDR &= (~(0x3 << 20));
// 设置PE10无上拉下拉
GPIOE->PUPDR &= (~(0x3 << 20));
}
void led2_init()
{
// 设置Pf10为输出模式
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
// 设置PF10为推挽输出
GPIOF->OTYPER &= (~(0x1 << 10));
// 设置PF10为低速输出
GPIOF->OSPEEDR &= (~(0x3 << 20));
// 设置PF10无上拉下拉
GPIOF->PUPDR &= (~(0x3 << 20));
}
void led3_init()
{
// 设置PE8为输出模式
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 16);
// 设置PE8为推挽输出
GPIOE->OTYPER &= (~(0x1 << 8));
// 设置PE8为低速输出
GPIOE->OSPEEDR &= (~(0x3 << 16));
// 设置PE8无上拉下拉
GPIOE->PUPDR &= (~(0x3 << 16));
}
//LED1控制
void led1_ctl(int flag)
{
if(flag==1)
{
GPIOE->ODR |= (0x1<<10);
}
else if(flag==0)
{
GPIOE->ODR &= (~(0x1<<10));
}
}
//LED2控制
void led2_ctl(int flag)
{
if(flag==1)
{
GPIOF->ODR |= (0x1<<10);
}
else if(flag==0)
{
GPIOF->ODR &= (~(0x1<<10));
}
}
//LED3控制
void led3_ctl(int flag)
{
if(flag==1)
{
GPIOE->ODR |= (0x1<<8);
}
else if(flag==0)
{
GPIOE->ODR &= (~(0x1<<8));
}
}
main.c:
cpp
#include"key_it.h"
#include"led.h"
extern void printf(const char *fmt, ...);
//手动封装一个延时函数
void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{
}
}
}
int main()
{
//使能GPIO的外设时钟
*((unsigned int *)0X50000A28) |= (0X3<<4);
led1_init();
led2_init();
led3_init();
key1_config();
key2_config();
key3_config();
while(1)
{
printf("do_main\n");
delay_ms(1000);
}
return 0;
}