您现在的位置是:首页 >技术杂谈 >江科大STM32-GPIO输出 点亮LED,LED闪烁,LED流水灯,蜂鸣器(学习笔记)网站首页技术杂谈
江科大STM32-GPIO输出 点亮LED,LED闪烁,LED流水灯,蜂鸣器(学习笔记)
一、点亮LED
操作STM32的GPIO需要三个步骤,总共涉及了RCC和GPIO两个外设 ,下图圈出来的函数为最常用的函数
步骤一:使用RCC开启GPIO的时钟
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);
作用:开启或关闭APB2 (High Speed APB)外设时钟开关。
参数 | 说明 |
RCC_APB2Periph | 指定APB2外设对其时钟进行门控,指明需要开启的是哪一个APB2外设(点亮LED用GPIOA)下图可选 |
NewState | 指定外设时钟的新状态。取值为:ENABLE或DISABLE |
步骤二:使用GPIO_Init函数初始化GPIO
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);
作用: 根据指定初始化GPIOx外设GPIO_InitStruct中的参数。(用结构体参数来初始化GPIO口)
使用说明:先定义一个结构体变量,然后给结构体赋值,最后调用这个函数。这个函数内部就会自动读取结构体的值,然后自动把外设的各个参数配置好。
参数 | 说明 |
GPIOx | 其中x可以为(A..G)选择GPIO外设 |
GPIO_InitStruct | 指向GPIO_InitTypeDef结构体的指针 其中*为指定GPIO外设的配置信息。 |
指定要配置的GPIO引脚。
其中 GPIO InitTypeDef结构体配置信息如下:
typedef struct
{
uint16_t GPIO_Pin; /*!< Specifies the GPIO pins to be configured.
This parameter can be any value of @ref GPIO_pins_define */
GPIOSpeed_TypeDef GPIO_Speed; /*!< Specifies the speed for the selected pins.
This parameter can be a value of @ref GPIOSpeed_TypeDef */
GPIOMode_TypeDef GPIO_Mode; /*!< Specifies the operating mode for the selected pins.
This parameter can be a value of @ref GPIOMode_TypeDef */
}GPIO_InitTypeDef;
选中圈圈Ctrl+F就可以跳转到结构体赋值
端口选择 GPIO_Pin
最大频率选择
配置模式枚举
参数解释:
参数 | 解释 |
GPIO_Pin | 指定要配置的GPIO引脚。 |
GPIO_Speed | 指定所选引脚的速度 |
GPIO_Mode | 指定所选引脚的工作模式。 |
步骤三:使用输出或者输入函数控制GPIO口
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
作用:清除所选数据端口位。对某个端口写0,置低电平,点亮LED
参数 | 说明 |
GPIOx | 其中x可以为(A..G)选择GPIO外设。 |
GPIO_Pin | 指定要写入的端口位。该参数可以是GPIO_Pin_x的任意组合,其中x可以是(0..15)。 |
类似的还有:GPIO_SetBits函数,设置所选数据端口位,同样的用法,只不过这个函数是写1,置高电平,灭灯。
接线图:
点亮LED完整代码:
#include "stm32f10x.h" // Device header
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_ResetBits(GPIOA, GPIO_Pin_0);
while(1)
{
}
}
二、LED闪烁
使用RCC开启GPIO的时钟和使用GPIO_Init函数初始化GPIO跟前面的一样
GPIO_WriteBit();函数
点亮LED除了用GPIO_ResetBits();函数,还有GPIO_WriteBit();函数,这里介绍后者
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal)
作用: 设置或清除所选数据端口位。
参数说明:
参数 | 说明 |
GPIOx | 其中x可以为(A..G)选择GPIO外设。 |
GPIO_Pin | 指定要写入的端口位。该参数可以是GPIO_Pin_x中的一个,其中x可以是(0..15)。 |
BitVal | 指定要写入所选位的值。该参数可以是BitAction枚举值之一:Bit_RESET(清除端口引脚,置低电平)或者Bit_SET(设置端口引脚,置高电平) |
完整代码: ①
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
while(1)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_RESET);
Delay_ms(500);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,Bit_SET);
Delay_ms(500);
}
}
法②: 使用前面讲的方法
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
while(1)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0);//置低电平
Delay_ms(500);
GPIO_SetBits(GPIOA, GPIO_Pin_0);//置高电平
Delay_ms(500);
}
}
③:
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
while(1)
{
GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)0);//强制类型转换,把0转换为BitAction的枚举类型
Delay_ms(500);
GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)1);//强制类型转换,把1转换为BitAction的枚举类型
Delay_ms(500);
}
}
三、LED流水灯
因为我们这里连接的都是GPIOA的端口,所以外设时钟不用变。
而初始化端口前只是初始化了P0口,LED闪烁要用到P0~P7口,所以要初始化P0~P7口,可以使用按位或方法进行操作, 例如:
除了这里可以用按位或操作, 前面介绍的RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);和GPIO_SetBits(GPIOA, GPIO_Pin_0);函数的GPIO也可以用按位或方式。
这里为了方便,可以直接使用GPIO_Pin_All把16个端口全部配置成推挽输出模式。
GPIO_Write();函数
主循环while使用到void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal) 函数
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal)
作用: 将数据写入指定的GPIO数据端口。
参数说明:
参数 | 说明 |
GPIOx | 其中x可以为(A..G)选择GPIO外设。 |
PortVal | 指定要写入端口输出数据寄存器的值。(写十六进制) |
接线图:
完整代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_All; //初始化16个端口
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
while(1)
{
//因为C语言不支持直接写二进制,所以要转换为十六进制,且低电平点亮,所以要取反
GPIO_Write(GPIOA,~0x0001);//0000 0000 0000 0001 /第一个LED点亮,其他的熄灭
Delay_ms(100); //对应PA0~PA15,从右至左依次
GPIO_Write(GPIOA,~0x0002);//0000 0000 0000 0010
Delay_ms(100);
GPIO_Write(GPIOA,~0x0004);//0000 0000 0000 0100
Delay_ms(100);
GPIO_Write(GPIOA,~0x0008);//0000 0000 0000 1000
Delay_ms(100);
GPIO_Write(GPIOA,~0x0010);//0000 0000 0001 0001
Delay_ms(100);
GPIO_Write(GPIOA,~0x0020);//0000 0000 0010 0000
Delay_ms(100);
GPIO_Write(GPIOA,~0x0040);//0000 0000 0100 0000
Delay_ms(100);
GPIO_Write(GPIOA,~0x0080);//0000 0000 1000 0000
Delay_ms(100);
}
}
四、蜂鸣器
给PB12置低电平,蜂鸣器响,置高电平,蜂鸣器不响。
时钟:用PB口,所以用GPIOB
输出模式 :还是推挽输出,频率还是GPIO_Speed_50MHz
端口:PB12 -->GPIO_Pin_12
初始化:GPIO_Init(GPIOB,&GPIO_InitStruct);
接线图:
完整代码:
#include "stm32f10x.h" // Device header
#include "Delay.h"
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能(开启)或失能(关闭)APB2外设时钟
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
while(1)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12); //设低电平0,响
Delay_ms(100);
GPIO_SetBits(GPIOB, GPIO_Pin_12); //设高电平1,不响
Delay_ms(100);
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
Delay_ms(100);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
Delay_ms(700);
}
}
感谢观看,有错望兄弟们指出。