【arm实验2】按键中断事件控制实验

设置按键中断,按键1按下,LED亮,再次按下,灭

按键2按下,蜂鸣器叫,再次按下,停

按键3按下,风扇转,再次按下,停

主函数:

c 复制代码
linux@linux:~/study/08-c$ cat main.c 
#include "uart.h"
#include "key_it.h"
int main()
{
    char c;
    char *s;
    uart4_init();//串口初始化
	all_led_config();//灯泡初始化
    //中断初始化
    key_it_config();
	key3_it_config();
    while(1)
    {
        //保证主程序不结束
    }
    return 0;
}

头文件:

c 复制代码
linux@linux:~/study/08-c$ cat include/key_it.h 
#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"

void key_it_config();
void key3_it_config();
void all_led_config();

#endif

初始化文件:

c 复制代码
linux@linux:~/study/08-c$ cat src/key_it.c
#include"key_it.h"

void all_led_config()
{
	//GPIOE时钟使能
    (*(unsigned int*)0X50000A28)  |= (0X1<<4);
    //PE10设置为输出模式
    *((unsigned int *)0X50006000) &= (~(0X3<<20));
    *((unsigned int *)0X50006000) |= (0X1<<20);
	//PB6设置为输出模式
    *((unsigned int *)0X50003000) &= (~(0X3<<12));
    *((unsigned int *)0X50003000) |= (0X1<<12);
	//PE9设置为输出模式
    *((unsigned int *)0X50006000) &= (~(0X3<<18));
    *((unsigned int *)0X50006000) |= (0X1<<18);
    //PE10设置为推挽输出
    *((unsigned int *)0X50006004) &= (~(0X1<<10));
    //PE9设置为推挽输出
    *((unsigned int *)0X50006004) &= (~(0X1<<9));
    //PB6设置为推挽输出
    *((unsigned int *)0X50003004) &= (~(0X1<<6));
    //PE10设置为低速输出
    *((unsigned int *)0X50006008) &= (~(0X3<<20));
    //PE9设置为低速输出
    *((unsigned int *)0X50006008) &= (~(0X3<<18));
    //PB6设置为低速输出
    *((unsigned int *)0X50003008) &= (~(0X3<<12));
    //PE10设置无上拉下拉电阻
    *((unsigned int *)0X5000600C) &= (~(0X3<<20));
    //PE9设置无上拉下拉电阻
    *((unsigned int *)0X5000600C) &= (~(0X3<<18));
    //PB6设置无上拉下拉电阻
    *((unsigned int *)0X5000300C) &= (~(0X3<<12));
}

void key3_it_config()
{
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    GPIOF->MODER &= (~(0x3<<16));
    EXTI->EXTICR3 &=(~(0xFF<<0));
    EXTI->EXTICR3 |= (0x5<<0);
    EXTI->FTSR1 |= (0x1<<8);
    EXTI->C1IMR1 |= (0x1<<8);
    GICD->ISENABLER[3] |= (0X1<<2); 
    GICD->IPRIORITYR[24] &= (~(0X1F<<19)); 
    GICD->ITARGETSR[24]  &= (~(0X3<<16));
    GICD->ITARGETSR[24] |= (0X1<<16);
}
void key_it_config()
{
    //RCC使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF9 PF7 PF8GPIO输入
    //PF9
    GPIOF->MODER &= (~(0x3<<18));
    //PF8
    // GPIOF->MODER &= (~(0x3<<16));
    //pf7
    GPIOF->MODER &= (~(0x3<<14));
    //设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx
    //pf7
    EXTI->EXTICR2 &=(~(0xFF<<24));
    EXTI->EXTICR2 |= (0x5<<24);
    //pf8
    // EXTI->EXTICR3 &=(~(0xFF<<0));
    // EXTI->EXTICR3 |= (0x5<<0);
    //pf9
    EXTI->EXTICR3 &=(~(0xFF<<8));
    EXTI->EXTICR3 |= (0x5<<8);
    //设置事件触发方式为下降沿触发EXTI_FTSR1
    //pf7
    EXTI->FTSR1 |= (0x1<<7);
    //pf8
    // EXTI->FTSR1 |= (0x1<<8);
    //pf9
    EXTI->FTSR1 |= (0x1<<9);
    //设置EXTI事件触发不屏蔽EXTI_IMR1
    //PF7
    EXTI->C1IMR1 |= (0x1<<7);
    //PF8
    // EXTI->C1IMR1 |= (0x1<<8);
    //PF9
    EXTI->C1IMR1 |= (0x1<<9);
    //使能中断能转发到特定的CPU接口层GICD_ISENABLERx
    //PF7 97号中断
    GICD->ISENABLER[3] |= (0X1<<1); 
    //PF78 98号中断
    // GICD->ISENABLER[3] |= (0X1<<2); 
    //PF9 99号中断
    GICD->ISENABLER[3] |= (0X1<<3); 
    // GICD_ISENABLERx设置中断优先级
    //PF7
    GICD->IPRIORITYR[24] &= (~(0X1F<<11)); 
    //PF8
    // GICD->IPRIORITYR[24] &= (~(0X1F<<19)); 
    //PF9
    GICD->IPRIORITYR[24] &= (~(0X1F<<27)); 
    //设置当前中断被转发到哪一个CPU处理GICD_ITARGETSRx
    //pf7 97中断
    GICD->ITARGETSR[24]  &= (~(0X3<<8));
    GICD->ITARGETSR[24] |= (0X1<<8);
    //pf8 98中断
    // GICD->ITARGETSR[24]  &= (~(0X3<<16));
    // GICD->ITARGETSR[24] |= (0X1<<16);
    //pf9 99中断
    GICD->ITARGETSR[24]  &= (~(0X3<<24));
    GICD->ITARGETSR[24] |= (0X1<<24);
    //使能组0转发中断GICD_CTLR
    GICD->CTRL |= 0X1;
    //设置中断优先级掩码GICC_PMR
    GICC->PMR |= (0x1F<<3);

    //使能CPU可以处理组0转发的中断GICC_CTLR
    GICC->CTRL |= 0X1;

    
}

