实现三个按键中断,按键按下时,LED灯亮
mykey.h
#ifndef __MYKEY_H__
#define __MYKEY_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
void key1_int_config();
void key2_int_config();
void key3_int_config();
void led_init();
void LED1_CTRL(int flag);
void LED2_CTRL(int flag);
void LED3_CTRL(int flag);
#endif
mykey.c
#include "mykey.h"
void key1_int_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF9为输入
GPIOF->MODER &= (~(0x3<<18));
//设置PF9产生EXTI9事件
EXTI->EXTICR3 &= (~(0xff<<8));
EXTI->EXTICR3 |= (0x5<<8);
//设置下降沿检测PF9事件
EXTI->FTSR1 |= (0x1<<9);
//取消EXTI9事件的屏蔽
EXTI->C1IMR1 |= (0x1<<9);
//全局使能99号中断
GICD->ISENABLER[3] |= (0x1<<3);
//设置99号中断的优先级
GICD->IPRIORITYR[24] &= (~(0x1f<<27)); //00000
//设置99号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<24); //交给处理器0处理
//允许组0中断被转发到GICC层
GICD->CTRL |= 0x1;
//设置中断优先级掩码
GICC->PMR |= (0x1f<<3); //掩码为11111
//允许组0中断被转发给处理器
GICC->CTRL |= 0x1;
}
void key2_int_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF8为输入
GPIOF->MODER &= (~(0x3<<16));
//设置PF8产生EXTI8事件
EXTI->EXTICR3 &= (~(0xff));
EXTI->EXTICR3 |= (0x5);
//设置下降沿检测PF8事件
EXTI->FTSR1 |= (0x1<<8);
//取消EXTI8事件的屏蔽
EXTI->C1IMR1 |= (0x1<<8);
//全局使能98号中断
GICD->ISENABLER[3] |= (0x1<<2);
//设置98号中断的优先级
GICD->IPRIORITYR[24] &= (~(0x1f)<<19); //00000
//设置98号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<16); //交给处理器0处理
//允许组0中断被转发到GICC层
GICD->CTRL |= 0x1;
//设置中断优先级掩码
GICC->PMR |= (0x1f<<3); //掩码为11111
//允许组0中断被转发给处理器
GICC->CTRL |= 0x1;
}
void key3_int_config()
{
//使能GPIOF外设时钟
RCC->MP_AHB4ENSETR |= (0x1<<5);
//设置PF7为输入
GPIOF->MODER &= (~(0x3<<14));
//设置PF7产生EXTI7事件
EXTI->EXTICR2 &= (~(0xff<<24));
EXTI->EXTICR2 |= (0x5<<24);
//设置下降沿检测PF7事件
EXTI->FTSR1 |= (0x1<<7);
//取消EXTI7事件的屏蔽
EXTI->C1IMR1 |= (0x1<<7);
//全局使能97号中断
GICD->ISENABLER[3] |= (0x1<<1);
//设置97号中断的优先级
GICD->IPRIORITYR[24] &= (~(0x1f)<<11); //00000
//设置97号中断的目标处理器
GICD->ITARGETSR[24] |= (0x1<<8); //交给处理器0处理
//允许组0中断被转发到GICC层
GICD->CTRL |= 0x1;
//设置中断优先级掩码
GICC->PMR |= (0x1f<<3); //掩码为11111
//允许组0中断被转发给处理器
GICC->CTRL |= 0x1;
}
main.c
#include "mykey.h"
//手动封装一个延时函数
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{
}
}
}
int main()
{
//按键中断初始化
key1_int_config();
key2_int_config();
key3_int_config();
//led灯初始化
led_init();
while(1)
{
}
return 0;
}
do_irq.c
#include "mykey.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:
// 中断处理逻辑
LED1_CTRL(1);
LED2_CTRL(0);
LED3_CTRL(0);
// 清除GICD的中断标志
GICD->ICPENDR[3] |= (0x1 << 3);
// 清除EXTI中的中断触发标志
EXTI->FPR1 |= (0x1 << 9);
break;
case 98:
// 中断处理逻辑
LED1_CTRL(0);
LED2_CTRL(1);
LED3_CTRL(0);
// 清除GICD的中断标志
GICD->ICPENDR[3] |= (0x1 << 2);
// 清除EXTI中的中断触发标志
EXTI->FPR1 |= (0x1 << 8);
break;
case 97:
// 中断处理逻辑
LED1_CTRL(0);
LED2_CTRL(0);
LED3_CTRL(1);
// 清除GICD的中断标志
GICD->ICPENDR[3] |= (0x1 << 1);
// 清除EXTI中的中断触发标志
EXTI->FPR1 |= (0x1 << 7);
break;
default:
break;
}
// 清除IAR寄存器中保存的中断号
GICC->EOIR = irqno;
}
文章来源地址https://www.toymoban.com/news/detail-855694.html
文章来源:https://www.toymoban.com/news/detail-855694.html
到了这里,关于ARM_day7:按键中断的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!