led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
//LED端口定义 宏定义
//这里是使用了sys.h里提前定义好的端口,会使用就行
//位带操作--LED0=1输出高,LED0=0输出低
#define LED0 PBout(5)//DS0
#define LED1 PEout(5)//DS1
void LED_Init(void);//初始化
#endif
led.c
#include "led.h"
//初始化PB5和PE5为输出口,并使能这两个口的时钟
//这两个输出在led.h中已经定义好
//LED IO初始化 利用已定义函数实现配置
void LED_Init(void)
{ //GPIO是挂载在APB2总线上的外设
GPIO_InitTypeDef GPIO_InitStructure;//定义端口 结构体模式
//在配置外设时,任何时候都要先使能时钟
//时钟使能函数
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|
RCC_APB2Periph_GPIOE, ENABLE); //使能 PB,PE 端口时钟
//配置输出模式为推挽输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//IO口速度为50MHz
//两个IO口的模式和速度都一样,只用初始化一次
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0 ---->PB.5 推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIOB.5
GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB.5 输出高
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED1 ---->PE.5 推挽输出
GPIO_Init (GPIOE, &GPIO_InitStructure);//初始化GPIOE.5
GPIO_SetBits(GPIOE,GPIO_Pin_5); //PE.5 输出高
}
//推挽输出:两个灯只能亮一个,内部输出0、1,可控制不同的灯亮,用时钟就行了
main.c
#include "led.h"
#include "delay.h"
#include "sys.h"
//跑马灯实验
int main(void)
{
delay_init();//延时函数初始化
LED_Init();//初始化与LED连接的硬件接口
while(1)
{//实现推挽输出
LED0=0;
LED1=1;
delay_ms(300);//延时300ms
LED0=1;
LED1=0;
delay_ms(300);
}
}
//运行时,会先初始化系统时钟再调用main函数
//此处SystemInit()函数在文件system_ stm32f10x.c 中,会自动调用
其他文件均为系统文件