中断处理文件:

c 复制代码
linux@linux:~/study/08-c$ cat src/do_irq.c
#include "key_it.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
    int irqno; // 保存中断号
    printf("%d\n",irqno);
    irqno = GICC->IAR & 0X3FF;
    switch (irqno)
    {
    case 99: // key1
        // 按键1中断处理
        printf("KEY1 INT\n");
		*((unsigned int *)0X50006014) ^= (0X1<<10);
        // 清除挂起中断标志位GICD_ICPENDRx
        GICD->ICPENDR[3] |= (0x1 << 3);
        // 清除中断触发标志位EXTI_FPR1
        EXTI->FPR1 |= (0x1 << 9);
        break;
    case 97: // key2
        // 按键2中断处理
        printf("KEY2 INT\n");
		*((unsigned int *)0X50003014) ^= (0X1<<6);
        // 清除挂起中断标志位GICD_ICPENDRx
        GICD->ICPENDR[3] |= (0x1 << 1);
        // 清除中断触发标志位EXTI_FPR1
        EXTI->FPR1 |= (0x1 << 7);
        break;
    case 98: // key3
        // 按键3中断处理
        printf("KEY3 INT\n");
		*((unsigned int *)0X50006014) ^= (0X1<<9);
        // 清除挂起中断标志位GICD_ICPENDRx
        GICD->ICPENDR[3] |= (0x1 << 2);
        // 清除中断触发标志位EXTI_FPR1
        EXTI->FPR1 |= (0x1 << 8);
        break;
    }
    // 清除处理完的中断号GICC_EOIR
    GICC->EOIR = irqno;
}

效果图:


相关推荐
学习路上_write10 分钟前
FPGA/Verilog,Quartus环境下if-else语句和case语句RT视图对比/学习记录
单片机·嵌入式硬件·qt·学习·fpga开发·github·硬件工程
非概念16 分钟前
stm32学习笔记----51单片机和stm32单片机的区别
笔记·stm32·单片机·学习·51单片机
jjjxxxhhh1232 小时前
FPGA,使用场景,相比于单片机的优势
单片机·嵌入式硬件·fpga开发
无敌最俊朗@2 小时前
stm32学习之路——八种GPIO口工作模式
c语言·stm32·单片机·学习
EterNity_TiMe_2 小时前
【论文复现】STM32设计的物联网智能鱼缸
stm32·单片机·嵌入式硬件·物联网·学习·性能优化
changingshow3 小时前
Arduino IDE Windows 系统 离线安装 esp32 开发板 亲测好用。
单片机·嵌入式硬件
烬奇小云5 小时前
使用 unicorn 和 capstone 库来模拟 ARM Thumb 指令的执行,并实现多个钩子(hook)来监控代码执行、系统调用和内存读写操作(二)
java·arm开发·python
7yewh5 小时前
嵌入式硬件杂谈(一)-推挽 开漏 高阻态 上拉电阻
驱动开发·stm32·嵌入式硬件·mcu·物联网·硬件架构·pcb工艺
Chervin12 小时前
Windows,虚拟机Ubuntu和开发板三者之间的NFS服务器搭建
linux·单片机·ubuntu·arm
TeYiToKu13 小时前
笔记整理—linux驱动开发部分(8)framebuffer类设备
linux·驱动开发·笔记·嵌入式硬件·arm