mykey.h
#ifndef __MYKEY_H__
#define __MYKEY_H__
#include "stm32mp1xx_exti.h"
#include "stm32mp1xx_gic.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_rcc.h"
void key1_init();
void key2_init();
void key3_init();
#endif
mykey.c
#include "mykey.h"
void key1_init()
{
RCC->MP_AHB4ENSETR |= (0X3 << 4);
GPIOE->MODER &= (~(0X3<<20));
GPIOE->MODER |= (0x1<<20);
//设置PF10为输出
GPIOF->MODER&= (~(0X3<<20));
GPIOF->MODER |= (0x1<<20);
//设置PE8为输出
GPIOE->MODER &= (~(0X3<<16));
GPIOE->MODER |= (0x1<<16);
//设置PE10为推挽输出
GPIOE->OTYPER &= (~(0X1<<10));
//设置PF10为推挽输出
GPIOF->OTYPER &= (~(0X1<<10));
//设置PE8为推挽输出
GPIOE->OTYPER &= (~(0X1<<8));
//设置PE10为低速输出
GPIOE->OSPEEDR &= (~(0X3<<20));
//设置PF10为低速输出
GPIOF->OSPEEDR &= (~(0X3<<20));
//设置PE8为低速输出
GPIOE->OSPEEDR &= (~(0X3<<16));
//设置PE10输出无上拉下拉电阻
GPIOE->PUPDR &= (~(0X3<<20));
//设置PF10输出无上拉下拉电阻
GPIOF->PUPDR &= (~(0X3<<20));
//设置PE8输出无上拉下拉电阻
GPIOE->PUPDR &= (~(0X3<<16));
//三盏灯默认是关闭
GPIOE->ODR &= (~(0X1<<10));
GPIOF->ODR &= (~(0X1<<10));
GPIOE->ODR &= (~(0X1<<8));
RCC->MP_AHB4ENSETR |= (0X5 << 5);
GPIOF->MODER &= (~(0X3 << 14));
EXTI->EXTICR2 &= (~(0XFF << 24));
EXTI->EXTICR2 |= (0X5 << 24);
EXTI->FTSR1 |= (0X1 << 7);
EXTI->C1IMR1 |= (0X1 << 7);
GICD->CTRL |= 0X1;
GICD->ISENABLER[3] |= (0X1 << 1);
GICD->IPRIORITYR[24] &= (~(0X1F << 11));
GICD->ITARGETSR[24] &= (~(0X03 << 8));
GICD->ITARGETSR[24] |= (0X1 << 8);
GICC->CTRL |= 0X1;
GICC->PMR |= 0X1F;
}
void key2_init()
{
RCC->MP_AHB4ENSETR |= (0X3 << 4);
GPIOF->MODER &= (~(0X3 << 12));
GPIOF->MODER |= (0X1 << 12);
GPIOF->OTYPER &= (~(0X1<<6));
GPIOF->OSPEEDR &= (~(0X3<<12));
GPIOF->PUPDR &= (~(0X3<<12));
GPIOF->ODR &= (~(0X1<<6));
RCC->MP_AHB4ENSETR |= (0X5 << 5);
GPIOF->MODER &= (~(0X3 << 16));
EXTI->EXTICR3 &= (~(0XFF));
EXTI->EXTICR3 |= 0X5;
EXTI->FTSR1 |= (0X1 << 8);
EXTI->C1IMR1 |= (0X1 << 8);
GICD->CTRL |= 0X1;
GICD->ISENABLER[3] |= (0X1 << 2);
GICD->IPRIORITYR[24] &= (~(0X1F << 19));
GICD->ITARGETSR[24] &= (~(0X03 << 16));
GICD->ITARGETSR[24] |= (0X1 << 16);
GICC->CTRL |= 0X1;
GICC->PMR |= 0X1F;
}
void key3_init()
{
RCC->MP_AHB4ENSETR |= (0X3 << 4);
GPIOE->MODER &= (~(0X3 << 18));
GPIOE->MODER |= (0X1 << 18);
GPIOE->OTYPER &= (~(0X1<<9));
GPIOE->OSPEEDR &= (~(0X3<<18));
GPIOE->PUPDR &= (~(0X3<<18));
GPIOE->ODR &= (~(0X1<<9));
RCC->MP_AHB4ENSETR |= (0X5 << 5);
GPIOF->MODER &= (~(0X3 << 18));
EXTI->EXTICR3 &= (~(0XFF << 8));
EXTI->EXTICR3 |= (0X5 << 8);
EXTI->FTSR1 |= (0X1 << 9);
EXTI->C1IMR1 |= (0X1 << 9);
GICD->CTRL |= 0X1;
GICD->ISENABLER[3] |= (0X1 << 3);
GICD->IPRIORITYR[24] &= (~(0X1F << 27));
GICD->ITARGETSR[24] &= (~(0X03 << 24));
GICD->ITARGETSR[24] |= (0X1 << 24);
GICC->CTRL |= 0X1;
GICC->PMR |= 0X1F;
}
do_irq.c文章来源:https://www.toymoban.com/news/detail-860678.html
#include "mykey.h"
extern void printf(const char *fmt, ...);
unsigned int i = 0;
int flag1 = 0;
int flag2 = 0;
int flag3 = 0;
void do_irq(void)
{
unsigned int a = (GICC->IAR & 0X3FF);
switch (a)
{
case 97:
printf("key2\n");
flag2 = !flag2;
if(flag2 == 1)
{
GPIOF->ODR |= (0X1<<6);
}
if (flag2 == 0)
{
GPIOF->ODR &= (~(0X1<<6));
}
EXTI->FPR1 |= (0X1 << 7);
GICD->ICPENDR[3] |= (0X1 << 1);
break;
case 98:
printf("key3\n");
flag1 = !flag1;
if(flag1 == 1)
{
GPIOE->ODR |= (0X1<<10);
GPIOF->ODR |= (0X1<<10);
GPIOE->ODR |= (0X1<<8);
}
if (flag1 == 0)
{
GPIOE->ODR &= (~(0X1<<10));
GPIOF->ODR &= (~(0X1<<10));
GPIOE->ODR &= (~(0X1<<8));
}
EXTI->FPR1 |= (0X1 << 8);
GICD->ICPENDR[3] |= (0X1 << 2);
break;
case 99:
printf("key1\n");
flag3 = !flag3;
if(flag3 == 1)
{
GPIOE->ODR |= (0X1<<9);
}
if (flag3 == 0)
{
GPIOE->ODR &= (~(0X1<<9));
}
EXTI->FPR1 |= (0X1 << 9);
GICD->ICPENDR[3] |= (0X1 << 3);
break;
default:
break;
}
GICC->EOIR = a;
}
main.c文章来源地址https://www.toymoban.com/news/detail-860678.html
#include "mykey.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{
}
}
}
int main()
{
key1_init();
key2_init();
key3_init();
while (1)
{
printf("hello world\n");
delay_ms(1000);
}
}
到了这里,关于4.16 day7 ARM的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!