12.26 stm32mp157按键中断

key.h

#ifndef  __KEY_H__
#define __KEY_H__
#include  "stm32mp1xx_rcc.h"
#include  "stm32mp1xx_gpio.h"
#include  "stm32mp1xx_gic.h"
#include  "stm32mp1xx_exti.h"
void key1_config();
void key2_config();
void key3_config();
#endif

key.c

#include  "key.h"

void key1_config()
{
    //设置时钟使能
    RCC->MP_AHB4ENSETR |=(0x1<<5);
    //设置管脚输入模式PF9
    GPIOF->MODER  &=(~(0x3<<18));
    
    //设置EXTI事件与管脚对应
    EXTI->EXTICR3 &=(~(0xff<<8));
    EXTI->EXTICR3 |=(0x05<<8);    
    //设置检测方式为下降沿
    EXTI->FTSR1 |=(0x1<<9);
    //设置可以产生事件取消屏蔽
    EXTI->C1IMR1 |=(0x1<<9);
    
    //在GPID层启用中断
    GICD->ISENABLER[3] |=(0x1<<3);
    //设置组寄存器的优先级
    GICD->IPRIORITYR[24] &=(~(0x1f<<27));
    //设置处理CPU
    GICD->ITARGETSR[24] |=(0x1<<24);
    //设置全局使能组为0
    GICD->CTLR |=(0x1);


    //设置优先级掩码为最低
    GICC->PMR |=(0x1f<<3);
    //使能组0的中断
    GICC->CTLR |=(0x1);
}

void key2_config()
{
    //设置时钟使能
    RCC->MP_AHB4ENSETR |=(0x1<<5);
    //设置管脚输入模式PF7
    GPIOF->MODER  &=(~(0x3<<14));
    
    //设置EXTI事件与管脚对应
    EXTI->EXTICR2 &=(~(0xff<<24));
    EXTI->EXTICR2 |=(0x05<<24);    
    //设置检测方式为下降沿
    EXTI->FTSR1 |=(0x1<<7);
    //设置可以产生事件取消屏蔽
    EXTI->C1IMR1 |=(0x1<<7);
    
    //在GPID层启用中断
    GICD->ISENABLER[3] |=(0x1<<1);
    //设置组寄存器的优先级
    GICD->IPRIORITYR[24] &=(~(0x1f<<11));
    //设置处理CPU
    GICD->ITARGETSR[24] |=(0x1<<8);
    //设置全局使能组为0
    GICD->CTLR |=(0x1);


    //设置优先级掩码为最低
    GICC->PMR |=(0x1f<<3);
    //使能组0的中断
    GICC->CTLR |=(0x1);
}

void key3_config()
{
    //设置时钟使能
    RCC->MP_AHB4ENSETR |=(0x1<<5);
    //设置管脚输入模式PF8
    GPIOF->MODER  &=(~(0x3<<16));
    
    //设置EXTI事件与管脚对应
    EXTI->EXTICR3 &=(~(0xff));
    EXTI->EXTICR3 |=(0x05);    
    //设置检测方式为下降沿
    EXTI->FTSR1 |=(0x1<<8);
    //设置可以产生事件取消屏蔽
    EXTI->C1IMR1 |=(0x1<<8);
    
    //在GPID层启用中断
    GICD->ISENABLER[3] |=(0x1<<2);
    //设置组寄存器的优先级
    GICD->IPRIORITYR[24] &=(~(0x1f<<19));
    //设置处理CPU
    GICD->ITARGETSR[24] |=(0x1<<16);
    //设置全局使能组为0
    GICD->CTLR |=(0x1);


    //设置优先级掩码为最低
    GICC->PMR |=(0x1f<<3);
    //使能组0的中断
    GICC->CTLR |=(0x1);
}

do_irq.c

#include "key.h"
#include "led.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
//延时函数
void delay1(int ms)
{
   int i,j;
    for(i=0;i<ms;i++)
    {
        for(j=0;j<2000;j++);
    }
}
void do_irq(void) 
{

    int num=(GICC->IAR &(0x3ff));
    switch(num)
    {
        case 99:
                       led1_on();
                        //清除排队标志
                        GICD->ICPENDR[3] |=(0x1<<3);
                        //清除挂起标志
                        EXTI->FPR1 |=(0x1<<9);
                        break;
         case 98:
                        led1_off();
                        //清除排队标志
                        GICD->ICPENDR[3] |=(0x1<<2);
                        //清除挂起标志
                        EXTI->FPR1 |=(0x1<<8);
                        break;
         case 97:
                        led2_on();
                        delay1(1000);
                        led2_off();
                        //清除排队标志
                        GICD->ICPENDR[3] |=(0x1<<1);
                        //清除挂起标志
                        EXTI->FPR1 |=(0x1<<7);
                        break;                                              
    }
    //清除中断号
    GICC->EOIR |= num;
}

main.c

#include "key.h"

#include "led.h"

//延时函数

void delay(int ms)

{

   int i,j;

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

    {

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

    }

}



int main()

{

    all_led_init();

    key1_config();

    key2_config();

    key3_config();



    while(1)

    {

        printf("mmmm\n");

        delay(1000);

    }

	return 0;

}

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值