ARM中断实验

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

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

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

main.c

#include "uart1.h"
#include "key_it.h"
int main()
{
    uart4_init();//串口初始化


    //中断初始化
    key_it_config();
	key3_it_config();
	gpio_start();
    while(1)
    {
        //保证主程序不结束
    }
    return 0;
}

do_irp.c

#include "key_it.h"
#include "uart1.h"

extern void printf(const char *fmt, ...);
unsigned int i = 0;
unsigned int num1=0,num2=0,num3=0;
void do_irq(void) 
{
	int irqno;//保存中断号
	irqno = GICC->IAR&0X3FF;
	switch(irqno)
	{
	case 99://key1
		//按键1中断处理
		printf("KEY1 INT\n");
		//灯亮
		if(num1 == 0)
		{
			//PF10
			GPIOF->ODR |= (0x1<<10);
			//PE10 PE8
			GPIOE->ODR |= (0x5<<8);
			num1++;
		}
		//灯灭
		else if(num1 > 0)
		{
			//PF10
			GPIOF->ODR &= ~(0x1<<10);
			//PE10 PE8
			GPIOE->ODR &= ~(0x5<<8);
			num1 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<3);
		//清除中断触发标志位EXTI_FRR1
		EXTI->FPR1 |= (0x1<<9);
		break;
	case 97://key2
		//按键2中断处理
		printf("KEY2 INT\n");
		//蜂鸣器转
		if(num2 == 0)
		{
			//PB6
			GPIOB->ODR |= (0x1<<6);
			num2++;
		}
		//蜂鸣器停
		else if(num2 > 0)
		{
			//PB6
			GPIOB->ODR &= ~(0x1<<6);
			num2 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<1);
		//清除中断触发标志位EXTI_FRR1
		EXTI->FPR1 |= (0x1<<7);
		break;
	case 98://key3
		//按键3中断处理
		printf("KEY3 INT\n");
		//风扇转
		if(num3 == 0)
		{
			//PE9
			GPIOE->ODR |= (0x1<<9);
			num3++;
		}
		//风扇停
		else if(num3 > 0)
		{
			//PE9
			GPIOE->ODR &= ~(0x1<<9);
			num3 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<2);
		//清除中断触发标志位EXTI_FRR1
		EXTI->FPR1 |= (0x1<<8);
		break;
	}
	//清除处理完的中断号GICC_EOIR
	GICC->EOIR=irqno;
}

key_it.c

#include "key_it.h"
#include "uart1.h"

extern void printf(const char *fmt, ...);
unsigned int i = 0;
unsigned int num1=0,num2=0,num3=0;
void do_irq(void) 
{
	int irqno;//保存中断号
	irqno = GICC->IAR&0X3FF;
	switch(irqno)
	{
	case 99://key1
		//按键1中断处理
		printf("KEY1 INT\n");
		//灯亮
		if(num1 == 0)
		{
			//PF10
			GPIOF->ODR |= (0x1<<10);
			//PE10 PE8
			GPIOE->ODR |= (0x5<<8);
			num1++;
		}
		//灯灭
		else if(num1 > 0)
		{
			//PF10
			GPIOF->ODR &= ~(0x1<<10);
			//PE10 PE8
			GPIOE->ODR &= ~(0x5<<8);
			num1 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<3);
		//清除中断触发标志位EXTI_FRR1
		EXTI->FPR1 |= (0x1<<9);
		break;
	case 97://key2
		//按键2中断处理
		printf("KEY2 INT\n");
		//蜂鸣器转
		if(num2 == 0)
		{
			//PB6
			GPIOB->ODR |= (0x1<<6);
			num2++;
		}
		//蜂鸣器停
		else if(num2 > 0)
		{
			//PB6
			GPIOB->ODR &= ~(0x1<<6);
			num2 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<1);
		//清除中断触发标志位EXTI_FRR1
		EXTI->FPR1 |= (0x1<<7);
		break;
	case 98://key3
		//按键3中断处理
		printf("KEY3 INT\n");
		//风扇转
		if(num3 == 0)
		{
			//PE9
			GPIOE->ODR |= (0x1<<9);
			num3++;
		}
		//风扇停
		else if(num3 > 0)
		{
			//PE9
			GPIOE->ODR &= ~(0x1<<9);
			num3 = 0;
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |= (0x1<<2);
		//清除中断触发标志位EXTI_FRR1
		EXTI->FPR1 |= (0x1<<8);
		break;
	}
	//清除处理完的中断号GICC_EOIR
	GICC->EOIR=irqno;
}

你可能感兴趣的:(arm开发)