用C语言实现LED1/LED2/LED3灯点亮
实验现象
实验代码
gpio.h
#ifndef __GPIO_H__
#define __GPIO_H__
#define RCC_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
typedef struct
{
volatile unsigned int MODE; //00
volatile unsigned int OTYPER; //04
volatile unsigned int OSPEEDR; //08
volatile unsigned int PURDR; //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.RCC使能
RCC_AHB4_ENSETR |= (0x1 << 4);
//1.PE10引脚设为输出模式
GPIOE->MODE &= (~(0x3<<20));
GPIOE->MODE |= (0x1<<20);
//2.设置PE10引脚为推挽输出模式
GPIOE->OTYPER &= (~(0x1<<10));
//3.设置PE10引脚为低速输出模式
GPIOE->OSPEEDR &= (~(0x3)<<20);
//4.设置PE10引脚为禁止上下拉
GPIOE->PURDR &= (~(0x3 << 20));
}
void LED1_on()
{
GPIOE->ODR |= (0x1<<10);
}
void LED1_off()
{
GPIOE->ODR &= (~(0x1<<10));
}
void LED2_init()
{
RCC_AHB4_ENSETR |= (0x1 << 5);
//1.PF10引脚设为输出模式
GPIOF->MODE &= (~(0x3<<20));
GPIOF->MODE |= (0x1<<20);
//2.设置PF10引脚为推挽输出模式
GPIOF->OTYPER &= (~(0x1<<10));
//3.设置PF10引脚为低速输出模式
GPIOF->OSPEEDR &= (~(0x3)<<20);
//4.设置PF10引脚为禁止上下拉
GPIOF->PURDR &= (~(0x3 << 20));
}
void LED2_on()
{
GPIOF->ODR |= (0x1<<10);
}
void LED2_off()
{
GPIOF->ODR &= (~(0x1<<10));
}
void LED3_init()
{
//0.RCC使能
RCC_AHB4_ENSETR |= (0x1 << 4);
//1.PE10引脚设为输出模式
GPIOE->MODE &= (~(0x3<<16));
GPIOE->MODE |= (0x1<<16);
//2.设置PE10引脚为推挽输出模式
GPIOE->OTYPER &= (~(0x1<<8));
//3.设置PE10引脚为低速输出模式
GPIOE->OSPEEDR &= (~(0x3)<<16);
//4.设置PE10引脚为禁止上下拉
GPIOE->PURDR &= (~(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();
delay_ms(500);
LED1_off();
delay_ms(500);
LED2_on();
delay_ms(500);
LED2_off();
delay_ms(500);
LED3_on();
delay_ms(500);
LED3_off();
delay_ms(500);
}
return 0;
}