STM32下LED闪烁实现详解

目录

一、操作STM32需要三个步骤:

二、LED闪烁

接线图

操作过程

代码

三、流水灯

接线图

讲解

代码

四、蜂鸣器

接线图

代码 

 

一、操作STM32需要三个步骤:

  1. 使用RCC开启GPIOI的时钟
  2. 使用GPIO_INIT函数初始化GPIO端口
  3. 使用输入或者输出函数控制GPIO口

.h文件夹最下面有很多库函数的声明,以下列举三组常用函数:

RCC库中的外设时钟控制函数

void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);

void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);

void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);

/**  
 * @brief 控制指定AHB外设的时钟使能状态。  
 * @param RCC_AHBPeriph 要控制的AHB外设。  
 * @param NewState 新的时钟状态(使能或禁用)。  
 */  
void RCC_AHBPeriphClockCmd(uint32_t RCC_AHBPeriph, FunctionalState NewState);  

/**  
 * @brief 控制指定APB2外设的时钟使能状态。  
 * @param RCC_APB2Periph 要控制的APB2外设。  
 * @param NewState 新的时钟状态(使能或禁用)。  
 */  
void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState);  

/**  
 * @brief 控制指定APB1外设的时钟使能状态。  
 * @param RCC_APB1Periph 要控制的APB1外设。  
 * @param NewState 新的时钟状态(使能或禁用)。  
 */  
void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState);  

第一个参数选择外设,第二个函数选择使能或失能。

GPIO库文件

八个读写函数

GPIO_DeInit(void);

void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct);

先定义一个结构体变量,然后赋值,最后定义这个结构体函数。

初始化外设函数基本使用Init函数来完成。

uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);

uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);

void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);

void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);

/**  
 * @brief 读取指定GPIO引脚的输入状态(位)。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @param GPIO_Pin 要读取的GPIO引脚。  
 * @return 返回引脚的输入状态(0或1)。  
 */  
uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);  

/**  
 * @brief 读取指定GPIO端口的所有输入数据(字)。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @return 返回端口的输入状态(16位)。  
 */  
uint16_t GPIO_ReadInputData(GPIO_TypeDef* GPIOx);  

/**  
 * @brief 读取指定GPIO引脚的输出状态(位)。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @param GPIO_Pin 要读取的GPIO引脚。  
 * @return 返回引脚的输出状态(0或1)。  
 */  
uint8_t GPIO_ReadOutputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);  

/**  
 * @brief 读取指定GPIO端口的所有输出数据(字)。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @return 返回端口的输出状态(16位)。  
 */  
uint16_t GPIO_ReadOutputData(GPIO_TypeDef* GPIOx);  

/**  
 * @brief 设置指定GPIO引脚为高电平。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @param GPIO_Pin 要设置的GPIO引脚。  
 */  
void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);  

/**  
 * @brief 设置指定GPIO引脚为低电平。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @param GPIO_Pin 要重置的GPIO引脚。  
 */  
void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);  

/**  
 * @brief 设置指定GPIO引脚的状态(高/低)。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @param GPIO_Pin 要设置的GPIO引脚。  
 * @param BitVal 引脚的目标状态(高或低)。  
 */  
void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);  

/**  
 * @brief 写入指定GPIO端口的输出数据。  
 * @param GPIOx 指向目标GPIO端口的指针。  
 * @param PortVal 要写入的16位值。  
 */  
void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);  

可以实现读写的功能。

八种输出模式

IN模拟输入

IN_FLOOATING浮空输入

IPD下拉输入

IPU上拉输入

Out_OD开漏输出

