用C语言实现LED1/LED2/LED3灯点亮
代码
gpio.h
#ifndef __GPIO_H__
#define __GPIO_H__
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)
#define RCC_AHB4_ENSETR (*(volatile unsigned int*)0x50000A28)
void LED1_INIT();
void LED2_INIT();
void LED3_INIT();
void LED1_ON();
void LED2_ON();
void LED3_ON();
void LED1_OFF();
void LED2_OFF();
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 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 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 LED1_ON()
{
GPIOE->ODR |=(0x1<<10);
}
void LED2_ON()
{
GPIOF->ODR |=(0x1<<10);
}
void LED3_ON()
{
GPIOE->ODR |=(0x1<<8);
}
void LED1_OFF()
{
GPIOE->ODR &=(~(0x1<<10));
}
void LED2_OFF()
{
GPIOF->ODR &=(~(0x1<<10));
}
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();
delay_ms(500);
LED1_OFF();
LED2_ON();
delay_ms(500);
LED2_OFF();
LED3_ON();
delay_ms(500);
LED3_OFF();
}
return 0;
}
实验现象