目录
代码
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; //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();
//4.LED1灯点亮
void LED1_on();
//5.LED1灯熄灭
void LED1_off();
//3.LED1灯初始化
void LED2_init();
//4.LED1灯点亮
void LED2_on();
//5.LED1灯熄灭
void LED2_off();
//3.LED1灯初始化
void LED3_init();
//4.LED1灯点亮
void LED3_on();
//5.LED1灯熄灭
void LED3_off();
#endif
gpio.c
#include "gpio.h"
//1..LED1灯初始化
void LED1_init()
{
//GPIO时钟使能
RCC_AHB4_ENSETR |= (0x1<<4);
//pe10引脚输出模式
GPIOE->MODER &= (~(0x3<<20));
GPIOE->MODER |=(0x1<<20);
//推免输出
GPIOE->OTYPER &= (~(0x1<<10));
//低速模式
GPIOE->OSPEEDR &=(~(0x3<<20));
//禁止上下拉
GPIOE->PUPDR &=(~(0x3<<20));
}
void LED2_init()
{
//GPIO时钟使能
RCC_AHB4_ENSETR |= (0x1<<5);
//pe10引脚输出模式
GPIOF->MODER &= (~(0x3<<20));
GPIOF->MODER |=(0x1<<20);
//推免输出
GPIOF->OTYPER &= (~(0x1<<10));
//低速模式
GPIOF->OSPEEDR &=(~(0x3<<20));
//禁止上下拉
GPIOF->PUPDR &=(~(0x3<<20));
}
void LED3_init()
{
//GPIO时钟使能
RCC_AHB4_ENSETR |= (0x1<<4);
//pe10引脚输出模式
GPIOE->MODER &= (~(0x3<<16));
GPIOE->MODER |=(0x1<<16);
//推免输出
GPIOE->OTYPER &= (~(0x1<<8));
//低速模式
GPIOE->OSPEEDR &=(~(0x3<<16));
//禁止上下拉
GPIOE->PUPDR &=(~(0x3<<16));
}
//2..LED1灯点亮
void LED1_on()
{
//设置PE10引脚输出高电平
GPIOE->ODR |= (0x1<<10);
}
//3.LED1灯熄灭
void LED1_off()
{
//设置PE10引脚输出低电平
GPIOE->ODR &= (~(0x1<<10));
}
//2..LED1灯点亮
void LED2_on()
{
//设置PE10引脚输出高电平
GPIOF->ODR |= (0x1<<10);
}
//3.LED1灯熄灭
void LED2_off()
{
//设置PE10引脚输出低电平
GPIOF->ODR &= (~(0x1<<10));
}
//2..LED1灯点亮
void LED3_on()
{
//设置PE10引脚输出高电平
GPIOE->ODR |= (0x1<<8);
}
//3.LED1灯熄灭
void LED3_off()
{
//设置PE10引脚输出低电平
GPIOE->ODR &= (~(0x1<<8));
}
main.c
#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(1000);
LED2_on();
delay_ms(1000);
LED3_on();
delay_ms(1000);
LED1_off();
LED2_off();
LED3_off();
delay_ms(1000);
}
return 0;
}
运行效果