程序框架:
1、初始化 系统时钟 (ST公司已做好)
2、使能GPIO时钟,初始化相关参数
3、点亮LED
所需文件:
gpio口外设库文件:stm32f103x_gpio.c 、stm32f10x_gpio.h
RCC外设库文件:stm32f10x.c、stm32f10x_rcc.h
函数介绍:
使能时钟函数:void RCC_APB2periphClockCmd( )
gpio初始化函数:void_Init( )
设置管脚输出电平函数:
void_GPIO_SetBits()、//选择端口,管脚,输出高电平
void_GPIO_ResetBits() //复位,输出低电平
void_GPIO_WriteBits()//写(发送)1还是0
void_GPIO_WriteBits()、//
读取管脚输入电平函数:
uint8_t GPIO_ReadInputDataBit( )//位
uint8_t GPIO_ReadInputDataBit()//组
读取管脚输出电平函数:
uint8_t GPIO_ReadOutputDataBit( )//位
uint8_t GPIO_ReadOutputDataBit()//组
在工程模板建立文件app保存相应功能,比如灯的控制,在建立一个led文件。在keil5中新建led.c 和led.h并添加进led文件。记得添加路径。其中led.c(code)是写函数的,led.h(head)定义函数声明,宏定义等。
编写函数:
led.c
#include "led.h"
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_5);
}
main.c
#include "stm32f10x.h"
#include "led.h"
int main()
{
LED_Init() ;
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
while(1)
{
}
}