1、key.h——头文件的包络和函数的声明
#ifndef __KEY_H__
#define __KEY_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_gpio.h"
//初始化EXTI层——key1
void pf9_exti_init();
//初始化GICD层——key1
void pf9_gicd_init();
//初始化GICC层——key1
void pf9_gicc_init();
//初始化EXTI层——key3
void pf8_exti_init();
//初始化GICD层——key3
void pf8_gicd_init();
//初始化GICC层——key3
void pf8_gicc_init();
//初始化EXTI层——key2
void pf7_exti_init();
//初始化GICD层——key2
void pf7_gicd_init();
//初始化GICC层——key2
void pf7_gicc_init();
#endif
2、key.c——功能函数的定义(以key1为例)
1)RCC章节初始化
//1.设置GPIOF组时钟使能
RCC->MP_AHB4ENSETR |= (0x1 << 5);
2)GPIO章节初始化
//1.设置PF9引脚为输入模式
GPIOF->MODER &= (~(0x3 << 18));
3)EXTI章节初始化
3.1设置PF9引脚和EXIT9控制器进行连接
EXTI->EXTICR3 &= (~(0xff << 8));
EXTI->EXTICR3 |= (0x5 << 8);
3.2设置PF9引脚检测方式为下降沿
EXTI->FTSR1 |= (0x1 << 9);
3.3设置PF9引脚中断不屏蔽
EXTI->C1IMR1 |= (0x1 << 9);
4)初始化GICD层
4.1设置GICD层全局中断使能寄存器
GICD->CTRL |= (0x1 << 0);
4.2设置GICD层中断使能寄存器
GICD->ISENABLER[3] |= (0x1 << 3);
4.3设置GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &= (~(0x1f << 27));
GICD->IPRIORITYR[24] |= (0x1 << 27);
4.4设置GICD层中断目标分配寄存器
GICD->ITARGETSR[24] |= (0x3 << 24);
5)初始化GICC层
5.1设置GICC层全局中断使能寄存器
GICC->CTRL |= (0x1 << 0);
5.2设置GICC层中断优先级寄存器
GICC->PMR &= (~(0x1f << 3));
GICC->PMR |= (0x9 << 3);
6)完整代码
#include "key.h"
//初始化EXTI层——key1
void pf9_exti_init()
{
/****RCC章节初始化*******/
//1.设置GPIOF组时钟使能
RCC->MP_AHB4ENSETR |= (0x1 << 5);
/****GPIO章节初始化*******/
//1.设置PF9引脚为输入模式
GPIOF->MODER &= (~(0x3 << 18));
/*****EXTI章节初始化******/
//1.设置PF9引脚和EXIT9控制器进行连接
EXTI->EXTICR3 &= (~(0xff << 8));
EXTI->EXTICR3 |= (0x5 << 8);
//2.设置PF9引脚检测方式下降沿
EXTI->FTSR1 |= (0x1 << 9);
//3.设置PF9引脚中断不屏蔽
EXTI->C1IMR1 |= (0x1 << 9);
}
//初始化GICD层——key1
void pf9_gicd_init()
{
//1.设置GICD层全局中断使能寄存器
GICD->CTRL |= (0x1 << 0);
//2.设置GICD层中断使能寄存器
GICD->ISENABLER[3] |= (0x1 << 3);
//3.设置GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &= (~(0x1f << 27));
GICD->IPRIORITYR[24] |= (0x1 << 27);
//4.设置GICD层中断目标分配寄存器
GICD->ITARGETSR[24] |= (0x3 << 24);
}
//初始化GICC层——key1
void pf9_gicc_init()
{
//1.设置GICC层全局中断使能寄存器
GICC->CTRL |= (0x1 << 0);
//2.设置GICC层中断优先级寄存器
GICC->PMR &= (~(0x1f << 3));
GICC->PMR |= (0x9 << 3);
}
//初始化EXTI层——key3
void pf8_exti_init()
{
/****GPIO章节初始化*******/
//1.设置PF8引脚为输入模式
GPIOF->MODER &= (~(0x3 << 16));
/*****EXTI章节初始化******/
//1.设置PF8引脚和EXIT8控制器进行连接
EXTI->EXTICR3 &= (~(0xff << 0));
EXTI->EXTICR3 |= (0x5 << 0);
//2.设置PF8引脚检测方式下降沿
EXTI->FTSR1 |= (0x1 << 8);
//3.设置PF8引脚中断不屏蔽
EXTI->C1IMR1 |= (0x1 << 8);
}
//初始化GICD层——key3
void pf8_gicd_init()
{
//1.设置GICD层全局中断使能寄存器
GICD->CTRL |= (0x1 << 0);
//2.设置GICD层中断使能寄存器
GICD->ISENABLER[3] |= (0x1 << 2);
//3.设置GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &= (~(0x1f << 19));
GICD->IPRIORITYR[24] |= (0x3 << 19);
//4.设置GICD层中断目标分配寄存器
GICD->ITARGETSR[24] |= (0x3 << 16);
}
//初始化GICC层——key3
void pf8_gicc_init()
{
//1.设置GICC层全局中断使能寄存器
GICC->CTRL |= (0x1 << 0);
//2.设置GICC层中断优先级寄存器
GICC->PMR &= (~(0x1f << 3));
GICC->PMR |= (0x8 << 3);
}
//初始化EXTI层——key2
void pf7_exti_init()
{
/****GPIO章节初始化*******/
//1.设置PF7引脚为输入模式
GPIOF->MODER &= (~(0x3 << 14));
/*****EXTI章节初始化******/
//1.设置PF7引脚和EXIT7控制器进行连接
EXTI->EXTICR2 &= (~(0xff << 24));
EXTI->EXTICR2 |= (0x5 << 24);
//2.设置PF7引脚检测方式下降沿
EXTI->FTSR1 |= (0x1 << 7);
//3.设置PF7引脚中断不屏蔽
EXTI->C1IMR1 |= (0x1 << 7);
}
//初始化GICD层——key2
void pf7_gicd_init()
{
//1.设置GICD层全局中断使能寄存器
GICD->CTRL |= (0x1 << 0);
//2.设置GICD层中断使能寄存器
GICD->ISENABLER[3] |= (0x1 << 1);
//3.设置GICD层中断优先级寄存器
GICD->IPRIORITYR[24] &= (~(0x1f << 11));
GICD->IPRIORITYR[24] |= (0x2 << 11);
//4.设置GICD层中断目标分配寄存器
GICD->ITARGETSR[24] |= (0x3 << 8);
}
//初始化GICC层——key2
void pf7_gicc_init()
{
//1.设置GICC层全局中断使能寄存器
GICC->CTRL |= (0x1 << 0);
//2.设置GICC层中断优先级寄存器
GICC->PMR &= (~(0x1f << 3));
GICC->PMR |= (0x7 << 3);
}
3、do_irq.c——中断处理函数
使用延时函数进行去抖
#include "key.h"
extern void printf(const char *fmt, ...);
extern void delay_ms(int ms);
unsigned int i = 0;
void do_irq(void)
{
unsigned int num = 0;
//获取中断号
num = GICC->IAR;
switch(num)
{
case 97:
delay_ms(500);
printf("key2#####\n");
//清除EXTI层中断挂起标志位
EXTI->FPR1 |= (0x1 << 7);
//清除GICD层中断挂起标志位
GICD->ICPENDR[3] |= (0x1 << 1);
break;
case 98:
delay_ms(500);
printf("key3#####\n");
//清除EXTI层中断挂起标志位
EXTI->FPR1 |= (0x1 << 8);
//清除GICD层中断挂起标志位
GICD->ICPENDR[3] |= (0x1 << 2);
break;
case 99:
delay_ms(500);
printf("key1#####\n");
//清除EXTI层中断挂起标志位
EXTI->FPR1 |= (0x1 << 9);
//清除GICD层中断挂起标志位
GICD->ICPENDR[3] |= (0x1 << 3);
break;
}
//清除中断号
GICC->EOIR &= (0x3ff << 0);
GICC->EOIR |= num;
}
4、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++);
}
int main()
{
/******key1初始化*****/
//初始化EXTI层
pf9_exti_init();
//初始化GICD层
pf9_gicd_init();
//初始化GICC层
pf9_gicc_init();
/******key3初始化*****/
//初始化EXTI层
pf8_exti_init();
//初始化GICD层
pf8_gicd_init();
//初始化GICC层
pf8_gicc_init();
/******key3初始化*****/
//初始化EXTI层
pf7_exti_init();
//初始化GICD层
pf7_gicd_init();
//初始化GICC层
pf7_gicc_init();
while(1)
{
}
return 0;
}
5、实验现象
?
|