10.4国庆作业(中断实验)

文章目录

  • 作业内容
  • 思路分析
    • 1. 电路图分析
    • 2. RCC章节分析
    • 3. GPIO章节分析
    • 4. EXTI章节分析
    • 5. GICD章节分析
    • 6. GICC章节分析
  • 代码实现
    • 1. key.h
    • 2. key.c
    • 3. do_irq.c
    • 4. main.c
  • 测试结果

作业内容

完成cortexA7核中断实验:实现按下任意按键,在串口工具上显示对应的按键编号

思路分析

1. 电路图分析

10.4国庆作业(中断实验)_第1张图片
10.4国庆作业(中断实验)_第2张图片
分析三个按键对应的GPIO接口:
KEY1----->PF9
KEY2----->PF7
KEY3----->PF8

2. RCC章节分析

设置GPIOF控制器使能工作:RCC_MP_AHB4ENSETR[5] = 1

3. GPIO章节分析

设置KEY1为输入模式:GPIOF_MODER[19:18] = 00
设置KEY2为输入模式:GPIOF_MODER[15:14] = 00
设置KEY3为输入模式:GPIOF_MODER[17:16] = 00

4. EXTI章节分析

  1. EXTI_EXTICRn(设置外部中断选择寄存器):
    PF9------>EXTI9-------->EXTI_EXTICR3[15:8] = 0x05
    PF7------>EXTI7-------->EXTI_EXTICR2[31:24] = 0x05
    PF8------>EXTI8-------->EXTI_EXTICR3[7:0] = 0x05
  2. EXTI_FTSR1(设置下降沿检测):
    PF9------>EXTI9—>EXTI_FTSR1[9] = 1
    PF7------>EXTI7—>EXTI_FTSR1[7] = 1
    PF8------>EXTI8—>EXTI_FTSR1[8] = 1
  3. EXTI_IMR1(设置中断不屏蔽):
    PF9------>EXTI9—>EXTI_IMR1[9] = 1
    PF7------>EXTI7—>EXTI_IMR1[7] = 1
    PF8------>EXTI8—>EXTI_IMR1[8] = 1
  4. EXTI_FPR1(清除中断挂起标志位):
    PF9------>EXTI9—>EXTI_FPR1[9] = 1
    PF7------>EXTI7—>EXTI_FPR1[7] = 1
    PF8------>EXTI8—>EXTI_FPR1[8] = 1

5. GICD章节分析

  1. 设置GICD层全局中断使能寄存器:GICD_CTLR[0] = 1
  2. 设置GICD层中断使能寄存器:
    PF9------>EXTI9—>中断号99—>GICD_ISENABLER3[3] = 1
    PF7------>EXTI7—>中断号97—>GICD_ISENABLER3[1] = 1
    PF8------>EXTI8—>中断号98—>GICD_ISENABLER3[2] = 1
  3. 设置GICD层中断优先级寄存器:
    PF9------>EXTI9—>中断号99—> 99 / 4 = 24 … 3—>GICD_IPRIORITYR24[31:27]
    PF7------>EXTI7—>中断号97—> 97 / 4 = 24 … 1—>GICD_IPRIORITYR24[15:11]
    PF8------>EXTI8—>中断号98—> 98 / 4 = 24 … 2—>GICD_IPRIORITYR24[23:19]
  4. 设置GICD层中断目标分配寄存器:
    PF9------>EXTI9—>中断号99—> 99 / 4 = 24 … 3—>GGICD_ITARGETSR24[25:24] = 0bx1
    PF7------>EXTI7—>中断号97—> 97 / 4 = 24 … 1—>GICD_ITARGETSR24[9:8] = 0bx1
    PF8------>EXTI8—>中断号98—> 98 / 4 = 24 … 2—>GICD_ITARGETSR24[17:16] = 0bx1
  5. 设置中断清除GICD层挂起标志位寄存器:
    PF9------>EXTI9—>中断号99—>GICD_ICPENDR3[3] = 1
    PF7------>EXTI7—>中断号97—>GICD_ICPENDR3[1] = 1
    PF8------>EXTI8—>中断号98—>GICD_ICPENDR3[2] = 1

6. GICC章节分析

  1. 设置GICC层全局中断使能寄存器:GICC_CTLR[0] = 1
  2. 设置中断优先级寄存器,此优先级的值要比GICD层中断优先级的值大:GICC_PMR[7:3]
  3. 获取按键的中断号:GICC_IAR[9:0]
  4. 清除按键的中断号:GICC_EOIR[9:0]

代码实现

1. key.h

#ifndef __KEY_H__
#define __KEY_H__

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

