STM32学习笔记1——最简单的GPIO

前面的学习参照的是《STM32新手入门教程》


一、配置GPIO

LED局部原理图如下图所示:
LED局部原理图
现在以配置LED0(PA8)这个引脚为例进行说明。

1.定义初始化结构体变量

GPIO_InitTypeDef GPIO_InitStrcture;

转到结构体的定义:

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;

2.使能IO口时钟

//使能PA的外设时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

转到函数定义去学习,这个函数的第一个参数用于指定打开那一组IO口的时钟,第二个参数设置打开或者关闭使能。

3.配置IO口引脚

//配置PA口的引脚
GPIO_InitStrcture.GPIO_Pin=GPIO_Pin_8;	

4.配置IO口输入输出模式

//配置为输出推挽模式
GPIO_InitStrcture.GPIO_Mode=GPIO_Mode_Out_PP;	

5.配置IO口输出速度

//可选择10Mhz、20Mhz、50Mhz
GPIO_InitStrcture.GPIO_Speed=GPIO_Speed_50MHz;

6.初始化相应寄存器

//初始化A口相应寄存器
GPIO_Init(GPIOA,&GPIO_InitStrcture);

LED0和LED1两个引脚的配置

led.h

#ifndef __LED_H
#define __LED_H

#include "stm32f10x.h"

#define LED0_OFF  GPIO_SetBits(GPIOA,GPIO_Pin_8)
#define LED0_ON   GPIO_ResetBits(GPIOA,GPIO_Pin_8)
#define LED0_REV  GPIO_WriteBit(GPIOA,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_8)))

#define LED1_OFF  GPIO_SetBits(GPIOB,GPIO_Pin_11)
#define LED1_ON   GPIO_ResetBits(GPIOB,GPIO_Pin_11)
#define LED1_REV  GPIO_WriteBit(GPIOB,GPIO_Pin_11,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOB,GPIO_Pin_11)))


void LED_GPIO_Config(void);

#endif /*__LED_H */

led.c

#include "led.h"
#include "stm32f10x.h"

void LED_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStrcture;
	
	//set PA8
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//clk
	
	GPIO_InitStrcture.GPIO_Pin=GPIO_Pin_8;							//Pin
	GPIO_InitStrcture.GPIO_Speed=GPIO_Speed_50MHz;			//speed
	GPIO_InitStrcture.GPIO_Mode=GPIO_Mode_Out_PP;				//mode
	
	GPIO_Init(GPIOA,&GPIO_InitStrcture);								//init
	
	//set PB11
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//clk
	GPIO_InitStrcture.GPIO_Pin=GPIO_Pin_11;							//Pin
	GPIO_InitStrcture.GPIO_Speed=GPIO_Speed_50MHz;			//speed
	GPIO_InitStrcture.GPIO_Mode=GPIO_Mode_Out_PP;				//mode
	
	GPIO_Init(GPIOB,&GPIO_InitStrcture);								//init
	
}

二、按键输入控制LED的点亮

KEY-LED原理图

1.按键输入的配置

Key.h

#ifndef __KEY_H
#define __KEY_H


#define KEY1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)
#define KEY2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13)
#define KEY3 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)

void Key_GPIO_Config(void);

unsigned char KEY_Scan(void);
unsigned char KEY1_Scan(void);
unsigned char KEY2_Scan(void);
unsigned char KEY3_Scan(void);

#endif /* __KEY_H */

Key.c

#include "stm32f10x.h"
#include "Key.h"
#include "delay.h"

void Key_GPIO_Config(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//PC
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_13;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	
	GPIO_Init(GPIOC,&GPIO_InitStructure);
	
	//PA
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;
	
	GPIO_Init(GPIOA,&GPIO_InitStructure);
}


unsigned char KEY_Scan(void)
{
	unsigned char key_code;
	if(KEY1_Scan()==1) key_code=1;
	else if(KEY2_Scan()==1) key_code=2;
	else if(KEY3_Scan()==1) key_code=3;
	else key_code=0;
	return key_code;
}

unsigned char KEY1_Scan(void)
{
	static char key_up1=0;
	if(KEY1==0)
	{
		Delay(10000);
		if(KEY1==0)
		{
			key_up1=1;
		}
	}
	if(KEY1==1&&key_up1==1)
	{
		key_up1=0;
		return 1;
	}
	return 0;
}

unsigned char KEY2_Scan(void)
{
	static char key_up2=0;
	
	if(KEY2==0)
	{
		Delay(10000);
		if(KEY2==0)
		{
			key_up2=1;
		}
	}
	if(KEY2==1&&key_up2==1)
	{
		key_up2=0;
		return 1;
	}
	return 0;
}


unsigned char KEY3_Scan(void)
{
	static char key_up3=0;
	
	if(KEY3==0)
	{
		Delay(10000);
		if(KEY3==0)
		{
			key_up3=1;
		}
	}
	if(KEY3==1&&key_up3==1)
	{
		key_up3=0;
		return 1;
	}
	return 0;
}

2.实现功能

当WK UP按下,LED0、LED1同时取反;
当KEY0按下,LED0取反;
当KEY1按下,LED1取反。

主函数:

#include "stm32f10x.h"
#include "led.h"
#include "Key.h"



int main(void)
{
	unsigned char value=0;
	
	LED_GPIO_Config();
	
	Key_GPIO_Config();
	
	while(1)
	{
		value =KEY_Scan();
		
		if(value==1)
		{
			LED0_REV;
		}
		else if(value==2)
		{
			LED1_REV;
		}
		else if(value==3)
		{
			LED0_REV;
			LED1_REV;
		}
	}
}

学习心得

主要是通过训练熟悉STM32GPIO最简单的配置。看得懂代码在干什么,并且根据自己学习的板子去修改代码是近期最大的进步,继续努力。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于STM32GPIO输出实验,点亮三色LED,您可以按照以下步骤进行操作: 1. 首先,您需要在STM32的开发环境中创建一个新的工程。 2. 在工程中,您需要包含相应的头文件,如"stm32f10x.h"。 3. 接下来,配置相应的引脚为输出模式。假设红色LED连接到GPIOA的Pin0引脚,绿色LED连接到GPIOA的Pin1引脚,蓝色LED连接到GPIOA的Pin2引脚,您可以使用以下代码进行配置: ``` GPIO_InitTypeDef GPIO_InitStructure; // 使能GPIOA的时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 配置红色LED引脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // 推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置绿色LED引脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); // 配置蓝色LED引脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); ``` 4. 现在,您可以使用相应的寄存器来控制LED的点亮和熄灭。例如,要点亮红色LED,可以使用以下代码: ``` GPIO_SetBits(GPIOA, GPIO_Pin_0); // 将Pin0引脚置高,点亮红色LED ``` 要熄灭红色LED,可以使用以下代码: ``` GPIO_ResetBits(GPIOA, GPIO_Pin_0); // 将Pin0引脚置低,熄灭红色LED ``` 同样的方法,您可以控制绿色和蓝色LED的点亮和熄灭。 这就是点亮三色LED的基本步骤。希望对您有所帮助!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值