2022/10/1——基于stm32mp157a的A7核LED实验

分析开发板电路图可得:LED1--->PE10   LED2--->PF10   LED3--->PE8

一、实验硬件的试验

在串口工具的交互模式下使用如下命令,如果发现三盏LED灯全部点亮则说明硬件完好

gpio toggle gpioe10
gpio toggle gpioe8
gpio toggle gpiof10

二、gpio.h——功能函数的声明与所用寄存器的封装

#ifndef __GPIO_H__
#define __GPIO_H__

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

//2.GPIO寄存器进行封装,用结构体进行封装
typedef struct{
	volatile unsigned int MODER;
	volatile unsigned int OTYPER;
	volatile unsigned int OSPEEDR;
	volatile unsigned int PUPDR;
	volatile unsigned int IDR;
	volatile unsigned int ODR;
}gpio_t;
#define GPIOE ((gpio_t*)0x50006000)
#define GPIOF ((gpio_t*)0x50007000)

//3.LED1灯初始化
void LED1_init();
//4.LED1灯点亮
void LED1_on();
//5.LED1灯熄灭
void LED1_off();

//6.LED2灯初始化
void LED2_init();
//7.LED2灯点亮
void LED2_on();
//8.LED2灯熄灭
void LED2_off();

//9.LED3灯初始化
void LED3_init();
//10.LED3灯点亮
void LED3_on();
//11.LED3灯熄灭
void LED3_off();

三、gpio.c——功能函数的定义

以LED1为例

1、LED1灯初始化

void LED1_init()
{
	//0.设置GPIOE时钟使能
	RCC_AHB4_ENSETR |= (0x1 << 4);
	
	//1.设置PE10引脚为输出模式
	GPIOE->MODER |= (0x1 << 20);
	GPIOE->MODER &= (~(0x1 << 21));

	//2.设置PE10引脚为推挽模式
	GPIOE->OTYPER &= (~(0x1 << 10));

	//3.设置PE10引脚为低速模式
	GPIOE->OSPEEDR &= (~(0x3 << 20));

	//4.设置PE10引脚禁止上下拉
	GPIOE->PUPDR &= (~(0x3 << 20));
}

2、LED1的点亮和熄灭

//LED1灯点亮
void LED1_on()
{
	//1.设置PE10引脚输出高电平
	GPIOE->ODR |= (0x1 << 10);
}
//LED1灯熄灭
void LED1_off()
{
	//1.设置PE10引脚输出低电平
	GPIOE->ODR &= (~(0x1 << 10));
}

3、完整代码

#include "gpio.h"


//1.LED1灯初始化
void LED1_init()
{
	//0.设置GPIOE时钟使能
	RCC_AHB4_ENSETR |= (0x1 << 4);
	
	//1.设置PE10引脚为输出模式
	GPIOE->MODER |= (0x1 << 20);
	GPIOE->MODER &= (~(0x1 << 21));

	//2.设置PE10引脚为推挽模式
	GPIOE->OTYPER &= (~(0x1 << 10));

	//3.设置PE10引脚为低速模式
	GPIOE->OSPEEDR &= (~(0x3 << 20));

	//4.设置PE10引脚禁止上下拉
	GPIOE->PUPDR &= (~(0x3 << 20));
}
//2.LED1灯点亮
void LED1_on()
{
	//1.设置PE10引脚输出高电平
	GPIOE->ODR |= (0x1 << 10);
}
//3.LED1灯熄灭
void LED1_off()
{
	//1.设置PE10引脚输出低电平
	GPIOE->ODR &= (~(0x1 << 10));
}

//4.LED2灯初始化
void LED2_init()
{
	//0.设置GPIOF时钟使能
	RCC_AHB4_ENSETR |= (0x1 << 5);
	
	//1.设置PF10引脚为输出模式
	GPIOF->MODER |= (0x1 << 20);
	GPIOF->MODER &= (~(0x1 << 21));

	//2.设置PF10引脚为推挽模式
	GPIOF->OTYPER &= (~(0x1 << 10));

	//3.设置PF10引脚为低速模式
	GPIOF->OSPEEDR &= (~(0x3 << 20));

	//4.设置PF10引脚禁止上下拉
	GPIOF->PUPDR &= (~(0x3 << 20));
}
//5.LED1灯点亮
void LED2_on()
{
	//1.设置PE10引脚输出高电平
	GPIOF->ODR |= (0x1 << 10);
}
//6.LED1灯熄灭
void LED2_off()
{
	//1.设置PE10引脚输出低电平
	GPIOF->ODR &= (~(0x1 << 10));
}

//7.LED3灯初始化
void LED3_init()
{
	//0.设置GPIOE时钟使能
	RCC_AHB4_ENSETR |= (0x1 << 4);
	
	//1.设置PE8引脚为输出模式
	GPIOE->MODER |= (0x1 << 16);
	GPIOE->MODER &= (~(0x1 << 17));

	//2.设置PE8引脚为推挽模式
	GPIOE->OTYPER &= (~(0x1 << 8));

	//3.设置PE8引脚为低速模式
	GPIOE->OSPEEDR &= (~(0x3 << 16));

	//4.设置PE8引脚禁止上下拉
	GPIOE->PUPDR &= (~(0x3 << 16));
}
//8.LED3灯点亮
void LED3_on()
{
	//1.设置PE8引脚输出高电平
	GPIOE->ODR |= (0x1 << 8);
}
//9.LED3灯熄灭
void LED3_off()
{
	//1.设置PE8引脚输出低电平
	GPIOE->ODR &= (~(0x1 << 8));
}

四、main.c——延时函数和功能函数的引用

1、延时函数

void delay_ms(int ms)
{
	int i,j;
	for(i = 0; i < ms;i++)
		for (j = 0; j < 1800; j++);
}

2、完整代码——实现流水灯

#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亮起
		LED1_on();
		delay_ms(1000);
		//经过约1秒后LED2亮起
		LED2_on();
		delay_ms(1000);
		//经过约1秒后LED2亮起
		LED3_on();
		delay_ms(1000);
    	//LED1熄灭
		LED1_off();
		delay_ms(1000);
		//1秒后LED2熄灭
		LED2_off();
		delay_ms(1000);
		//1秒后LED3熄灭
		LED3_off();
		delay_ms(1000);
	}
	return 0;

}

你可能感兴趣的:(arm复习,stm32,单片机,arm)