/********KEY1-->PF9********/
//初始化EXTI层
void pf9_exit_init();
//初始化GICD层
void pf9_gicd_init();
//初始化GPCC层
void pf9_gicc_init();

/********KEY2-->PF7********/
//初始化EXTI层
void pf7_exit_init();
//初始化GICD层
void pf7_gicd_init();
//初始化GPCC层
void pf7_gicc_init();

/********KEY3-->PF8********/
//初始化EXTI层
void pf8_exit_init();
//初始化GICD层
void pf8_gicd_init();
//初始化GPCC层
void pf8_gicc_init();

#endif

2. key.c

#include "key.h"

/********KEY1-->PF9********/
//初始化EXTI层
void pf9_exit_init()
{
	//RCC章节初始化
	//1.设置GPIOF组时钟使能
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	
	//GPIO章节初始化
	//1.设置PF9引脚为输入模式
	GPIOF->MODER &= (~(0x3 << 18));
	
	//EXTI章节初始化
	//1.设置PF9引脚和EXTI9控制器进行连接
	EXTI->EXTICR3 &= (~(0xff << 8));
	EXTI->EXTICR3 |= (0x05 << 8);
	//2.设置PF9引脚检测方式下降沿检测
	EXTI->FTSR1 |= (0x1 << 9);
	//3.设置PF9引脚中断不屏蔽
	EXTI->C1IMR1 |= (0x1 << 9);

}
//初始化GICD层
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] |= (0x1 << 24);

}
//初始化GICC层
void pf9_gicc_init()
{
	//1.设置GICC层全局中断使能寄存器
	GICC->CTRL |= (0x1 << 0);
	//2.设置GICC层中断优先级寄存器
	GICC->PMR &= (~(0x1f << 3));
	GICC->PMR |= (0x4 << 3);
	
}

/********KEY2-->PF7********/
//初始化EXTI层
void pf7_exit_init()
{
	//RCC章节初始化
	//1.设置GPIOF组时钟使能
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	
	//GPIO章节初始化
	//1.设置PF7引脚为输入模式
	GPIOF->MODER &= (~(0x3 << 14));
	
	//EXTI章节初始化
	//1.设置PF7引脚和EXTI7控制器进行连接
	EXTI->EXTICR2 &= (~(0xff << 24));
	EXTI->EXTICR2 |= (0x05 << 24);
	//2.设置PF7引脚检测方式下降沿检测
	EXTI->FTSR1 |= (0x1 << 7);
	//3.设置PF7引脚中断不屏蔽
	EXTI->C1IMR1 |= (0x1 << 7);

}

//初始化GICD层
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] |= (0x1 << 8);

}
//初始化GICC层
void pf7_gicc_init()
{
	//1.设置GICC层全局中断使能寄存器
	GICC->CTRL |= (0x1 << 0);
	//2.设置GICC层中断优先级寄存器
	GICC->PMR &= (~(0x1f << 3));
	GICC->PMR |= (0x4 << 3);
	
}

/********KEY3-->PF8********/
//初始化EXTI层
void pf8_exit_init()
{
	//RCC章节初始化
	//1.设置GPIOF组时钟使能
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	
	//GPIO章节初始化
	//1.设置PF8引脚为输入模式
	GPIOF->MODER &= (~(0x3 << 16));
	
	//EXTI章节初始化
	//1.设置PF8引脚和EXTI8控制器进行连接
	EXTI->EXTICR3 &= (~(0xff << 0));
	EXTI->EXTICR3 |= (0x05 << 0);
	//2.设置PF8引脚检测方式下降沿检测
	EXTI->FTSR1 |= (0x1 << 8);
	//3.设置PF8引脚中断不屏蔽
	EXTI->C1IMR1 |= (0x1 << 8);

}
//初始化GICD层
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] |= (0x1 << 16);

}
//初始化GICC层
void pf8_gicc_init()
{
	//1.设置GICC层全局中断使能寄存器
	GICC->CTRL |= (0x1 << 0);
	//2.设置GICC层中断优先级寄存器
	GICC->PMR &= (~(0x1f << 3));
	GICC->PMR |= (0x4 << 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 & 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;
}

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 < 1000; j++);
}

int main()
{
	//1.EXTI初始化
	pf9_exit_init();
	pf7_exit_init();
	pf8_exit_init();
	
	//2.GICD初始化
	pf9_gicd_init();
	pf7_gicd_init();
	pf8_gicd_init();
	
	//3.GICC初始化
	pf9_gicc_init();
	pf7_gicc_init();
	pf8_gicc_init();

	while(1);
	return 0;
}

测试结果

10.4国庆作业(中断实验)_第3张图片

你可能感兴趣的:(单片机,stm32,物联网)