.led.h
#ifndef __LED_H__
#define __LED_H__
//构建寄存器组织结构体
typedef struct
{
unsigned int moder;
unsigned int otyper;
unsigned int ospeedr;
unsigned int pupdr;
unsigned int idr;
unsigned int odr;
}gpio_t;
#define GPIOE (*(gpio_t*)0x50006000)
#define GPIOF (*(gpio_t*)0x50007000)
#define RCC (*((unsigned int *)0x50000A28))
void delay(int ms);
void all_led_init();
#endif
led.c
#include"led.h"
//延时函数
void delay(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++);
}
}
//GPIO初始化
void all_led_init()
{
//RCC使能
RCC |= (0x3<<20);
//设置PE10 PF10 PE8为输出
GPIOE.moder &= (~(0x3<<20));
GPIOE.moder |= (0x1<<20);
GPIOF.moder &= (~(0x3<<20));
GPIOF.moder |= (0x1<<20);
GPIOE.moder &= (~(0x3<<16));
GPIOE.moder |= (0x1<<16);
//设置推挽输出
GPIOE.otyper &= (~(0x1<<10));
GPIOF.otyper &= (~(0x1<<10));
GPIOE.otyper &= (~(0x1<<8));
//设置三个管脚低速输出
GPIOE.ospeedr &= (~(0x3<<20));
GPIOF.ospeedr &= (~(0x3<<20));
GPIOE.ospeedr &= (~(0x3<<16));
//设置三个管脚输出时无上拉电阻和下拉电阻
GPIOE.pupdr &= (~(0x3<<20));
GPIOE.pupdr &= (~(0x3<<20));
GPIOE.pupdr &= (~(0x3<<20));
}
main.c
#include "led.h"
int main()
{
//完成GPIO的相关初始化
all_led_init();
while(1)
{
//LED1亮
GPIOE.odr |= (0x1<<10);
GPIOF.odr &= (~(0x1<<10));
GPIOE.odr &= (~(0x1<<8));
delay(1000);
GPIOE.odr &= (~(0x1<<10));
GPIOF.odr |= ((0x1<<10));
GPIOE.odr &= (~(0x1<<8));
delay(1000);
GPIOE.odr &= (~(0x1<<10));
GPIOF.odr &= (~(0x1<<10));
GPIOE.odr |= (0x1<<8);
delay(1000);
}
return 0;
}