设置按键中断,按键1按下,LED亮,再按一次,灭
按键2按下,蜂鸣器响。再按一次,不响
按键3按下,风扇转,再按一次,风扇停
keyit.h:
#ifndef __KEYIT_H__
#define __KEYIT_H__#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"void key_it_init();
void led_init();
void beep_init();
void fan_init();
#endif
keyit.c:
#include"keyit.h"void key_it_init(){//RCC是能GPIOF时钟RCC->MP_AHB4ENSETR |= (0x1<<5);//设置PF9 PF7 PF8的GPIO输入模式GPIOF->MODER &= (~(0x3f<<14));//设置PF9 PF7 PF8产生EXTI事件//设置PF7EXTI->EXTICR2 &= (~(0xff<<24));EXTI->EXTICR2 |= (0x05<<24);//设置PF8,PF9EXTI->EXTICR3 &= (~(0xff));EXTI->EXTICR3 |= (0x05);EXTI->EXTICR3 &= (~(0xff<<8));EXTI->EXTICR3 |= (0x05<<8);//设置事件触发方式为下降沿触发EXTI->FTSR1 |= (0x7<<7);//设置EXTI事件触发不屏蔽EXTI->C1IMR1 |= (0x7<<7);//使能中断能转发到待定的CPU接口层GICD->ISENABLER[3] |= (0x7<<1);//设置中断优先级//设置按键1,PF9的优先级GICD->IPRIORITYR[24] &= (~(0x1f<<27));//设置按键2,PF7的优先级GICD->IPRIORITYR[24] &= (~(0x1f<<11));//设置按键3,PF8的优先级 GICD->IPRIORITYR[24] &= (~(0x1f<<19));//设置当前中断被转发到哪一个CPU处理//设置按键1对应中断处理程序由CPU0处理GICD->ITARGETSR[24] &= (~(0x3<<24));GICD->ITARGETSR[24] |= (0x1<<24);//设置按键2对应中断处理程序由CPU0处理GICD->ITARGETSR[24] &= (~(0x3<<8));GICD->ITARGETSR[24] |= (0x1<<8);//设置按键3对应中断处理程序由CPU0处理GICD->ITARGETSR[24] &= (~(0x3<<16));GICD->ITARGETSR[24] |= (0x1<<16);//使能组0转发中断GICD->CTRL |= 0x1;//设置中断优先级掩码GICC->PMR |= (0xf<<3);//使能CPU可以处理组0转发的中断GICC->CTRL |= (0x1);}void led_init(){//将gpioe和GPIOF的时钟使能RCC->MP_AHB4ENSETR |= (0x3<<4);//将三盏灯对应的gpio口的模式设置为输出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->MODER |= (0x1<<10);GPIOF->MODER |= (0x1<<10);GPIOE->MODER |= (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 beep_init(){//将gpiob的时钟使能RCC->MP_AHB4ENSETR |= (0x1<<1);//讲蜂鸣器对应的PB6口的模式设置为输出模式GPIOB->MODER &= (~(0x3<<12));GPIOB->MODER |= (0x1<<12);//将输出模式设置为推挽输出GPIOB->MODER |= (0x1<<6);//讲输出设置为低速GPIOB->OSPEEDR &= (~(0x3<<12));//设置无上拉下拉电阻GPIOB->PUPDR &= (~(0x3<<12));
}void fan_init(){//将gpiob的时钟使能RCC->MP_AHB4ENSETR |= (0x1<<4);//讲风扇对应的PE9口的模式设置为输出模式GPIOE->MODER &= (~(0x3<<18));GPIOE->MODER |= (0x1<<18);//将输出模式设置为推挽输出GPIOE->MODER |= (0x1<<9);//讲输出设置为低速GPIOE->OSPEEDR &= (~(0x3<<18));//设置无上拉下拉电阻GPIOE->PUPDR &= (~(0x3<<18));
}
do_irq.c:
//extern void printf(const char *fmt, ...);
#include "keyit.h"
void delay(int ms){for(int i=0; i<ms; i++){for(int j=0; j<2000; j++){}}
}
unsigned int flag_led = 0;
unsigned int flag_beep = 0;
unsigned int flag_fan = 0;
void do_irq(void)
{int irqno;//保存中断号irqno = GICC->IAR&0x3FF;switch(irqno){case 99://key1//按键1中断处理if(flag_led==0){GPIOE->ODR |= (0x1<<10);GPIOF->ODR |= (0x1<<10);GPIOE->ODR |= (0x1<<8);flag_led = 1;}else{GPIOE->ODR &= (~(0x1<<10));GPIOF->ODR &= (~(0x1<<10));GPIOE->ODR &= (~(0x1<<8));flag_led = 0;}//清除挂起中断标志位GICD->ICPENDR[3] |= (0x1<<3);//清除中断触发标志位EXTI->FPR1 |= (0x1<<9);//清除处理完的中断号GICC->EOIR |= irqno;break;case 97://key2//按键2中断处理if(flag_beep==0){GPIOB->ODR |= (0x1<<6);flag_beep = 1;}else{GPIOB->ODR &= (~(0x1<<6));flag_beep = 0;}//清除挂起中断标志位GICD->ICPENDR[3] |= (0x1<<1);//清除中断触发标志位EXTI->FPR1 |= (0x1<<7);//清除处理完的中断号GICC->EOIR |= irqno;break;case 98://key3//按键3中断处理if(flag_fan==0){GPIOE->ODR |= (0x1<<9);flag_fan = 1;}else{GPIOE->ODR &= (~(0x1<<9));flag_fan = 0;}//清除挂起中断标志位GICD->ICPENDR[3] |= (0x1<<2);//清除中断触发标志位EXTI->FPR1 |= (0x1<<8);//清除处理完的中断号GICC->EOIR |= irqno;break;}}
main.c:
#include"keyit.h"int main()
{led_init();key_it_init();beep_init();fan_init();while(1){}return 0;
}
效果图:
LED:
风扇:
蜂鸣器无可见现象
按键中断控制led,蜂鸣器和风扇