ARM-day9

按键控制小灯、蜂鸣器、风扇,按一次启动,第二次关闭

key_it.c

#include "key_it.h"

//按键3的配置
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_APB1ENSETR |= (0x1<<5);
	//设置PF9 PF7 Pf8 的GPIO输入
	//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);
	//PF8 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处理
	//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_CTRL
	GICD->CTRL |= 0x1;
	//设置中断优先级掩码 	GICC_PMR
	GICC->PMR |= (0x1F<<3);
	//使能CPU可以处理组0转发的中断GICC_CTRL
	GICC->CTRL |= 0x1;
}

led.c

#include "led.h"

//延时函数
void delay(int ms)
{
	int i,j;
	for(i=0; i

feng2.c

#include "feng2.h"

void buzzer_init()
{
	//RCC使能GPIOB时钟
	RCC->MP_AHB4ENSETR |= (0x1<<1);
	//PB6
	GPIOB->MODER &= (~(0x3<<12));
	GPIOB->MODER |= (0x1<<12);
	GPIOB->OTYPER &= (~(0x1<<6));
	GPIOB->OSPEEDR &= (~(0x3<<12));
	GPIOB->PUPDR &= (~(0x3<<12));
}

void fan_init()
{
	GPIOE->MODER &= (~(0x3<<18));
	//PE9
	GPIOE->MODER |= (0x1<<18);
	GPIOE->OTYPER &= (~(0x1<<9));
	GPIOE->OSPEEDR &= (~(0x3<<18));
	GPIOE->PUPDR &= (~(0x3<<18));
	
}

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: 	//key1
		//按键1中断处理
		printf("KEY1 INT\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 INT\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 INT\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;
	default:
		break;
	}
	//清除处理完的中断号 GICC_EOIR
	GICC->EOIR = irqno;
}

main.c

#include "uart.h"

int main()
{

//	char c;
//	char *s;
	//串口初始化
	uart4_init();
	//中断初始化
	all_led_init();	
	buzzer_init();
	fan_init();
	key_it_config();
	key3_it_config();

	while(1)
	{
		//保证主程序不结束
	}
	return 0;
}

你可能感兴趣的:(作业,arm)