头文件
#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();
void delay_ms(int ms);
#endif
二级目录
#include "gpio.h"
void led1_init()
{ //通过RCC_AHB4_ENSETR:0x50000A28寄存器设置GPIOE控制器使能 0x50000A28[4] = 1
RCC_AHB4_ENSETR |= 0x1<<4;
//通过GPIOE_MODER:0x50006000寄存器设置PE10引脚为输出模式 0x50006000[21:20]= 01
GPIOE->MODER |= 0x1<<20;
GPIOE->MODER &= (~(0x1<<21));
//通过GPIOE_OTYPER:0x50006004寄存器设置PE10引脚为输出模式 0x50006000[21:20]= 01
GPIOE->OTYPER &= ~(0x1<<10);
//通过GPIOE_OSPEEDR:0x50006008寄存器设置PE10引脚为低速输出模式0x50006008[21:20]= 00
GPIOE->OSPEEDR &= ~(0x3<<20);
//通过GPIOE_PUPDR:0x5000600C寄存器设置PE10引脚为禁止上下拉0x5000600C[21:20]= 00
GPIOE->PUPDR &= ~(0x3<<20);
}
void led2_init()
{ //通过RCC_AHB4_ENSETR:0x50000A28寄存器设置GPIOE控制器使能 0x50000A28[5] = 1
RCC_AHB4_ENSETR |= 0x1<<5;
//通过GPIOE_MODER:0x50007000寄存器设置PE10引脚为输出模式 0x50006000[21:20]= 01
GPIOF->MODER |= 0x1<<20;
GPIOF->MODER &= (~(0x1<<21));
//通过GPIOE_OTYPER:0x50007004寄存器设置PE10引脚为输出模式 0x50006000[21:20]= 01
GPIOF->OTYPER &= ~(0x1<<10);
//通过GPIOE_OSPEEDR:0x50007008寄存器设置PE10引脚为低速输出模式0x50006008[21:20]= 00
GPIOF->OSPEEDR &= ~(0x3<<20);
//通过GPIOE_PUPDR:0x5000700C寄存器设置PE10引脚为禁止上下拉0x5000600C[21:20]= 00
GPIOF->PUPDR &= ~(0x3<<20);
}
void led3_init()
{ //通过RCC_AHB4_ENSETR:0x50000A28寄存器设置GPIOE控制器使能 0x50000A28[4] = 1
RCC_AHB4_ENSETR |= 0x1<<4;
//通过GPIOE_MODER:0x50006000寄存器设置PE10引脚为输出模式 0x50006000[17:16]= 01
GPIOE->MODER |= 0x1<<16;
GPIOE->MODER &= (~(0x1<<17));
//通过GPIOE_OTYPER:0x50006004寄存器设置PE10引脚为输出模式 0x50006000[17:16]= 01
GPIOE->OTYPER &= ~(0x1<<8);
//通过GPIOE_OSPEEDR:0x50006008寄存器设置PE10引脚为低速输出模式0x50006008[17:16]= 00
GPIOE->OSPEEDR &= ~(0x3<<16);
//通过GPIOE_PUPDR:0x5000600C寄存器设置PE10引脚为禁止上下拉0x5000600C[17:16]= 00
GPIOE->PUPDR &= ~(0x3<<16);
}
//通过GPIOE_ODR:0x50006014寄存器设置PE10引脚为输出高电平 0x50006014[10]= 1
void led1_on()
{
GPIOE->ODR |= 0x1<<10;
}
//通过GPIOE_ODR:0x50006014寄存器设置PE10引脚为输出低电平 0x50006014[10]= 0
void led1_off()
{
GPIOE->ODR &= ~(0x1<<10);
}
//通过GPIOF_ODR:0x50007014寄存器设置PF10引脚为输出高电平 0x50007014[10]= 1
void led2_on()
{
GPIOF->ODR |= 0x1<<10;
}
//通过GPIOE_ODR:0x50007014寄存器设置PE10引脚为输出低电平 0x50007014[10]= 0
void led2_off()
{
GPIOF->ODR &= ~(0x1<<10);
}
//通过GPIOE_ODR:0x50006014寄存器设置PE8引脚为输出高电平 0x50006014[8]= 1
void led3_on()
{
GPIOE->ODR |= 0x1<<8;
}
//通过GPIOE_ODR:0x50006014寄存器设置PE8引脚为输出低电平 0x50006014[8]= 0
void led3_off()
{
GPIOE->ODR &= ~(0x1<<8);
}
void delay_ms(int ms)
{
int i,j;
for(i = 0; i < ms;i++)
for (j = 0; j < 1800; j++);
}
主函数
#include"gpio.h"
int main(int argc, const char *argv[])
{
led1_init();
led2_init();
led3_init();
while(1)
{
led1_on();
delay_ms(650);
led1_off();
delay_ms(650);
led2_on();
delay_ms(650);
led2_off();
delay_ms(650);
led3_on();
delay_ms(650);
led3_off();
delay_ms(650);
}
return 0;
}
运行现象