2023年12月26日:按键中断

2023-12-26 21:37:57

key_int.h

#ifndef __KEY_INT_H__
#define __KEY_INT_H__

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
void key1_it_config();
void key2_it_config();
void key3_it_config();
void led1_on();
void led2_on();
void led3_on();

#endif

key_int.c

#include "key_int.h"

void key1_it_config()
{

    //设置GPIOF时钟使能
    RCC->MP_AHB4ENSETR |= (0X1 << 5);
    //设置PF9管脚输入
    GPIOF->MODER &= (~(0X3 << 18));
    //设置PF9作为EXTI9事件输入
    EXTI->EXTICR3 &= (~(0X1F << 8));
    EXTI->EXTICR3 |= (0X5 << 8);
    //设置下降沿使能检测EXTI9事件
    EXTI->FTSR1 |= (0X1 << 9);
    //设置EXIT9事件不屏蔽
    EXTI->C1IMR1 |= (0X1 << 9);
    //GICD使能EXIT9(99号)中断
    GICD->ISENABLER[3] = (0X1 << 3);
    //GICD设置99号中断的优先级
    GICD->IPRIORITYR[24] &= (~(0X1F << 27));
    //选择CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0X1 << 24);
    //全局使能组0中断被转发到GICC
    GICD->CTRL |= (0X1);
    //设置终端的优先级掩码
    GICC->PMR |= (0X1F << 3);
    //允许组0中断被CPU处理
    GICC->CTRL |= (0X1);
}

void key2_it_config()
{
 
    //设置PF7管脚输入
    GPIOF->MODER &= (~(0X3 << 14));
    //设置PF7作为EXTI7事件输入
    EXTI->EXTICR2 &= (~(0X1F << 24));
    EXTI->EXTICR2 |= (0X5 << 24);
    //设置下降沿使能检测EXTI7事件
    EXTI->FTSR1 |= (0X1 << 7);
    //设置EXIT7事件不屏蔽
    EXTI->C1IMR1 |= (0X1 << 7);
    //GICD使能EXIT9(97号)中断
    GICD->ISENABLER[3] = (0X1 << 1);
    //GICD设置97号中断的优先级
    GICD->IPRIORITYR[24] &= (~(0X1F << 11));
    //选择CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0X1 << 8);
    //全局使能组0中断被转发到GICC
    GICD->CTRL |= (0X1);
    //设置终端的优先级掩码
    GICC->PMR |= (0X1F << 3);
    //允许组0中断被CPU处理
    GICC->CTRL |= (0X1);
}

void key3_it_config()
{

    //设置PF8管脚输入
    GPIOF->MODER &= (~(0X3 << 16));
    //设置PF8作为EXTI8事件输入
    EXTI->EXTICR3 &= (~(0X1F));
    EXTI->EXTICR3 |= (0X5);
    //设置下降沿使能检测EXTI8事件
    EXTI->FTSR1 |= (0X1 << 8);
    //设置EXIT8事件不屏蔽
    EXTI->C1IMR1 |= (0X1 << 8);
    //GICD使能EXIT8(98号)中断
    GICD->ISENABLER[3] = (0X1 << 2);
    //GICD设置98号中断的优先级
    GICD->IPRIORITYR[24] &= (~(0X1F << 19));
    //选择CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0X1 << 16);
    //全局使能组0中断被转发到GICC
    GICD->CTRL |= (0X1);
    //设置终端的优先级掩码
    GICC->PMR |= (0X1F << 3);
    //允许组0中断被CPU处理
    GICC->CTRL |= (0X1);
}
void led1_on()
{
    //LED1的操作
   
    GPIOE->MODER &=(~(0x3<<20));//设置PE10输出
    GPIOE->MODER |=(0x1<<20);

    GPIOE->OTYPER &=(~(0x1<<10));//设置PE10为推挽输出
    GPIOE->OSPEEDR &=(~(0x3<<20));//设置低速输出
    GPIOE->PUPDR &=(~(0x3<<20));//设置无上拉下拉
}
void led2_on()
{
    //LED2的操作
    GPIOF->MODER &=(~(0x3<<20));//设置PF10输出
    GPIOF->MODER |=(0x1<<20);

    GPIOF->OTYPER &=(~(0x1<<10));//设置PF10为推挽输出
    GPIOF->OSPEEDR &=(~(0x3<<20));//设置低速输出
    GPIOF->PUPDR &=(~(0x3<<20));//设置无上拉下拉
}
void led3_on()
{
     //LED3的操作
    GPIOE->MODER &=(~(0x3<<16));//设置PE10输出
    GPIOE->MODER |=(0x1<<16);

    GPIOE->OTYPER &=(~(0x1<<8));//设置PE10为推挽输出
    GPIOE->OSPEEDR &=(~(0x3<<16));//设置低速输出
    GPIOE->PUPDR &=(~(0x3<<16));//设置无上拉下拉

}

do_irq.c

#include "key_int.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
    int irqnu = (GICC->IAR & 0x3ff);
    switch (irqnu)
    {
    case 99: //KEY1
        printf("KEY1_INT\n");
        GPIOE->ODR |= (0x1 << 10); //灯1亮
       	GPIOE->ODR &=(~(0x1<<8));//灯3灭
        //清除GICD的中断排队标志
        GICD->ICPENDR[3] |= (0x1 << 3);
        //清除EXTI的事件触发标志
        EXTI->FPR1 |= (0x1 << 9);
        break;
    case 98: //KEY3
        printf("KEY3_INT\n");
        GPIOE->ODR |= (0x1 << 8);     //灯3亮
        GPIOF->ODR &= (~(0x1 << 10)); //灯2灭
        //清除GICD的中断排队标志
        GICD->ICPENDR[3] |= (0x1 << 2);
        //清除EXTI的事件触发标志
        EXTI->FPR1 |= (0x1 << 8);
        break;
    case 97: //KEY2
        printf("KEY2_INT\n");
        GPIOF->ODR |= (0x1 << 10);    //灯2亮
        GPIOE->ODR &= (~(0x1 << 10)); //灯1灭
        //清除GICD的中断排队标志
        GICD->ICPENDR[3] |= (0x1 << 1);
        //清除EXTI的事件触发标志
        EXTI->FPR1 |= (0x1 << 7);
        break;
    }
    GICC->EOIR = irqnu;
}

main.c


#include "key_int.h"

void delay(int ms)

{
	int i,j;

	for(i=0;i<ms;i++)

	{

		for(j=0;j<2000;j++);

	}

}


int main()

{

	key1_it_config();

	key2_it_config();

	key3_it_config();

	led1_on();

	led2_on();

	led3_on();

while(1)

{

	printf("do_main...\n");

	delay(1000);

}

	return 0;

}

效果图

文章来源:https://blog.csdn.net/Huai1230/article/details/135230024
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。