Out_PP推挽输出(开灯使用

AF_OD复用开漏

AF_PP复用推挽

 

二、LED闪烁

接线图

操作过程

本次课程,我们尝试点亮LED连接的是PA0口的,所以我们选择,GPIOA一项。

先初始化时钟,打开rcc.h在文档的末尾我们可以看到函数们的声明,找到我们需要使用的,跳转到定义,然后我们就回去rcc.c文件中函数的定义,在注释中,我们可以看到使用用途以及所需参数,根据需要cv就可以。

接下来是初始化GPIO口,打开GPIO.h我们根据需要跳转到函数定义处,也就是跳转到Iniit函数,第一个参数GPIIOA,第二个参数是个结构体,我们在Init上方定义一个结构体,然后复制结构体名字,用.把结构成员都引出来,对其进行初始化。

初始化的选择同样跳转到定义,然后在注释中分别选择如下几个选项

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

其中Pin有多个定义,我们点击跳转后选择左下角member选项,就可以跳转到对应宏定义或者注释的位置了。

void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);将接口设置为高电平

void GPIO_ResetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin);将接口设置为低电平

void GPIO_WriteBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, BitAction BitVal);前两个参数指定端口,后面一个指定数值

void GPIO_Write(GPIO_TypeDef* GPIOx, uint16_t PortVal);选择端口,同时对16个端口实施写入操作

如果想要01来表示高低电平的话记得使用枚举类型强制类型转换。

长脚PA0,短脚负极,高电平点亮,说明高低电平都有输出能力,如果开漏输出,则高电平没有输出能力,相当于高阻态。

代码

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//初始化时钟
    GPIO_InitTypeDef GPIO_InitStructure;//定义一个结构体填充参数2
    //初始化结构体成员
    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);//初始化GPIO
	
	
    while (1)
    {
        GPIO_WriteBit(GPIOA, GPIO_Pin_0,Bit_SET);
        Delay_ms(500);
        GPIO_WriteBit(GPIOA, GPIO_Pin_0,Bit_RESET);
        Delay_ms(500);
//  方法二
//	GPIO_SetBits(GPIOA,GPIO_LED1);
//	GPIO_ResetBits(GPIOA,GPIO_LED1);            
    }
}

 

三、流水灯

接线图

讲解

以上每个端口对应一个位,

0000 0000 0000 0001

0000 0000 0000 0010

0000 0000 0000 0100

而0111相当于同时选中三个端口

也可以使用按位或同时选择多个引脚

代码

#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//初始化时钟
	
	GPIO_InitTypeDef GPIO_InitStructure;//定义一个结构体填充参数2
	//初始化结构体成员
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;//直接全部引脚初始化为推挽输出模式GPIO_Pin_01 | GPIO_Pin_02 | GPIO_Pin_03
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
	
	
	while (1)
	{
		GPIO_Write(GPIOA, ~0x0001);//C语言不支持二进制这里用十六进制表示
		//加入按位取反符号,只有第一个点亮,其他的熄灭
		Delay_ms(500);
		
		GPIO_Write(GPIOA, ~0x0002);
		Delay_ms(500);
		
		GPIO_Write(GPIOA, ~0x0004);
		Delay_ms(500);

		GPIO_Write(GPIOA, ~0x0008);
		Delay_ms(500);

		GPIO_Write(GPIOA, ~0x0010);
		Delay_ms(500);

		GPIO_Write(GPIOA, ~0x0020);
		Delay_ms(500);

		GPIO_Write(GPIOA, ~0x0040);
		Delay_ms(500);
		
		GPIO_Write(GPIOA, ~0x0080);
		Delay_ms(500);

	}
}

 

四、蜂鸣器

接线图

代码 

//2023/09/14 20:54
#include "stm32f10x.h"                  // Device header
#include "Delay.h"

int main(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//初始化时钟
	
	GPIO_InitTypeDef GPIO_InitStructure;//定义一个结构体填充参数2
	//初始化结构体成员
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIO
	
	
	while (1)
	{
		GPIO_WriteBit(GPIOB, GPIO_Pin_12,Bit_SET);
		Delay_ms(500);
		GPIO_WriteBit(GPIOB, GPIO_Pin_12,Bit_RESET);
		Delay_ms(500);


	}
}

作者:寒雒

物联沃分享整理
物联沃-IOTWORD物联网 » STM32下LED闪烁实现详解

发表回复