ARM按键中断

do_irq.c

判断中断号

#include "key_it.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
    // 获取中断号,根据中断号的不同进行不同的中断处理
    int irqno;
    irqno = GICC->IAR & 0x3ff;
    switch (irqno)
    {
    case 99:
        printf("key1 int\n");
        // 清除exti中断标志位
        EXTI->FPR1 |= (0x1 << 9);
        // 清除GICD中断标志位  99/32=3...3
        GICD->ICPENDR[3] = (0x1 << 3);
        led1_on();
        break;
    case 97:
        printf("key2 int\n");
        // 清除exti中断标志位
        EXTI->FPR1 |= (0x1 << 7);
        // 清除GICD中断标志位
        GICD->ICPENDR[3] = (0x1 << 1);
        led1_off();
        break;
    case 98:
        printf("key3 int\n");
        // 清除exti中断标志位
        EXTI->FPR1 |= (0x1 << 8);
        // 清除GICD中断标志位
        GICD->ICPENDR[3] = (0x1 << 2);
        break;
    }
    // 清除IAR寄存器的值,也就是中断号
    GICC->EOIR = irqno;
}

 key_it.c

按钮操作

#include "key_it.h"

void key1_it_config()
{

    //1.设置GPIOF时钟使能
    RCC->MP_AHB4ENSETR |= (0X1 << 5);
    // 2.将PF9管脚设置为输入
    GPIOF->MODER &= (~(0x3 << 18));
    //3.设置由PF9产生EXTI9事件
    EXTI->EXTICR3 &= (~(0XFF << 8));
    EXTI->EXTICR3 |= (0x05 << 8);
    // 4.设置EXTI9事件的检测方式为下降沿检测
    EXTI->FTSR1 |= (0x1 << 9);
    // 5.允许中断不屏蔽,可以被转发到GIC
    EXTI->C1IMR1 |= (0x1 << 9);
    // 6.允许EXTI9(99号)中断被保存在组0中
    GICD->ISENABLER[3] |= (0x1 << 3);
    // 7.设置99号中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1f << 27));
    GICD->IPRIORITYR[24] |= (0x0 << 27);
    // 8.设置99号中断可以被CPU0处理
    GICD->ITARGETSR[24] &= (~(0x3 << 24));
    GICD->ITARGETSR[24] |= (0x1 << 24);
    //9.允许99号中断被转发到CPU接口层
    GICD->CTRL |= 0x1;
    //10.设置中断优先级掩码
    GICC->PMR |= (0x1f << 3);
    //11.允许中断被转发给CPU处理
    GICC->CTRL |= 0x1;
}
void key2_it_config()
{
    //1.设置GPIOF时钟使能
    RCC->MP_AHB4ENSETR |= (0X1 << 5);
    // 2.将PF7管脚设置为输入
    GPIOF->MODER &= (~(0x3 << 14));
    //3.设置由PF7产生EXTI7事件
    EXTI->EXTICR2 &= (~(0XFF << 24));
    EXTI->EXTICR2 |= (0x05 << 24);
    // 4.设置EXTI7事件的检测方式为下降沿检测
    EXTI->FTSR1 |= (0x1 << 7);
    // 5.允许中断不屏蔽,可以被转发到GIC
    EXTI->C1IMR1 |= (0x1 << 7);
    // 6.允许EXTI7(97号)中断被保存在组0中
    GICD->ISENABLER[3] |= (0x1 << 1);
    // 7.设置97号中断优先级
    GICD->IPRIORITYR[24] &= (~(0x1f << 11));
    GICD->IPRIORITYR[24] |= (0x0 << 11);
    // 8.设置97号中断可以被CPU0处理
    GICD->ITARGETSR[24] &= (~(0x3 << 8));
    GICD->ITARGETSR[24] |= (0x1 << 8);
    //9.允许97号中断被转发到CPU接口层
    GICD->CTRL |= 0x1;
    //10.设置中断优先级掩码
    GICC->PMR |= (0x1f << 3);
    //11.允许中断被转发给CPU处理
    GICC->CTRL |= 0x1;
}
void key3_it_config()
{
    //1.设置GPIOF时钟使能
    RCC->MP_AHB4ENSETR |= (0X1 << 5);
    // 2.将PF8管脚设置为输入
    GPIOF->MODER &= (~(0x3 << 16));
    //3.设置由PF8产生EXTI8事件
    EXTI->EXTICR3 &= (~(0XFF << 0));
    EXTI->EXTICR3 |= (0x05 << 0);
    // 4.设置EXTI8事件的检测方式为下降沿检测
    EXTI->FTSR1 |= (0x1 << 8);
    // 5.允许中断不屏蔽,可以被转发到GIC
    EXTI->C1IMR1 |= (0x1 << 8);
    // 6.允许EXTI8(98号)中断被保存在组0中 98/32=3...2
    GICD->ISENABLER[3] |= (0x1 << 2);
    // 7.设置98号中断优先级   98/4=24...2     2*8+3=19
    GICD->IPRIORITYR[24] &= (~(0x1f << 19));
    GICD->IPRIORITYR[24] |= (0x0 << 19);
    // 8.设置98号中断可以被CPU0处理 98/4=24...2
    GICD->ITARGETSR[24] &= (~(0x3 << 16));
    GICD->ITARGETSR[24] |= (0x1 << 16);
    //9.允许97号中断被转发到CPU接口层
    GICD->CTRL |= 0x1;
    //10.设置中断优先级掩码
    GICC->PMR |= (0x1f << 3);
    //11.允许中断被转发给CPU处理
    GICC->CTRL |= 0x1;
}
void all_led_init()
{
   //1.使能外设时钟
  (*((unsigned int*)0X50000A28)) |= (0x3<<4);
  //2.设置PF9为输出输出
  (*((unsigned int*)0X50007000)) &= (~(0x3<<12));
  (*((unsigned int*)0X50007000)) |= (0x1<<12);
  //3.设置推挽输出
  (*((unsigned int*)0X50007004)) &= (~(0x1<<6));
  //4.设置输出速度为低速
  (*((unsigned int*)0X50007008)) &= (~(0x3<<12));
//5.设置无上拉下拉
  (*((unsigned int*)0X5000700c)) &= (~(0x3<<12));
  
}

void led1_on()
{
    
  (*((unsigned int*)0X50007014)) |= (0x1<<6);

}


void led1_off()
{
  (*((unsigned int*)0X50007014)) &= (~(0x1<<6));

}

key_it.h

#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gic.h"

void key1_it_config();
void key2_it_config();
void key3_it_config();
void all_led_init();
void led1_on();
void led1_off();
#endif

main.c

#include "key_it.h"



void delay(int ms)

{

  int i,j;

  for(i=0;i

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