gpio.h
#ifndef __GPIO_H__
#define __GPIO_H__
#define RCC_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
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)
void LED1_init();
void LED1_on();
void LED1_off();
void LED2_init();
void LED2_on();
void LED2_off();
void LED3_init();
void LED3_on();
void LED3_off();
#endif
gpio.c
#include"gpio.h"
void LED1_init()
{
//0.设置GPIOE时钟使能
RCC_AHB4_ENSETR |=(0x1 << 4);
//1.设置PE10引脚为输出模式
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |=(0x1 << 20);
//2.设置PE10引脚为推挽模式
GPIOE->OTYPER &=(~(0x1 << 10));
//3.设置PE10引脚为低速模式
GPIOE->OSPEEDR &=(~(0x3 << 20));
//4.设置PE10引脚禁止上下拉
GPIOE->PUPDR &=(~(0x3 << 20));
}
void LED2_init()
{
//0.设置GPIOF时钟使能
RCC_AHB4_ENSETR |=(0x1 << 5);
//1.设置PF10引脚为输出模式
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |=(0x1 << 20);
//2.设置PF10引脚为推挽模式
GPIOF->OTYPER &=(~(0x1 << 10));
//3.设置PF10引脚为低速模式
GPIOF->OSPEEDR &=(~(0x3 << 20));
//4.设置PF10引脚禁止上下拉
GPIOF->PUPDR &=(~(0x3 << 20));
}
void LED1_on()
{
GPIOE->ODR |=(0x1 << 10);
}
void LED1_off()
{
GPIOE->ODR &=(~(0x1 << 10));
}
void LED2_on()
{
GPIOF->ODR |=(0x1 << 10);
}
void LED2_off()
{
GPIOF->ODR &=(~(0x1 << 10));
}
void LED3_init()
{
//0.设置GPIOE时钟使能
RCC_AHB4_ENSETR |=(0x1 << 5);
//1.设置PE10引脚为输出模式
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |=(0x1 << 16);
//2.设置PE10引脚为推挽模式
GPIOE->OTYPER &=(~(0x1 << 8));
//3.设置PE10引脚为低速模式
GPIOE->OSPEEDR &=(~(0x3 << 16));
//4.设置PE10引脚禁止上下拉
GPIOE->PUPDR &=(~(0x3 << 16));
}
void LED3_on()
{
GPIOE->ODR |=(0x1 << 8);
}
void LED3_off()
{
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(); // LED灯初始化
LED2_init();
LED3_init();
while(1)
{
LED1_on();
LED2_on();
LED3_on();
delay_ms(1000);
LED1_off();
LED2_off();
LED3_off();
delay_ms(1000);
}
return 0;
}