07_cortex-A7核按键中断实验

07_cortex-A7核按键中断实验

  • 实验内容
    • 代码实现
      • key5.h
      • main.c
      • key.c
      • do_irq.c
    • 实验现象

实验内容

07_cortex-A7核按键中断实验_第1张图片

代码实现

key5.h

#ifndef __KEY_H__
#define __KEY_H__

#include"stm32mp1xx_rcc.h" 
#include "stm32mp1xx_gic.h" 
#include"stm32mp1xx_gpio.h" 
#include "stm32mp1xx_exti.h"


//初始化EXTI层
void pf9_exti_init();
//初始化GICD层
void pf9_gicd_init();
//初始化GICC层
void pf9_gicc_init();



#endif

main.c

#include "key5.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()
{
	//调用初始化函数
	pf9_exti_init();
	pf9_gicd_init();
	pf9_gicc_init();
	while(1)
	{
	
	}
	return 0;
}

key.c

#include "key5.h"
void pf9_exti_init()
{
	//1.设置GPIOF组始终使能
	RCC->MP_AHB4ENSETR |= (0x1<<5);
	//设置PF9引脚为输入模式
	GPIOF->MODER &=(~(0x3f<<14));
	//设置PF9引脚和EXTI9控制器进行连接
	EXTI->EXTICR3 &= (~(0xff<<8));
	EXTI->EXTICR3 |= (0x05)<<8;

	EXTI->EXTICR2 &= (~(0xff<<24));
	EXTI->EXTICR2 |= (0x05)<<24;

	EXTI->EXTICR3 &= (~(0xff));
	EXTI->EXTICR3 |= (0x05);
	//设置PF9引脚检测方式为下降沿
	EXTI->FTSR1 |= (0x7<<7);
	//设置PF9引脚中断不屏蔽
	EXTI->C1IMR1 |= (0x7<<7);
	//
//	EXTI->FPR1 &= 0x7<<7;
	
}
//初始化GICD层
void pf9_gicd_init()
{
	//设置GICD层全局中断使能寄存器
	GICD->CTRL |= 0x1;
	//设置GICD层中断使能寄存器
	GICD->ISENABLER[3] |= (0x7<<1);
	//中断优先级设置
	GICD->IPRIORITYR[24] &= (~(0X1F <<11));
	GICD->IPRIORITYR[24] |= (0X1 <<11);
	GICD->IPRIORITYR[24] &= (~(0X1F <<19));
	GICD->IPRIORITYR[24] |= (0X1 <<19);
	GICD->IPRIORITYR[24] &= (~(0X1F <<27));
	GICD->IPRIORITYR[24] |= (0X1 <<27);
	//选择CPU,0号,设置中断目标分配
	GICD->ITARGETSR[24] &= (~(0X3 <<8));
	GICD->ITARGETSR[24] |= (0X1 <<8);
	GICD->ITARGETSR[24] &= (~(0X3 <<16));
	GICD->ITARGETSR[24] |= (0X1 <<16);
	GICD->ITARGETSR[24] &= (~(0X3 <<24));
	GICD->ITARGETSR[24] |= (0X1 <<24);
/*
	//设置GICD层中断优先级寄存器
	GICD->IPRIORITYR[24] &= 0x3<<27;
	GICD->IPRIORITYR[24] &= 0x3<<11;
	GICD->IPRIORITYR[24] &= 0x3<<19;

	
	//设置GICD层中断目标分配寄存器
	GICD->ITARGETSR[24] &= 0x1<<24;
	GICD->ITARGETSR[24] &= 0x1<<8;
	GICD->ITARGETSR[24] &= 0x1<<16;
	//
	//GICD->ICPENDR[3] &= 0x7<<1;
*/
}
//初始化GICC层
void pf9_gicc_init()
{
	GICC->CTRL |= 0x1;

	GICC->PMR |= 0x7<<3;
}

do_irq.c

#include "key5.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
extern void delay_ms(int ms);
void do_irq(void) 
{
	unsigned int num = 0;
	//获取中断号
	num = GICC->IAR & (0x1ff);
	switch(num)
	{
		case 97:
			
			
				delay_ms(500);
				printf("key2######\n");
				EXTI->FPR1 &= 0x7<<7;
				GICD->ICPENDR[3] &= 0x7<<1;
			
			break;
		case 98:
			
			
				delay_ms(500);
				printf("key3######\n");
				EXTI->FPR1 &= 0x7<<7;
				GICD->ICPENDR[3] &= 0x7<<1;
			
			
			
			break;
		case 99:
			
				delay_ms(500);
				printf("key1######\n");
				EXTI->FPR1 &= 0x7<<7;
				GICD->ICPENDR[3] &= 0x7<<1;
			
				break;
	}

//清除中断号
	GICC->EOIR &= (~(0x3ff));
	GICC->EOIR |= num;
 
}

实验现象

07_cortex-A7核按键中断实验_第2张图片

你可能感兴趣的:(单片机,stm32,嵌入式硬件)