ARM按键中断实验

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

按键2按下,蜂鸣器响。再按一次,不响

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

src/do_irq.c

cs 复制代码
#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 灯亮灭\n");
			if(GPIOE->ODR & (0X1<<10)){
				GPIOE->ODR &= (~(0X1<<10));
			}else{
				GPIOE->ODR |= (0X1<<10);
			}
			//清除挂起中断标志位GICD_ICPENDRx
			GICD->ICPENDR[3] |= (0X1<<3);
			//清除中断触发标志位EXTI_FPR1
			EXTI->FPR1 |= (0X1<<9);
			break;
		case 97://key2
			//按键2中断处理
			printf("KEY2 蜂鸣器\n");
			if(GPIOB->ODR & (0X1<<6)){
				GPIOB->ODR &= (~(0X1<<6));
			}else{
				GPIOB->ODR |= (0X1<<6);
			}

			//清除挂起中断标志位GICD_ICPENDRx
			GICD->ICPENDR[3] |= (0X1<<1);
			//清除中断触发标志位EXTI_FPR1
			EXTI->FPR1 |= (0X1<<7);
			break;
		case 98://key3
			//按键3中断处理
			printf("KEY3 风扇\n");
			if(GPIOE->ODR & (0X1<<9)){
				GPIOE->ODR &= (~(0X1<<9));
			}else{
				GPIOE->ODR |= (0X1<<9);
			}

			//清除挂起中断标志位GICD_ICPENDRx
			GICD->ICPENDR[3] |= (0X1<<2);
			//清除中断触发标志位EXTI_FPR1
			EXTI->FPR1 |= (0X1<<8);
			break;
	}
	//清除处理完的中断号GICC_EOTR
	GICC->EOIR = irqno;
}

src/key_it.c

cs 复制代码
#include"key_it.h"

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;

    
}
//灯1初始化
void led1_init()
{
	RCC->MP_AHB4ENSETR |= (0x1<<4);
	GPIOE->MODER &= (~(0X3<<20));
	GPIOE->MODER |= (0X1<<20);
	GPIOE->OTYPER &= (~(0X1<<10));
	GPIOE->OSPEEDR &= (~(0X3<<20));
	GPIOE->PUPDR &= (~(0X3<<20));

}
//蜂鸣器初始化
void tim_init()
{
	RCC->MP_AHB4ENSETR |= (0x1<<1);
	GPIOB->MODER &= (~(0X3<<12));
	GPIOB->MODER |= (0X1<<12);
	GPIOB->OTYPER &= (~(0X1<<6));
	GPIOB->OSPEEDR &= (~(0X3<<12));
	GPIOB->PUPDR &= (~(0X3<<12));

}
//风扇初始化
void fan_init()
{
	RCC->MP_AHB4ENSETR |= (0x1<<4);
	GPIOE->MODER &= (~(0X3<<18));
	GPIOE->MODER |= (0X1<<18);
	GPIOE->OTYPER &= (~(0X1<<9));
	GPIOE->OSPEEDR &= (~(0X3<<18));
	GPIOE->PUPDR &= (~(0X3<<18));

}

测试结果

相关推荐
烬奇小云5 小时前
使用 unicorn 和 capstone 库来模拟 ARM Thumb 指令的执行,并实现多个钩子(hook)来监控代码执行、系统调用和内存读写操作(二)
java·arm开发·python
CYRUS STUDIO14 小时前
Android 下内联汇编,Android Studio 汇编开发
android·汇编·arm开发·android studio·arm
艾格北峰1 天前
STM32 BootLoader 刷新项目 (九) 跳转指定地址-命令0x55
arm开发·stm32·单片机·嵌入式硬件
橘色的喵2 天前
嵌入式ARM平台Linux网络实时性能优化
linux·网络·arm开发·性能优化·实时·内核优化
荣世蓥3 天前
2.ARM_ARM是什么
arm开发
憧憬一下3 天前
Linux 内核中断描述符 (irq_desc) 的初始化与动态分配机制详解
arm开发·嵌入式硬件·嵌入式·c/c++·linux驱动开发
亿道电子3 天前
【ARM】MDK-E203 Undefined identifier
arm开发·stm32·单片机·arm
ShuQiHere3 天前
【ShuQiHere】️计算机架构:x86 与 ARM 指令集架构的对比与发展
arm开发·架构
CYRUS STUDIO3 天前
Android下的系统调用 (syscall),内联汇编syscall
android·linux·汇编·arm开发·arm·内联汇编
橘色的喵4 天前
C++编程:嵌入式Linux-ARM与外设中断交互的程序设计
linux·arm开发·select·interrupt·中断·低延迟·设备交互