gpio.h文件
#ifndef __GPIO_H__
#define __GPIO_H__
#define RCC_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
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)
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()
{
RCC_AHB4_ENSETR |= (0x1 << 4);
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |= (0x1 << 20);
GPIOE->OTYPER &= (~(0x1 << 10));
GPIOE->OSPEEDR &= (~(0x3 << 20));
GPIOE->PUPDR &= (~(0x3 << 20));
}
void LED1_on()
{
GPIOE->ODR |= (0x1 << 10);
}
void LED1_off()
{
GPIOE->ODR &=(~(0x1 << 10));
}
void LED2_init()
{
RCC_AHB4_ENSETR |= (0x1 << 5);
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
GPIOF->OTYPER &= (~(0x1 << 20));
GPIOF->OSPEEDR &= (~(0x3 << 20));
GPIOF->PUPDR &= (~(0x3 << 20));
}
void LED2_init()
{
RCC_AHB4_ENSETR |= (0x1 << 5);
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
GPIOF->OTYPER &= (~(0x1 << 20));
GPIOF->OSPEEDR &= (~(0x3 << 20));
GPIOF->PUPDR &= (~(0x3 << 20));
}
void LED2_on()
{
GPIOF->ODR |= (0x1 << 10);
}
void LED2_off()
{
GPIOF->ODR &=(~(0x1 << 10));
}
void LED3_init()
{
RCC_AHB4_ENSETR |= (0x1 << 4);
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 16);
GPIOE->OTYPER &= (~(0x1 << 16));
GPIOE->OSPEEDR &= (~(0x3 << 16));
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();
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;
}
测试结果