这里以一个跑马灯程序为例,从声明LED_Init()到定义,到调用。对IO口电平的输出使用库函数完成。
-
先写一个LED.h,内容如下:完成LED_Init()的声明
#ifndef __LED_H #define __LED_H void LED_Init(void);//声明函数 #endif
这里为防止一个头文件被多次包含,加入条件编译#ifndef __LED_H。
按照C语言的编程习惯,LED_Init()函数的声明需要放在LED.h中。实现函数声明和定义的分离。
- 再写一个LED.c,内容如下:完成LED_Init()的定义
#include "LED.h"
#include "stm32f10x.h"
void LED_Init(void)//定义函数
{
GPIO_InitTypeDef GPIO_InitSteucture;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);//GPIOB Clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);//GPIOE Clock
GPIO_InitSteucture.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitSteucture.GPIO_Pin=GPIO_Pin_5;
GPIO_InitSteucture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitSteucture);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_Init(GPIOE,&GPIO_InitSteucture);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
3,在main函数中,调用LED_Init()
#include "stm32f10x.h"
#include "LED.h"
#include "delay.h"
int main(void)
{
LED_Init();//调用函数
delay_init();
while(1)
{
//GPIO_SetBits(GPIOB,GPIO_Pin_5);//high
GPIO_SetBits(GPIOE,GPIO_Pin_5);
delay_ms(500);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);//low
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
delay_ms(500);
}
}