操作stm32的GPIO总共需要4个步骤。
1.使用RCC开启GPIO的时钟。(在stm32中,所有的gpio都是挂载在APB2外设总线上)
2.GPIO结构体定义。
3.使用GPIO_Init函数初始化GPIO。
4.使用使用输出或输入的函数控制GPIO口。
(1)led闪烁
led.h
#ifndef __LED_H
#define __LED_H
void led(void);
#endif
led.c
#include "stm32f10x.h" // Device header
#include "delay.h"
void led(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
while(1){
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
Delay_s(100);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
Delay_s(100);
}
}
main.c
#include "stm32f10x.h" // Device header
#include "Led.h"
#include "delay.h"
int main(void)
{
while (1)
{
led();
}
}
(2)led流水灯
led.c
#include "stm32f10x.h" // Device header
#include "delay.h"
void led(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_All;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
while(1){
for(u8 i=0;i<8;i++){
GPIO_Write(GPIOA,~(0x0001<<i));
Delay_s(100);
}
}
}
(3)按键控制led
main.c
#include "stm32f10x.h" // Device header
#include "Led.h"
#include "delay.h"
#include "Key.h"
uint8_t num=0;
int main(void)
{
Led_Init();
Key_Init();
while (1)
{
num=Key_Getnum();
if(num==1)
{
Led_Turn(GPIOA,GPIO_Pin_0);
}
if(num==2)
{
Led_Turn(GPIOA,GPIO_Pin_1);
}
}
}
led.c
#include "stm32f10x.h" // Device header
#include "delay.h"
void Led_Init(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1);
}
void Led_On(GPIO_TypeDef* GPIOX,uint16_t GPIO_Pin){
GPIO_ResetBits(GPIOX,GPIO_Pin);
}
void Led_Off(GPIO_TypeDef* GPIOX,uint16_t GPIO_Pin){
GPIO_SetBits(GPIOX,GPIO_Pin);
}
void Led_Turn(GPIO_TypeDef* GPIOX,uint16_t GPIO_Pin)
{
if(GPIO_ReadOutputDataBit(GPIOX,GPIO_Pin)==0){
GPIO_SetBits(GPIOX,GPIO_Pin);
}
else{
GPIO_ResetBits(GPIOX,GPIO_Pin);
}
}
key.c
#include "stm32f10x.h" // Device header
#include "delay.h"
void Key_Init(void) //按键初始化
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_Initstructure;
GPIO_Initstructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Initstructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;
GPIO_Initstructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_Initstructure);
}
uint8_t Key_Getnum(void)
{
uint8_t num;
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0) /判断引脚输入电平
{
Delay_s(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0)==0);//判断按键是否松开(消抖)
Delay_s(20);
num=1;
}
if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0)
{
Delay_s(20);
while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)==0);/
Delay_s(20);
num=2;
}
return num;
}
通过引脚高低电平,获取返回值在主函数中根据返回值判断哪个灯亮灭。