9.17 实现KEY1,KEY2,KEY3三个按键按下打印信息

发布于:2022-12-14 ⋅ 阅读:(364) ⋅ 点赞:(0)

在这里插入图片描述

代码示例

key.h

#include "stm32mp1xx_exti.h"           
#include "stm32mp1xx_gpio.h"           
#include "stm32mp1xx_rcc.h"            
#include "stm32mp1xx_gic.h"            
/********PF9----KEY1***********/       
//初始化EXIT层                         
void pf9_exti_init();                  
//初始化GICD层                         
void pf9_gicd_init();                  
//初始化GICC层                         
void gicc_init();                      
                                       
//初始化EXIT层                         
void pf8_exti_init();                  
//初始化GICD层                         
void pf8_gicd_init();                  
                                       
//初始化EXIT层                         
void pf7_exti_init();                  
//初始化GICD层                         
void pf7_gicd_init();                                                                                              
#endif                                 

key.c

#include "key.h"
/********PF9----KEY1***********/
//初始化EXIT层
void pf9_exti_init()
{
	/******RCC章节初始化****/
	//1.设置GPIOF时钟使能
	RCC->MP_AHB4ENSETR |= (0X1<<5);
	/******GPIO章节初始化****/
	//1.设置PF9引脚输入模式
	GPIOF->MODER &= (~(0X3<<18));
	/******EXIT章节初始化****/
	//1.设置PF9引脚和EXTI9控制器进行连接
	EXTI->EXTICR3 |= (0X05<<8);
	//2.设置PF9引脚检测方式下降沿检测
	EXTI->FTSR1 |= (0X1<<9);
	//3.设置PF9引脚中断不屏蔽
	EXTI->C1IMR1 |= (0X1<<9);
}
void pf8_exti_init()
{
	GPIOF->MODER &= (~(0X3<<16)); 
	//1.设置PF8引脚和EXTI8控制器进行连接
	EXTI->EXTICR3 |= (0X05<<0);
	//2.设置PF8引脚检测方式下降沿检测
	EXTI->FTSR1 |= (0X1<<8);
	//3.设置PF8引脚中断不屏蔽
	EXTI->C1IMR1 |= (0X1<<8);
}
void pf7_exti_init()
{
    GPIOF->MODER &= (~(0X3<<14));        
    //1.设置PF7引脚和EXTI7控制器进行连接 
    EXTI->EXTICR2 |= (0X05<<24);          
    //2.设置PF7引脚检测方式下降沿检测    
    EXTI->FTSR1 |= (0X1<<7);             
    //3.设置PF7引脚中断不屏蔽            
    EXTI->C1IMR1 |= (0X1<<7);            
}

//初始化GCID层
void pf9_gicd_init()
{
	//1.设置GICD层全局中断使能
	GICD->CTLR |= 0X1;
	//2.设置GICD层中断使能寄存器
	GICD->ISENABLER[3] |= (0X1<<3);
	//3.设置GICD层中断优先级寄存器
	GICD->IPRIORITYR[24] |= (0X1<<27); 
	//4.设置GICD层中断目标分配
	GICD->ITARGETSR[24] |= (0X1<<24);
}
void pf8_gicd_init()
{
	//1.设置GICD层全局中断使能                                
	//2.设置GICD层中断使能寄存器          
	GICD->ISENABLER[3] |= (0X1<<2);       
	//3.设置GICD层中断优先级寄存器        
	GICD->IPRIORITYR[24] |= (0X1<<19);    
	//4.设置GICD层中断目标分配            
	GICD->ITARGETSR[24] |= (0X1<<16);     
}
void pf7_gicd_init()
{
    //1.设置GICD层全局中断使能                       
    //2.设置GICD层中断使能寄存器       
    GICD->ISENABLER[3] |= (0X1<<1);    
    //3.设置GICD层中断优先级寄存器     
    GICD->IPRIORITYR[24] |= (0X1<<11); 
    //4.设置GICD层中断目标分配         
    GICD->ITARGETSR[24] |= (0X1<<8);  
}

//初始化GICC层
void gicc_init()
{
	//1.设置GICC层全局中断使能寄存器
	GICC->CTLR |= 0X1;
	//2.设置GICC层中断优先级寄存器
	GICC->PMR |= (0X5<<3);
}

do_irq.c

#include "key.h"
extern void printf(const char *fmt, ...);
extern void delay_ms(int ms);
void do_irq(void) 
{
	unsigned int num = 0;
	//获取中断号
	num = GICC->IAR & 0X3FF;
	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));
	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++);
}
int main()
{
	//1.EXTI初始化
	pf9_exti_init();
	//2.GICD初始化
	pf9_gicd_init();
	//初始化EXIT层         
	pf8_exti_init();  
	//初始化GICD层         
	pf8_gicd_init();  
	//初始化EXIT层        
	pf7_exti_init(); 
	//初始化GICD层        
	pf7_gicd_init(); 

	//3.GICC初始化
	gicc_init();   

	while(1)
	{
	}
	return 0;
}

测试结果

在这里插入图片描述

本文含有隐藏内容,请 开通VIP 后查看