实验现象
实现代码
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;
#define GPIOE ((gpio*)0x50006000)
#define GPIOF ((gpio*)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<<10));
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<<8));
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();
// LED灯初始化
while(1)
{
LED1_ON();
LED2_ON();
LED3_ON();
delay_ms(500);
LED1_OFF();
LED2_OFF();
LED3_OFF();
delay_ms(500);
}
r
eturn 0;
}