本文主要是介绍STM32MP157按键中断实验,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
按键配置
#include "key_it.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_rcc.h"void key_it_config()
{/* RCC使能GPIOF时钟 */RCC->MP_AHB4ENSETR |= (1<<5);/* 设置PF9 PF7 PF8输入 */GPIOF->MODER &= (~(0x3 << 14));GPIOF->MODER &= (~(0x3 << 16));GPIOF->MODER &= (~(0x3 << 18));/* 设置PF9 PF7 PF8产生EXTI事件 EXTI_EXTICRx */EXTI->EXTICR2 &= ~(0xff<<24);EXTI->EXTICR2 |= (0x05<<24);EXTI->EXTICR3 &= ~(0xff);EXTI->EXTICR3 |= (0x05);EXTI->EXTICR3 &= ~(0xff<<8);EXTI->EXTICR3 |= (0x05<<8);/* 设置事件触发方式为下降沿触发EXTI_FISR1 */EXTI->FTSR1 |= (1<<7);EXTI->FTSR1 |= (1<<8);EXTI->FTSR1 |= (1<<9);/* 设置EXTI事件触发不屏蔽XITI_IMR1 */EXTI->C1IMR1 |= (1<<7);EXTI->C1IMR1 |= (1<<8);EXTI->C1IMR1 |= (1<<9);/* 使能中断能转发到特定的CPU接口层GICD_ISENABLERx */GICD->ISENABLER[3] |= (1<<3);GICD->ISENABLER[3] |= (1<<2);GICD->ISENABLER[3] |= (1<<1);/* GICD_IPPIORITYRx设置中断优先级 */GICD->IPRIORITYR[24] &= ~(0x1f<<27);GICD->IPRIORITYR[24] &= ~(0x1f<<19);GICD->IPRIORITYR[24] &= ~(0x1f<<11);/* 设置当前终端被转发到哪一个CPU处理GIC_ITARGETSRx */GICD->ITARGETSR[24] &= ~(0x03<<24);GICD->ITARGETSR[24] |= (0x01<<24);GICD->ITARGETSR[24] &= ~(0x03<<16);GICD->ITARGETSR[24] |= (0x01<<16);GICD->ITARGETSR[24] &= ~(0x03<<8);GICD->ITARGETSR[24] |= (0x01<<8);/* 使能组0转发终端GICD_CTLR */GICD->CTRL |= 0x1;/* 设置中断优先级掩码GICC_PMR */GICC->PMR |= (0x1f<<3);/* 使能CPU可以处理组0转发的终端GICC_CTLR */GICC->CTRL |= 0x1;}
中断实现
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_exti.h"
#include "beep.h"#define EXTI9_IRQ 99
#define EXTI8_IRQ 98
#define EXTI7_IRQ 97extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{/* 保存终端号 */int irqno;irqno = GICC->IAR & 0x3ff;if(irqno == EXTI9_IRQ){delay(1000);if(irqno == EXTI9_IRQ){/* KEY1中断处理 */printf("KEY1 pressed!\n\r");/* 蜂鸣器 */hal_gpio_write(GPIOB, 6, !hal_gpio_read(GPIOB, 6));/* 清除挂起中断标志位GICD_ICPENDRx */GICD->ICPENDR[3] |= (1<<3); /* 清除终端触发标志EXTI_FRR1 */EXTI->FPR1 |= (1<<9);}}else if(irqno == EXTI8_IRQ){delay(1000);if(irqno == EXTI8_IRQ){/* KEY3中断处理 */printf("KEY3 pressed!\n\r");/* 风扇 */hal_gpio_write(GPIOE, 8, !hal_gpio_read(GPIOE, 8));/* 清除挂起中断标志位GICD_ICPENDRx */GICD->ICPENDR[3] |= (1<<2);/* 清除终端触发标志EXTI_FRR1 */EXTI->FPR1 |= (1<<8);}}else if(irqno == EXTI7_IRQ){delay(1000);if(irqno == EXTI7_IRQ){/* KEY2中断处理 */printf("KEY2 pressed!\n\r");/* LED灯 */hal_gpio_write(GPIOE, 9, !hal_gpio_read(GPIOE, 9));/* 清除挂起中断标志位GICD_ICPENDRx */GICD->ICPENDR[3] |= (1<<1);/* 清除终端触发标志EXTI_FRR1 */EXTI->FPR1 |= (1<<7);}}/* 清除处理完的中断号GICC_EOIR */GICC->EOIR = irqno;}
这篇关于STM32MP157按键中断实验的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!