1.实验目的:实现KEY1/LEY2/KE3三个按键,中断触发打印一句话,并且灯的状态取反;
key1 ----> LED3灯状态取反;
key2 ----> LED2灯状态取反;
key3 ----> LED1灯状态取反;
2.分析框图:
3.代码:
---key.h头文件---
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
typedef enum
{
FALLING_TRI, //下降沿触发方式
RISING_TRI, //上升沿触发方式
}trigger_t;
//中断RCC章节初始化 PF9 7 8
void hal_interrupt_rcc_init();
//EXTI初始化函数
//EXTI编号 GPIO组号 触发方式
void hal_exti_init(unsigned int extix,unsigned int groupx,trigger_t trix);
//GIC初始化函数
//中断号,中断优先级
void hal_gic_init(unsigned int interruptx,unsigned int priority);
//led初始化函数
void led_init(gpio_t* gpiox,unsigned int pin);
#endif
---key.c函数实现---
#include "key.h"
//中断RCC章节初始化 PF9 7 8
void hal_interrupt_rcc_init()
{
//RCC章节初始化 PF9 [5]=1
RCC->MP_AHB4ENSETR |= (0x1 << 5);
}
//EXTI初始化函数
//EXTI编号 GPIO组号 触发方式
void hal_exti_init(unsigned int extix,unsigned int groupx,trigger_t trix)
{
//GPIO章节初始化 设置三个按键为输入模式
GPIOF->MODER &= (~(0x3 << 2*extix));
//EXTI章节初始化,引脚信号与EXTI连接
//1.EXIT_EXTICR组 0x05
EXTI->EXTICR[extix/4] &= (~(0xff << ((extix%4)*8)));
EXTI->EXTICR[extix/4] |= (groupx << ((extix%4)*8));
//2.EXTI_FTSR1 设置下降沿触发方式
if(trix == FALLING_TRI)
EXTI->FTSR1 |= (0x1 << extix);
else
EXTI->RTSR1 |= (0x1 << extix);
//3.EXTI_IMR1 设置中断不屏蔽 =1
EXTI->C1IMR1 |= (0x1 << extix);
}
//GIC初始化函数
//中断号,中断优先级
void hal_gic_init(unsigned int interruptx,unsigned int priority)
{
//GICD初始化
//1.GICD_CTRL 使能组0 [0]=1
GICD->CTRL |= (0x1 << 0);
//2.GICD_ISENABLER组 设置GICD层中断使能
GICD->ISENABLER[interruptx/32] |= (0x1 << (interruptx % 32));
//3.GICD_IPRIORITYR组 设置中断优先级 操作高5位
GICD->IPRIORITYR[interruptx/4] &= (~(0x1f << (interruptx % 4 * 8 + 3)));
GICD->IPRIORITYR[interruptx/4] |= (priority << (interruptx % 4 * 8 + 3));
//4.GICD_ITARGETSR组 设置中断目标分配给CPU0
GICD->ITARGETSR[interruptx/4] &= (~(0x3 << (interruptx % 4 * 8)));
GICD->ITARGETSR[interruptx/4] |= (0x1 << (interruptx % 4 * 8));
//GICC初始化
//1.GICC_CTRL 设置GICC层组0使能 [0]=1
GICC->CTRL |= (0x1 << 0);
//2.GICC_PMR 设置GICC层中断优先级
GICC->PMR |= ((priority+1) << 3);
}
//led灯初始化
//参数1:GPIO组号
//参数2:引脚编号
void led_init(gpio_t* gpiox,unsigned int pin)
{
//1输出模式
gpiox->MODER &= ~(0x3 << (pin*2));
gpiox->MODER |= (0x1 << (pin*2));
//2输出类型
gpiox->OTYPER &= (~(0x1 << pin));
//3输出速率
gpiox->OSPEEDR &= ~(0x3 << (pin*2));
//4拉电阻
gpiox->PUPDR &= ~(0x3 << (pin*2));
}
---do_irq.c终端处理函数---
#include "key.h"
extern void printf(const char *fmt, ...);
//中断处理函数
void do_irq(void)
{
//1获取中断号 IAR[9:0]
unsigned int num = 0;
num = GICC->IAR & 0x3ff;
//2判断中断号,并且打印一句话
switch(num)
{
case 99:
printf("KEY1按键中断,LED3灯取反\n");
//led3灯状态取反
GPIOE->ODR ^= (0x1 << 8);
//3清除EXTI层中断挂起标志位 [9]=1
//EXTI_FDR1 [9]=1
EXTI->FPR1 |= (0x1 << 9);
//4清除GICD层中断挂起标志位
//GICD_ICPENDR3 [3]=1
GICD->ICPENDR[3] |= (0x1 << 3);
break;
case 97:
printf("KEY2按键中断,LED2灯取反\n");
//led2灯状态取反
GPIOF->ODR ^= (0x1 << 10);
//清除EXTI层中断挂起标志位 [9]=1
EXTI->FPR1 |= (0x1 << 7);
GICD->ICPENDR[3] |= (0x1 << 1);
break;
case 98:
printf("KEY3按键中断,LED1灯取反\n");
//led1灯状态取反
GPIOE->ODR ^= (0x1 << 10);
EXTI->FPR1 |= (0x1 << 8);
GICD->ICPENDR[3] |= (0x1 << 2);
break;
}
//5清楚获取到的中断号
GICC->EOIR = num;
}
---main.c测试文件---
#include "key.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 < 1800; j++);
}
#define GPIO_PIN10 10 //引脚编号
#define GPIO_PIN8 8
#define EXTI9 9 //EXTI编号
#define EXTI7 7
#define EXTI8 8
#define GPIOF_GROUP 0x05 //GPIOF组号
#define KEY1_ID 99 //中断号
#define KEY2_ID 97
#define KEY3_ID 98
#define KEY1_PRIORITY 9 //中断优先级
#define KEY2_PRIORITY 7
#define KEY3_PRIORITY 8
int main()
{
//使能三盏灯PE10 PF10 PE8
RCC->MP_AHB4ENSETR |= (0x3 << 4);
//led初始化函数
led_init(GPIOE,GPIO_PIN10);
led_init(GPIOF,GPIO_PIN10);
led_init(GPIOE,GPIO_PIN8);
//中断RCC章节初始化 PF9 7 8
hal_interrupt_rcc_init();
//EXTI初始化函数
hal_exti_init(EXTI9,GPIOF_GROUP,FALLING_TRI);
hal_exti_init(EXTI7,GPIOF_GROUP,FALLING_TRI);
hal_exti_init(EXTI8,GPIOF_GROUP,FALLING_TRI);
//GIC初始化函数
hal_gic_init(KEY1_ID,KEY1_PRIORITY);
hal_gic_init(KEY2_ID,KEY2_PRIORITY);
hal_gic_init(KEY3_ID,KEY3_PRIORITY);
while(1)
{
}
return 0;
}
4.实验现象:
文章来源:https://www.toymoban.com/news/detail-674628.html
文章来源地址https://www.toymoban.com/news/detail-674628.html
到了这里,关于ARM开发,stm32mp157a-A7核中断实验(实现按键中断功能)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!