①led.h:
#ifndef led_h
#define led_h
void led_init(void);
#endif
②led.c:
#include"led.h"
#include"stm32f10x.h"
void led_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //结构体;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能GPIOB;
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);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//使能GPIOE;
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);
}
③main:
#include"led.h"
#include"stm32f10x.h"
#include"delay.h"
int main(void)
{
led_init();
delay_init();
while(1)
{
GPIO_ResetBits(GPIOB,GPIO_Pin_5); // PBout(5)=1;
GPIO_SetBits(GPIOE,GPIO_Pin_5); // PEout(5)=1;
delay_ms(500); // delay_ms(500);
//库函数 //位操作
GPIO_SetBits(GPIOB,GPIO_Pin_5); // PBout(5)=0;
GPIO_ResetBits(GPIOE,GPIO_Pin_5); // PEout(5)=0;
delay_ms(500); // delay_ms(500);
}
}
(使用到了stm32f10x.rcc.h、stm32f10x_gpio.h与delay.h 已有头文件。)
补:GPIO的配置种类有8种。分别为模拟输入,浮空输入,上拉输入,下拉输入,开漏输出,推挽输出,复用开漏输出。
2019.8.11