main代码如下:
#include "./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()
{
led1_init();
led3_init();
led2_init();// LED灯初始化
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;
}
led.h代码如下:
#ifndef __LED_H__
#define __LED_H__
#define GPIOE (*(volatile unsigned int*)0x50000A28)
#define GPIOE_MODER (*(volatile unsigned int*)0x50006000)
#define GPIOE_OTYPER (*(volatile unsigned int*)0x50006004)
#define GPIOE_OSPEEDR (*(volatile unsigned int*)0x50006008)
#define GPIOE_PUPDR (*(volatile unsigned int*)0x5000600C)
#define GPIOE_ODR (*(volatile unsigned int*)0x50006014)
#define GPIOF (*(volatile unsigned int*)0x50000A28)
#define GPIOF_MODER (*(volatile unsigned int*)0x50007000)
#define GPIOF_OTYPER (*(volatile unsigned int*)0x50007004)
#define GPIOF_OSPEEDR (*(volatile unsigned int*)0x50007008)
#define GPIOF_PUPDR (*(volatile unsigned int*)0x5000700C)
#define GPIOF_ODR (*(volatile unsigned int*)0x50007014)
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
led.c代码如下:
#include "../include/led.h"
void led1_init()
{
GPIOE |=(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()
{
GPIOF |=(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()
{
GPIOE |=(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);
}
结果如果所示: