ARM - STM32MP157-A7内核-C代码点亮LED灯

头文件:

#ifndef __GPIO_H__
#define __GPIO_H__

//1.RCC寄存器封装,用宏定义封装
#define RCC_AHB4_ENSERT (*(volatile unsigned int *)0x50000A28)

//2.GPIO寄存器进行封装,用结构体封装
typedef struct
{
	volatile unsigned int MODER; //00
	volatile unsigned int OTYPER; //04
	volatile unsigned int OSPEEDR; //08
	volatile unsigned int PUPDR; //0C
	volatile unsigned int IDR; //10
	volatile unsigned int ODR; //14
}gpio_t;

#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)

//3.LED1灯初始化
void LED1_init();
void LED2_init();
void LED3_init();

//4.LED1灯点亮
void LED1_on();
void LED2_on();
void LED3_on();

//5.LED1灯熄灭
void LED1_off();
void LED2_off();
void LED3_off();


#endif

被调函数:

#include "gpio.h"
extern void printf(const char *fmt, ...);

//1.LED1灯初始化
void LED1_init()
{
	//0.设置GPIOE时钟使能
	RCC_AHB4_ENSERT |= (0x1 << 4);
	
	//1.设置PE10引脚为输出模式 0x50006000[21:20]= 01
	GPIOE->MODER &= (~(0x1 << 21));
	GPIOE->MODER |= (0x1 << 20);
	
	//2.设置PE10引脚为推挽输出 0x50006004[10]= 0
	GPIOE->OTYPER &= (~(0x1 << 10));
	
	//3.设置PE10引脚为低速模式 0x50006008[21:20]= 00
	GPIOE->OSPEEDR &= (~(0x1 << 20));
	GPIOE->OSPEEDR &= (~(0x1 << 21));

	//4.设置PE10引脚禁止上下拉 0x5000600C[21:20]= 00
	GPIOE->PUPDR &= (~(0x1 << 20));
	GPIOE->PUPDR &= (~(0x1 << 21));
}
void LED2_init()
{
	//0.设置GPIOE时钟使能
	RCC_AHB4_ENSERT |= (0x1 << 5);
	
	//1.设置PE10引脚为输出模式 0x50006000[21:20]= 01
	GPIOF->MODER &= (~(0x1 << 21));
	GPIOF->MODER |= (0x1 << 20);
	
	//2.设置PE10引脚为推挽输出 0x50006004[10]= 0
	GPIOF->OTYPER &= (~(0x1 << 10));
	
	//3.设置PE10引脚为低速模式 0x50006008[21:20]= 00
	GPIOF->OSPEEDR &= (~(0x1 << 20));
	GPIOF->OSPEEDR &= (~(0x1 << 21));

	//4.设置PE10引脚禁止上下拉 0x5000600C[21:20]= 00
	GPIOF->PUPDR &= (~(0x1 << 20));
	GPIOF->PUPDR &= (~(0x1 << 21));
}
void LED3_init()
{
	//0.设置GPIOE时钟使能
	RCC_AHB4_ENSERT |= (0x1 << 4);
	
	//1.设置PE10引脚为输出模式 0x50006000[17:16]= 01
	GPIOE->MODER &= (~(0x1 << 17));
	GPIOE->MODER |= (0x1 << 16);
	
	//2.设置PE10引脚为推挽输出 0x50006004[8]= 0
	GPIOE->OTYPER &= (~(0x1 << 8));
	
	//3.设置PE10引脚为低速模式 0x50006008[17:16]= 00
	GPIOE->OSPEEDR &= (~(0x1 << 17));
	GPIOE->OSPEEDR &= (~(0x1 << 16));

	//4.设置PE10引脚禁止上下拉 0x5000600C[17:16]= 00
	GPIOE->PUPDR &= (~(0x1 << 16));
	GPIOE->PUPDR &= (~(0x1 << 17));
}

//2.LED1灯点亮
void LED1_on()
{
	//设置PE10引脚输出高电平 0x50006014[10]= 1
	GPIOE->ODR |= (0x1 << 10);
}
void LED2_on()
{
	//设置PE10引脚输出高电平 0x50007014[10]= 1
	GPIOF->ODR |= (0x1 << 10);
}
void LED3_on()
{
	//设置PE10引脚输出高电平 0x50006014[10]= 1
	GPIOE->ODR |= (0x1 << 8);
}

//3.LED1灯熄灭
void LED1_off()
{
	//设置PE10引脚输出低电平 0x50006014[10]= 0
	GPIOE->ODR &= (~(0x1 << 10));
}
void LED2_off()
{
	//设置PE10引脚输出低电平 0x50006014[10]= 0
	GPIOF->ODR &= (~(0x1 << 10));
}
void LED3_off()
{
	//设置PE10引脚输出低电平 0x50006014[10]= 0
	GPIOE->ODR &= (~(0x1 << 8));
}

主函数:

#include "gpio.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 < 1800; j++);
}


int main()
{
	LED1_init();
	LED2_init();
	LED3_init();
	while(1)
	{
		LED1_on();
		delay_ms(200);
		LED1_off();
		delay_ms(200);

		LED2_on();
		delay_ms(200);
		LED2_off();
		delay_ms(200);

		LED3_on();
		delay_ms(200);
		LED3_off();
		delay_ms(200);
	}
	return 0;
}

现象:

VID_20220913_203122

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding Peasant

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值