2023-2-22作业

按下按键之后,打印一句话------>三个按键需要实现,

例如:当按键1按下之后,打印11111111


key_it.h:

#ifndef __KET_IT_H__
#define __KET_IT_H__

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

//EXTI控制器初始化
void hal_exti_init();

//GICD控制器初始化
void hal_gicd_init();

//GICC控制器初始化
void hal_gicc_init();

#endif

key_it.c:

#include "key_it.h"


//EXTI控制器初始化
void hal_exti_init()
{
	/*********RCC章节*********/
	RCC->MP_AHB4ENSETR |= (0x1 << 5);
	/*********GPIO章节*********/
	GPIOF->MODER &= (~(0x3 << 18)); 	//KEY1--->PF9模式设置为input
	GPIOF->MODER &= (~(0x3 << 14)); 	//KEY2--->PF7模式设置为input
	GPIOF->MODER &= (~(0x3 << 16)); 	//KEY3--->PF8模式设置为input
	/*********EXTI章节*********/
	//将GPIO控制器与EXTI控制器进行连接
	EXTI->EXTICR3 &= (~(0xFF << 8));
	EXTI->EXTICR3 |= (0x05 << 8);
	EXTI->EXTICR2 &= (~(0xFF << 24));
	EXTI->EXTICR2 |= (0x05 << 24);
	EXTI->EXTICR3 &= (~(0xFF << 0));
	EXTI->EXTICR3 |= (0x05 << 0);
	//选择下降沿触发
	EXTI->FTSR1 |= (0x1 << 9); 		//KEY1
	EXTI->FTSR1 |= (0x1 << 7); 		//KEY2
	EXTI->FTSR1 |= (0x1 << 8); 		//KEY3
	//中断屏蔽选择不屏蔽
	EXTI->C1IMR1 |= (0x1 << 9); 	//KEY1
	EXTI->C1IMR1 |= (0x1 << 7); 	//KEY2
	EXTI->C1IMR1 |= (0x1 << 8); 	//KEY3
}

//GICD控制器初始化
void hal_gicd_init()
{
	/*********GICD章节*********/
	GICD->CTRL |= (0x1 << 0); 		//设置GICD层CPU0组使能
	//中断设置使能寄存器,设置为使能
	GICD->ISENABLER[3] |= (0x1 << 3);
	GICD->ISENABLER[3] |= (0x1 << 1);
	GICD->ISENABLER[3] |= (0x1 << 2);
	//设置中断优先级
	GICD->IPRIORITYR[24] &= (~(0x1F << 27));
	GICD->IPRIORITYR[24] |= (0xF << 27);
	GICD->IPRIORITYR[24] &= (~(0x1F << 11));
	GICD->IPRIORITYR[24] |= (0xF << 11);
	GICD->IPRIORITYR[24] &= (~(0x1F << 19));
	GICD->IPRIORITYR[24] |= (0xF << 19);
	//中断目标分配寄存器,设置为分配给CPU0
	GICD->ITARGETSR[24] |= (0x1 << 24);
	GICD->ITARGETSR[24] |= (0x1 << 8);
	GICD->ITARGETSR[24] |= (0x1 << 16);
}

//GICC控制器初始化
void hal_gicc_init()
{
	/*********GICC章节*********/
	GICC->CTRL |= (0x1 << 0); 		//设置GICC层CPU0组使能
	//设置中断优先级
	GICC->PMR &= (~(0x1F << 3));
	GICC->PMR |= (0x1F << 3);
}

main.c:

#include "key_it.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()
{
	//初始化
	hal_exti_init();
	hal_gicd_init();
	hal_gicc_init();

	while(1)
	{
	
	}
	return 0;
}

do_irq.c:

#include "key_it.h"

extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void) 
{
	//获取中断号	
	unsigned int id = GICC->IAR;
	//判断中断号
	switch(id)
	{
		case 99:
			printf("KEY1按键按下\n");
			//清除中断,挂起标志位
			EXTI->FPR1 |= (0x1 << 9); 	//EXTI9清除中断挂起标志位
			GICD->ICPENDR[3] |= (0x1 << 3); 	//GICD清除中断挂起标志位
			break;
		case 97:
			printf("KEY2按键按下\n");
			//清除中断,挂起标志位
			EXTI->FPR1 |= (0x1 << 7); 	//EXTI7清除中断挂起标志位
			GICD->ICPENDR[1] |= (0x1 << 1); 	//GICD清除中断挂起标志位
			break;
		case 98:
			printf("KEY3按键按下\n");
			//清除中断,挂起标志位
			EXTI->FPR1 |= (0x1 << 8); 	//EXTI8清除中断挂起标志位
			GICD->ICPENDR[2] |= (0x1 << 2); 	//GICD清除中断挂起标志位
			break;
	}

	GICC->EOIR |= id; 		//清除获取的中断号
}

测试结果如下:

按下按键之后,打印一句话------>三个按键需要实现,

例如:当按键1按下之后,打印11111111

 

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