arm实验

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

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

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

头文件

#ifndef __CTRL_KEY_H__
#define __CTRL_KEY_H__

#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
void all_led_init();   //初始化灯
void fan_init(); 		//初始化风扇
void voice_init();    //初始化蜂鸣器
void key_config();    //初始化中断
#endif

异常处理文件

#include "ctrl_key.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void) 
{
	int irq_num; //保存中断号
	irq_num = GICC->IAR &0x3ff;
	switch(irq_num)
	{
		//key1
	case 99:
		//按键1中断处理
		printf("KEY1 INT\n");
		//灯亮
		if(GPIOE->ODR &=(0x1<<10))
		{
			GPIOE->ODR &= (~(0x1<<10));
		}
		else
		{
			GPIOE->ODR |=(0x1<<10);
		}
		//清除挂起中断标志位
		GICD->ICPENDR[3] |= (0x1<<3);
		//清除中断触发标志位
		EXTI->FPR1 |=(0x1<<9);
		break;
	case 97:  //key2
		//按键1中断处理
		printf("KEY2 INT\n");
		//蜂鸣器叫
		if(GPIOB->ODR &=(0x1<<6))
		{
			GPIOB->ODR &= (~(0x1<<6));
		}
		else
		{
			GPIOB->ODR |=(0x1<<6);
		}

		//清除挂起中断标志位
		GICD->ICPENDR[3] |= (0x1<<1);
		//清除中断触发标志位
		EXTI->FPR1 |=(0x1<<7);
		break;
	case 98: //key3
		//按键1中断处理
		printf("KEY3 INT\n");
		//风扇转
		if(GPIOE->ODR &= (0x1<<9))
		{
			GPIOE->ODR &=(~(0x1<<9));
		}
		else
		{
			GPIOE->ODR |=(0x1<<9);
		}
		//清除挂起中断标志位
		GICD->ICPENDR[3] |= (0x1<<2);
		//清除中断触发标志位
		EXTI->FPR1 |=(0x1<<8);
		break;
	}
		//清除处理完的中断号
		GICC->EOIR =irq_num;
}

初始化文件

#include "ctrl_key.h"

void key_config()
{
	//RCC使能GPIOF时钟   第5位置1
	RCC->MP_AHB4ENSETR |=(0x1<<5);
	//设置PF9、PF7、PF8的GPIOF输入模式 将[14:19]位置0
	GPIOF->MODER &= (~(0x3f<<14));
	//设置PF9、PF7、PF8产生EXTI事件
	//key2
	EXTI->EXTICR2 &=(~(0xff<<24));
	EXTI->EXTICR2 |=(0x5<<24);
	//key3
	EXTI->EXTICR3 &=(~(0xff));
	EXTI->EXTICR3 |=(0x05);
	//key1
	EXTI->EXTICR3 &=(~(0xff<<8));
	EXTI->EXTICR3 |=(0x05<<8);
	

	//设置事件触发方式为下降沿触发
	EXTI->FTSR1 |= (0x7<<7);
	//设置EXTI事件触发不屏蔽
	EXTI->C1IMR1 |=(0x7<<7);


	//使能组0转发中断
	GICD->CTRL |=(0x1);

	//使能中断能转发到指定的cpu接口层GICD_ISENABLERx
	//key1
	GICD->ISENABLER[3] |=(0x1<<3);
	//key2
	GICD->ISENABLER[3] |=(0x1<<1);
	//key3
	GICD->ISENABLER[3] |=(0x1<<2);
	//GICD_IPRIORITYRx寄存器 设置中断优先级
	//key1优先级
	GICD->IPRIORITYR[24] &= (~(0x1f<<27));
	//Key2优先级
	GICD->IPRIORITYR[24] &= (~(0x1f<<11));
	//key3优先级
	GICD->IPRIORITYR[24] &= (~(0x1f<<19));
	//设置当前中断被转发到哪一个cpu处理GICD_ITARGETSRx
	GICD->ITARGETSR[24] &= (~(0x3<<24));
	GICD->ITARGETSR[24] |= (0x1<<24);
	GICD->ITARGETSR[24] &= (~(0x3<<8));
	GICD->ITARGETSR[24] |= (0x1<<8);
	GICD->ITARGETSR[24] &= (~(0x3<<16));
	GICD->ITARGETSR[24] |= (0x1<<16);
	//使能cpu可以处理组0转发的中断GICC_CTLR
	GICC->CTRL |=(0x1);
	//设置中断优先级掩码GICC_PMR
	GICC->PMR |=(0x1f<<3);  //设置最低优先级
}

//初始化灯、
void all_led_init()
{
	//RCC使能
	RCC->MP_AHB4ENSETR |= (0x3<<4);
	//设置为输出模式
	GPIOE->MODER &= (~(0x3<<20));
	GPIOE->MODER |= (0x1<<20);
	
	GPIOF->MODER &= (~(0x3<<20));
	GPIOF->MODER |= (0x1<<20);

	GPIOE->MODER &= (~(0x3<<16));
	GPIOE->MODER |= (0x1<<16);

	//设置为推挽输出
	GPIOE->OTYPER &= (~(0x1<<10));
	GPIOF->OTYPER &= (~(0X1<<10));
	GPIOE->OTYPER &= (~(0X1<<8));

	//设置输出速度
	GPIOE->OSPEEDR &= (~(0X3<<20));
	GPIOF->OSPEEDR &= (~(0X3<<20));
	GPIOE->OSPEEDR &= (~(0X3<<16));

	//不设置上拉、下拉
	GPIOE->PUPDR &= ~((0X3<<20));
	GPIOF->PUPDR &= ~((0X3<<20));
	GPIOE->PUPDR &= ~((0X3<<16));
}

//初始化风扇    
void fan_init()    //PE9
{
	//RCC使能GPIOE
	RCC->MP_AHB4ENSETR |= (0x1<<4);
	//设置为输出模式
	GPIOE->MODER &= (~(0x3<<18));
	GPIOE->MODER |= (0x1<<18);
	//设置推挽输出
	GPIOE->OTYPER &= (~(0x1<<9));	
}

//初始化蜂鸣器
void voice_init()   //PB6
{

	//RCC使能GPIOB
	RCC->MP_AHB4ENSETR |=(0x1<<1);
	//设置输出模式
	GPIOB->MODER &= (~(0x3<<12));
	GPIOB->MODER |= (0x1<<12);
	//设置推挽输出
	GPIOB->OTYPER &=(~(0x1<<6));
	
}

调试主文件

#include "ctrl_key.h"

int main()
{
	//初始化灯
	all_led_init();
	//初始化风扇
	fan_init();

	//初始化蜂鸣器
	voice_init();
	//中断初始化
	key_config();
	while(1)
	{
		//保证程序不结束
	}
	return 0;
}

实验效果:

arm实验_第1张图片

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