库函数的使用方法:首先要建立一个新的工程,将一个项目的所有内容都放到一个文件夹下面,对于外设的使用,统一放到HAREWARE新建的文件夹下面,以跑马灯为例,编写的代码要建立一个头文件,头文件led.h文件代码如下
#ifndef __led_h
#define __led_h
void led_Init(void); //声明函数,头文件一般都是用作声明函数
#endif
接下来建立一个源文件led.c,该文件其实就是将头文件所提到的函数进行描述,同时与外设建立联系,其代码如下
#include "led.h"
#include "stm32f10x.h"
void led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO_InitStructure是自定义的名字
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//GPIOB的使能函数
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//GPIOE的使能函数
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//初始化IO口模式
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//初始化IO口模式
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//初始化IO口模式
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
以上就是源文件的格式。
1.必须要有使能IO口时钟,不同的IO组,调用的时钟使能函数不同。
2.初始化IO口模式,其调用了GPIO_Init()函数,所以在开头加了#include "stm32f10x.h"
之后在USER的工程下面选中main.c文件将主函数代码写入其中
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
int main(void)
{
delay_init();//调用了延时函数
led_Init();
while(1){
GPIO_SetBits(GPIOB,GPIO_Pin_5);//设置使其IO口输出为1,则显示灯亮
GPIO_SetBits(GPIOE,GPIO_Pin_5);
delay_ms(500);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);//设置使其IO口输出为0,则显示灯灭
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
delay_ms(500);
}
}
下载到芯片内即可运行