STM32 多功能按键中断

key1 开关实现led1亮灭,key2开关实现蜂鸣器开关,key3开关实现风扇开关

main.c

#include "uart.h"
#include "key_it.h"
#include "led.h"
int main()
{
    char c;
    char *s;
    uart4_init();//串口初始化
	all_led_init();
	key_it_config();
	fengshan_init();
	key3_it_config();
    while(1)
    {
		
    }
    return 0;
}

 按键功能函数 

#include"key_it.h"
void key3_it_config()
{	RCC->MP_AHB4ENSETR |= (0x1<<5);

	//PF8
	GPIOF->MODER &= (~(0x3<<16));
	//PF8
	EXTI->EXTICR3 &= (~(0xFF<<0));
	EXTI->EXTICR3 |= (0x5<<0);
	//设置触发方式为下降沿触发EXTI_FTSR1
	EXTI->FTSR1 |= (0x1<<8);
	//设置EXTI事件触发不屏蔽 EXTI_IMR1
	EXTI->C1IMR1 |=(0x1<<8);
	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);

	RCC->MP_AHB4ENSETR |= (0x1<<1);
	
	RCC->MP_AHB4ENSETR |= (0x1<<4);
	//设置PF9 PF7 PF8 GPIO_MODER输入
	//PF9
	GPIOF->MODER &= (~(0x3<<18)); 
	//PF7
	GPIOF->MODER &= (~(0x3<<14));
	GICD->ISENABLER[3] |= (0x1<<2);   //PF8 98号中断 
	//PB6 蜂鸣器
	GPIOB->MODER &= (~(0x3<<12));
	GPIOB->MODER |=(0x1<<12);
	GPIOB->OTYPER &=(~(0x1<<6));
	GPIOB->OSPEEDR &= (~(0x3<<12));
	GPIOB->PUPDR &= (~(0x3<<12));
	//设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx
	//PF7
	EXTI->EXTICR2 &= (~(0xFF<<24));
	EXTI->EXTICR2 |= (0x5<<24);

	//pF9
	EXTI->EXTICR3 &= (~(0xFF)<<8);
	EXTI->EXTICR3 |= (0x5<<8);	
	//设置触发方式为下降沿触发EXTI_FTSR1
	EXTI->FTSR1 |= (0x7<<7);
	//设置EXTI事件触发不屏蔽 EXTI_IMR1
	EXTI->C1IMR1 |=(0x7<<7);
	//使能组0转发中断GICD_CTRL
	GICD->CTRL |=(0x1);
	//使能中断能转发到特定的CPU接口层
	GICD->ISENABLER[3] |= (0x1<<3);   //PF9 99号中断 

	GICD->ISENABLER[3] |= (0x1<<1);   //PF7 97号中断
	//GICD_IPRIOITYR设置中断优先级
	GICD->IPRIORITYR[24] &= (~(0x1F)<<11);

	//PF9
	GICD->IPRIORITYR[24] &= (~(0x1F<<27));
	//设置当前中断被转发特定的CPU接口层GICO_ITARGETSRx
	GICD->ITARGETSR[24] &= (~(0x3<<8));
	GICD->ITARGETSR[24] |= (0x1<<8);

	GICD->ITARGETSR[24] &= (~(0x3<<24));
	GICD->ITARGETSR[24] |= (0x1<<24);
	//使能CPU可以处理组0转发的中断GICD_CTLR
	GICC->CTRL |= 0x1;
	//设置中断优先级掩码GICC_CTKR
	GICC->PMR |= (0x1F<<3);
	GICC->CTRL |= (0x1);
	
}
void fengshan_init()
{
	RCC->MP_AHB4ENSETR |= (0x1<<4);
	//PB9
	GPIOE->MODER &= (~(0x3<<18));
	GPIOE->MODER |= (0x1<<18);
	GPIOE->OTYPER &=(~(0x1<<9));
	GPIOE->OSPEEDR &= (~(0x3<<18));
	GPIOE->PUPDR &= (~(0x3<<18));
}

 头文件 key_it.h

#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
void key_it_config();
void key3_it_config();
void fengshan_init();
#endif

 led的功能函数 led.c

#include"led.h"
//延时函数
void delay(int ms)
{
	int i,j;
	for(i=0;iMP_AHB4ENSETR |= (0x3<<20);
	//设置PE10 PF10 PE8为输出
	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));
	GPIOE->PUPDR &= (~(0x3<<20));
	GPIOE->PUPDR &= (~(0x3<<20));
	



}

中断处理函数实现do_irq.c

#include "key_it.h"
#include "led.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
int flag=0;
void do_irq(void) 
{
	int irqno; //保存中断号
	irqno=GICC->IAR & 0x3FF;
	switch(irqno)
	{
	case 99://key1
		//按键1中断处理
		printf("key1\n");
		printf("key1 ID =%d\n",irqno);
		//LED亮
		if(flag==0)
		{
		GPIOE->ODR |=(0x1<<10);
		flag=1;
		//GPIOE.odr &=(~(0x1<<10));
		//GPIOE.odr &= (~(0x1<<8));
		}else
		{
			flag=0;
			GPIOE->ODR &=(~(0x1<<10));

		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |=(0x1<<3);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1<<9);
		break;
	case 97:
		//按键2中断处理
		printf("key2\n");
		printf("key2 ID =%d\n",irqno);

		if(flag==0)
		{
		GPIOB->ODR |=(0x1<<6);
		flag=1;
		}
		else
		{
			flag=0;
			GPIOB->ODR &= (~(0x1<<6));
		}
		//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |=(0x1<<1);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1<<7);
		break;
	case 98:

		//按键3中断处理
		printf("key3\n");
		printf("key3 ID =%d\n",irqno);
		
		if(flag==0)
		{
		GPIOE->ODR |=(0x1<<9);
		flag=1;
		}
		else
		{
			flag=0;
			GPIOE->ODR &= (~(0x1<<9));
		}
//清除挂起中断标志位GICD_ICPENDRx
		GICD->ICPENDR[3] |=(0x1<<2);
		//清除中断触发标志位EXTI_FPR1
		EXTI->FPR1 |= (0x1<<8);

	}

		//清除处理完的中断号
		GICC->EOIR=irqno;
}

STM32 多功能按键中断_第1张图片STM32 多功能按键中断_第2张图片

你可能感兴趣的:(单片机,嵌入式硬件)