循环点亮三个led灯
方法1
------------------led.h----------------
#ifndef __LED_H__
#define __LED_H__
#define RCC (*(volatile unsigned int *)0x50000A28)
#define GPIOE ((GPIO_t *)0x50006000)
#define GPIOF ((GPIO_t *)0x50007000)
//结构体封装
typedef struct
{
volatile unsigned int MODER;
volatile unsigned int OTYPER;
volatile unsigned int OSPEED;
volatile unsigned int PUPDR;
volatile unsigned int IDR;
volatile unsigned int ODR;
}GPIO_t;
//RCC章节初始化
void RCC_INIT();
//LED1 ----> PE10
//LED1 - GPIO章节初始化
void LED1_GPIO_INIT();
//LED1点亮
void LED1_ON();
//LED1熄灭
void LED1_OFF();
//LED2 ----> PF10
//LED2 - GPIO章节初始化
void LED2_GPIO_INIT();
//LED2点亮
void LED2_ON();
//LED2熄灭
void LED2_OFF();
//LED3 ----> PF10
//LED3 - GPIO章节初始化
void LED3_GPIO_INIT();
//LED3点亮
void LED3_ON();
//LED3熄灭
void LED3_OFF();
#endif
------------------led.c----------------
#include "led.h"
//LED1 ---> PE10
//RCC章节初始化
void RCC_INIT()
{
RCC = RCC | (0x3 << 4);
}
//LED1 - GPIO章节初始化
void LED1_GPIO_INIT()
{
//设置为输出模式 0x50006000[21,20] -> 01
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |= (0x1 << 20);
//设置为推挽输出 0x50006004[10] -> 0
GPIOE->OTYPER &= (~(0x1 << 10));
//设置为低速模式 0x50006008[21,20] -> 00
GPIOE->OSPEED &= (~(0x3 << 20));
//设置为禁止上下拉电阻 0x5000600c[21,20] -> 00
GPIOE->PUPDR &= (~(0x3 << 20));
}
//LED1点亮
void LED1_ON()
{
//点亮LED1 0x50006014[10] -> 1
GPIOE->ODR |= (0x1 << 10);
}
//LED1熄灭
void LED1_OFF()
{
//熄灭LED1 0x50006014[10] -> 0
GPIOE->ODR &= (~(0x1 << 10));
}
//LED2 ----> PF10
//LED2 - GPIO章节初始化
void LED2_GPIO_INIT()
{
//设置为输出模式 0x50007000[21,20] -> 01
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
//设置为推挽输出 0x50007004[10] -> 0
GPIOF->OTYPER &= (~(0x1 << 10));
//设置为低速模式 0x50007008[21,20] -> 00
GPIOF->OSPEED &= (~(0x3 << 20));
//设置为禁止上下拉电阻 0x5000700c[21,20] -> 00
GPIOF->PUPDR &= (~(0x3 << 20));
}
//LED2点亮
void LED2_ON()
{
//点亮LED2 0x50007014[10] -> 1
GPIOF->ODR |= (0x1 << 10);
}
//LED2熄灭
void LED2_OFF()
{
//熄灭LED2 0x50007014[10] -> 0
GPIOF->ODR &= (~(0x1 << 10));
}
//LED3 ----> PF10
//LED3 - GPIO章节初始化
void LED3_GPIO_INIT()
{
//设置为输出模式 0x50006000[17,16] -> 01
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 16);
//设置为推挽输出 0x50006004[8] -> 0
GPIOE->OTYPER &= (~(0x1 << 8));
//设置为低速模式 0x50006008[17,16] -> 00
GPIOE->OSPEED &= (~(0x3 << 16));
//设置为禁止上下拉电阻 0x5000600c[17,16] -> 00
GPIOE->PUPDR &= (~(0x3 << 16));
}
//LED3点亮
void LED3_ON()
{
//点亮LED3 0x50006014[8] -> 1
GPIOE->ODR |= (0x1 << 8);
}
//LED3熄灭
void LED3_OFF()
{
//熄灭LED3 0x50006014[8] -> 0
GPIOE->ODR &= (~(0x1 << 8));
}
-----------------main.c----------------
#include "led.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()
{
// LED灯初始化
RCC_INIT();
LED1_GPIO_INIT();
LED2_GPIO_INIT();
LED3_GPIO_INIT();
while(1)
{
//点亮LED1
LED1_ON();
delay_ms(500);
//熄灭LED1
LED1_OFF();
//点亮LED2
LED2_ON();
delay_ms(500);
//熄灭LED2
LED2_OFF();
//点亮LED3
LED3_ON();
delay_ms(500);
//熄灭LED3
LED3_OFF();
}
return 0;
}
方法2