头文件:
#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