2024.3.22 ARM

实现三个按键的中断

main.c :主函数初始化

#include "key_inc.h"
#include "uart4.h"
//封装延时函数
void delay(int ms)
{
	int i, j;
	for (i = 0; i < ms; i++)
	{
		for (j = 0; j < 2000; j++)
			;
	}
}
int main()
{
	char *s = "hello world";
	//串口初始化
	uart4_init();
	//按键中断的初始化
	all_key_init();
	key1_it_config();
	key2_it_config();
	key3_it_config();
	while (1)
	{
		puts(s);
		delay(1000);
	}
	return 0;
}

do_irq.c :处理中断信号


#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
#include "uart4.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;

void do_irq(void)
{
    //获取要处理的中断的中断号
    unsigned int irqno = GICC->IAR & 0x3ff;
    switch (irqno)
    {
    case 99:
        puts("key1_intc");
        //清除GICD中断排队标志
        GICD->ICPENDR[3] |= (0X1 << 3);
        //清除EXTI中断触发标志
        EXTI->FPR1 |= (0X1 << 9);
        break;
    case 98:
        puts("key3_intc");
        //清除GICD中断排队标志
        GICD->ICPENDR[3] |= (0X1 << 2);
        //清除EXTI中断触发标志
        EXTI->FPR1 |= (0X1 << 8);
        break;
    case 97:
        puts("key2_intc");
        //清除GICD中断排队标志
        GICD->ICPENDR[3] |= (0X1 << 1);
        //清除EXTI中断触发标志
        EXTI->FPR1 |= (0X1 << 7);
        break;
    default:
        break;
    }
    //清除IAR寄存器中的中断号
    GICC->EOIR |= irqno;
}

key_inc.c :配置key

#include "key_inc.h"

void all_key_init()
{
    //使能GPIOF外设时钟
    RCC->MP_AHB4ENSETR |= (0x1 << 5);
}

void key1_it_config()
{
    //将PF9设置为输入模式
    GPIOF->MODER &= (~(0x3 << 18));
    //设置由PF9管脚产生EXTI9事件
    EXTI->EXTICR3 &= (~(0XFF << 8));
    EXTI->EXTICR3 |= (0X05 << 8);
    //通过下降沿检测EXTI9事件是否产生
    EXTI->FTSR1 |= (0x1 << 9);
    //取消EXTI9中断屏蔽
    EXTI->C1IMR1 |= (0x1 << 9);
    //设置EXTI9中断全局使能
    GICD->ISENABLER[3] |= (0x1 << 3);
    //设置中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1f << 27));
    //设置CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0x1 << 24);
    //设置组0中断可以被转发到GICC层
    GICD->CTRL |= 0X1;
    //设置中断优先级掩码
    GICC->PMR |= (0x1f << 3);
    //设置组0中断交给指定处理器处理
    GICC->CTRL |= 0x1;
}
void key2_it_config()
{
    //将PF7设置为输入模式
    GPIOF->MODER &= (~(0x3 << 14));
    //设置由PF7管脚产生EXTI7事件
    EXTI->EXTICR2 &= (~(0XFF << 24));
    EXTI->EXTICR2 |= (0X05 << 24);
    //通过下降沿检测EXTI7事件是否产生
    EXTI->FTSR1 |= (0x1 << 7);
    //取消EXTI7中断屏蔽
    EXTI->C1IMR1 |= (0x1 << 7);
    //设置EXTI7中断全局使能
    GICD->ISENABLER[3] |= (0x1 << 1);
    //设置中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1f << 11));
    //设置CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0x1 << 8);
    //设置组0中断可以被转发到GICC层
    GICD->CTRL |= 0X1;
    //设置中断优先级掩码
    GICC->PMR |= (0x1f << 3);
    //设置组0中断交给指定处理器处理
    GICC->CTRL |= 0x1;
}

void key3_it_config()
{
    //将PF8设置为输入模式
    GPIOF->MODER &= (~(0x3 << 16));
    //设置由PF8管脚产生EXTI8事件
    EXTI->EXTICR3 &= (~(0XFF << 0));
    EXTI->EXTICR3 |= (0X05 << 0);
    //通过下降沿检测EXTI8事件是否产生
    EXTI->FTSR1 |= (0x1 << 8);
    //取消EXTI7中断屏蔽
    EXTI->C1IMR1 |= (0x1 << 8);
    //设置EXTI7中断全局使能
    GICD->ISENABLER[3] |= (0x1 << 2);
    //设置中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1f << 19));
    //设置CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0x1 << 16);
    //设置组0中断可以被转发到GICC层
    GICD->CTRL |= 0X1;
    //设置中断优先级掩码
    GICC->PMR |= (0x1f << 3);
    //设置组0中断交给指定处理器处理
    GICC->CTRL |= 0x1;
}

key_inc.h

#ifndef __KEY_INC_H__
#define __KEY_INC_H__

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

void all_key_init();
void key1_it_config();
void key2_it_config();
void key3_it_config();

#endif

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