ARMday7

发布时间:2023年12月26日

stm32mp157AAA三个按键中断实现led灯亮灭

key_it.h

#ifndef __KEY_IT_H__
#define __KEY_IT_H__
#include"stm32mp1xx_gpio.h"
#include"stm32mp1xx_gic.h"
#include"stm32mp1xx_exti.h"
#include"stm32mp1xx_rcc.h"
#define RCC_GPIO (*(unsigned int*)0x50000a28)

void all_led_init();
void led1_on();
void led1_off();
void led2_on();
void led2_off();
void led3_on();
void led3_off();
void key1_it_config();
void key2_it_config();
void key3_it_config();


#endif

led.c

#include "key_it.h"
void all_led_init()
{
    RCC_GPIO |= (0X3<<4);//时钟使能
    GPIOE->MODER &=(~(0X3<<20));//设置PE10输出
    GPIOE->MODER |= (0X1<<20);
    //设置PE10为推挽输出
    GPIOE->OTYPER &=(~(0x1<<10));
    //PE10为低速输出
    GPIOE->OSPEEDR &= (~(0x3<<20));
    //设置无上拉下拉
    GPIOE->PUPDR &= (~(0x3<<20));
 
    //LED2
    GPIOF->MODER &=(~(0X3<<20));//设置Pf10输出
    GPIOF->MODER |= (0X1<<20);
    //设置Pf10为推挽输出
    GPIOF->OTYPER &=(~(0x1<<10));
    //Pf10为低速输出
    GPIOF->OSPEEDR &= (~(0x3<<20));
    //设置无上拉下拉
    GPIOF->PUPDR &= (~(0x3<<20));
 
    //LED3
    GPIOE->MODER &=(~(0X3<<16));//设置PE8输出
    GPIOE->MODER |= (0X1<<16);
    //设置PE8为推挽输出
    GPIOE->OTYPER &=(~(0x1<<8));
    //PE8为低速输出
    GPIOE->OSPEEDR &= (~(0x3<16));
    //设置无上拉下拉
    GPIOE->PUPDR &= (~(0x3<<16));
}
void led1_on()
{
    GPIOE->ODR |= (0x1<<10);
}
 
void led1_off()
{
    GPIOE->ODR &= (~(0x1<<10));
}
void led2_on()
{
    GPIOF->ODR |= (0x1<<10);
}
 
void led2_off()
{
    GPIOF->ODR &= (~(0x1<<10));
}
void led3_on()
{
    GPIOE->ODR |= (0x1<<8);
}
 
void led3_off()
{
    GPIOE->ODR &= (~(0x1<<8));
    
}

key_it.c

#include"key_it.h"
void key1_it_config()
{
    //使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF9管脚为输入
    GPIOF->MODER &= (~(0x3<<18));
    //设置PF9作为EXTI9事件的输入
    EXTI->EXTICR3 &= (~(0x1f<<8));
    EXTI->EXTICR3 |= (0x05<<8);
    //设置下降沿使能检测EXTI9事件
    EXTI->FTSR1 |= (0x1<<9);
    //设置EXTI9事件不屏蔽
    EXTI->C1IMR1 |= (0x1<<9);
    //GICD使能EXTI9(99号)中断
    GICD->ISENABLER[3] |= (0x1<<3);
    //GICD 设置99号中断的优先级
    GICD->IPRIORITYR[24] &= (~(0x1f<<27));
    //选择CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0x1<<24);
    //全局使能组0中断被转发到GICC
    GICD->CTRL |= (0X1);
    //设置中断的优先级掩码
    GICC->PMR |= (0x1f<<3);
    //允许组0中断被CPU处理
    GICC->CTRL |= (0x1);
}
void key2_it_config()
{
     //使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF7管脚为输入
    GPIOF->MODER &= (~(0x3<<14));
    //设置PF7作为EXTI7事件的输入
    EXTI->EXTICR2 &= (~(0x1f<<24));
    EXTI->EXTICR2 |= (0x05<<24);
    //设置下降沿使能检测EXTI7事件
    EXTI->FTSR1 |= (0x1<<7);
    //设置EXTI7事件不屏蔽
    EXTI->C1IMR1 |= (0x1<<7);
    //GICD使能EXTI7(97号)中断
    GICD->ISENABLER[3] |= (0x1<<1);
    //GICD 设置97号中断的优先级
    GICD->IPRIORITYR[24] &= (~(0x1f<<11));
    //选择CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0x1<<8);
    //全局使能组0中断被转发到GICC
    GICD->CTRL |= (0X1);
    //设置中断的优先级掩码
    GICC->PMR |= (0x1f<<3);
    //允许组0中断被CPU处理
    GICC->CTRL |= (0x1);
}
void key3_it_config()
{
     //使能GPIOF时钟
    RCC->MP_AHB4ENSETR |= (0x1<<5);
    //设置PF8管脚为输入
    GPIOF->MODER &= (~(0x3<<16));
    //设置PF8作为EXTI8事件的输入
    EXTI->EXTICR3 &= (~(0x1f));
    EXTI->EXTICR3 |= (0x05);
    //设置下降沿使能检测EXTI8事件
    EXTI->FTSR1 |= (0x1<<8);
    //设置EXTI8事件不屏蔽
    EXTI->C1IMR1 |= (0x1<<8);
    //GICD使能EXTI8(98号)中断
    GICD->ISENABLER[3] |= (0x1<<2);
    //GICD 设置98号中断的优先级
    GICD->IPRIORITYR[24] &= (~(0x1f<<19));
    //选择CPU0处理当前中断
    GICD->ITARGETSR[24] |= (0x1<<16);
    //全局使能组0中断被转发到GICC
    GICD->CTRL |= (0X1);
    //设置中断的优先级掩码
    GICC->PMR |= (0x1f<<3);
    //允许组0中断被CPU处理
    GICC->CTRL |= (0x1);
}

do_irq.c

#include "key_it.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
void do_irq(void)
{
    int irqno = (GICC->IAR & 0x3ff);
    switch (irqno)
    {
    case 99: // key1
        printf("KEY1_INT\n");
        led1_on();
        led2_off();
        led3_off();
        // 清除GICD的中断排队标志
        GICD->ICPENDR[3] |= (0x1<<3);
        // 清除exti的事件触发标志
        EXTI->FPR1 |= (0x1<<9);
        break;
    case 97: // KEY2
        printf("KEY2_INT\n");
        led1_off();
        led2_on();
        led3_off();
        // 清除GICD的中断排队标志
         GICD->ICPENDR[1] |= (0x1);
        // 清除exti的事件触发标志
         EXTI->FPR1 |= (0x1<<7);
        break;
    case 98: // key3
        printf("KEY3_INT\n");
        led1_off();
        led2_off();
        led3_on();
        // 清除GICD的中断排队标志
         GICD->ICPENDR[2] |= (0x1<<2);
        // 清除exti的事件触发标志
         EXTI->FPR1 |= (0x1<<8);
        break;
    }
    // 清除中断号
    GICC->EOIR = irqno;
}

main.c

#include "key_it.h"

 

void delay(int ms)

{

  int i,j;

  for(i=0;i<ms;i++)

  {

    for(j=0;j<2000;j++);

  }

}

 

int main()

{

  all_led_init();

  key1_it_config();

  key2_it_config();

  key3_it_config();

    while (1)

    {

        printf("do main...\n");

        delay(1000);

    }

}

运行结果:

文章来源:https://blog.csdn.net/qq_56986199/article/details/135228862
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。