main.c
#include "led.h"
int main()
{
//使能GPIOE,F时钟信号
*((unsigned int*)0x50000A28) |= (0x3<<4);
led1_init();
led2_init();
led3_init();
while(1)
{
led1_ctl(1);
led2_ctl(1);
led3_ctl(1);
delay_ms(100);
led3_ctl(0);
delay_ms(100);
led2_ctl(0);
delay_ms(100);
led1_ctl(0);
delay_ms(100);
led1_ctl(1);
led2_ctl(0);
led3_ctl(0);
delay_ms(500);
led2_ctl(1);
led3_ctl(0);
delay_ms(500);
led1_ctl(0);
led2_ctl(0);
led3_ctl(1);
delay_ms(500);
led1_ctl(1);
led2_ctl(0);
led3_ctl(0);
delay_ms(500);
led1_ctl(0);
led2_ctl(1);
led3_ctl(0);
delay_ms(500);
led1_ctl(0);
led2_ctl(0);
led3_ctl(1);
delay_ms(500);
led1_ctl(1);
led2_ctl(1);
led3_ctl(1);
delay_ms(100);
led1_ctl(0);
led2_ctl(0);
led3_ctl(0);
break;
}
return 0;
}
led.c
#include "led.h"
void led1_init()
{
// 设置PE10输出模式
GPIOE->MODER &= (~(0x3 << 20));
GPIOE->MODER |= (0x1 << 20);
// 设置PE10为推挽输出
GPIOE->OTYPER &= (~(0x1 << 10));
// 设置PE10低速输出
GPIOE->OSPEEDR &= (~(0x3 << 20));
// 设置PE10无上拉电阻
GPIOE->PUPDR &= (~(0x3 << 20));
}
void led2_init()
{
// 设置PF10输出模式
GPIOF->MODER &= (~(0x3 << 20));
GPIOF->MODER |= (0x1 << 20);
// 设置PF10为推挽输出
GPIOF->OTYPER &= (~(0x1 << 10));
// 设置pF10低速输出
GPIOF->OSPEEDR &= (~(0x3 << 20));
// 设置PF10无上拉电阻
GPIOF->PUPDR &= (~(0x3 << 20));
}
void led3_init()
{
// 设置PE8输出模式
GPIOE->MODER &= (~(0x3 << 16));
GPIOE->MODER |= (0x1 << 16);
// 设置PE8为推挽输出
GPIOE->OTYPER &= (~(0x1 << 8));
// 设置PE8低速输出
GPIOE->OSPEEDR &= (~(0x3 << 16));
// 设置PE8无上拉电阻
GPIOE->PUPDR &= (~(0x3 << 16));
}
void led1_ctl(int flag)
{
if (flag == 1)
{
GPIOE->ODR |= (0x1 << 10);
}
else if (flag == 0)
{
GPIOE->ODR &= ((~0x1 << 10));
}
}
void led2_ctl(int flag)
{
if (flag == 1)
{
GPIOF->ODR |= (0x1 << 10);
}
else if (flag == 0)
{
GPIOF->ODR &= (~(0x1 << 10));
}
}
void led3_ctl(int flag)
{
if (flag == 1)
{
GPIOE->ODR |= (0x1 << 8);
}
else if (flag == 0)
{
GPIOE->ODR &= (~(0x1 << 8));
}
}
void delay_ms(int ms)
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<2000;j++)
{
}
}
}
led.h
#ifndef __LED_H__
#define __LED_H__
typedef struct
{
unsigned int MODER;
unsigned int OTYPER;
unsigned int OSPEEDR;
unsigned int PUPDR;
unsigned int IDR;
unsigned int ODR;
unsigned int BSRR;
}gpio_t;
#define GPIOE ((gpio_t *)0X50006000)
#define GPIOF ((gpio_t *)0X50007000)
void led1_init();
void led2_init();
void led3_init();
void led1_ctl(int flag);
void led2_ctl(int flag);
void led3_ctl(int flag);
void delay_ms(int ms);
#endif