3 GPIO通用输入输出口(二)

该文介绍了按键和传感器模块的工作原理,包括按键的抖动问题和解决方法,以及传感器元件如何转换模拟信号为数字电压输出。通过STM32F10x微控制器,展示了如何初始化GPIO端口,实现按键控制LED及光敏传感器控制蜂鸣器的应用示例。
摘要由CSDN通过智能技术生成

3.3 GPIO输入**

按键简介

按键:常见的输入设备,按下导通,松手断开
按键抖动:由于按键内部使用的是机械式弹簧片来进行通断的,所以在按下和松手的瞬间会伴随有一连串的抖动

[ng)]

传感器模块简介

传感器模块:传感器元件(光敏电阻/热敏电阻/红外接收管等)的电阻会随外界模拟量的变化而变化,通过与定值电阻分压即可得到模拟电压输出,再通过电压比较器进行二值化即可得到数字电压输出

[)]

[)]

传感器元件的电阻会随着外界模拟量的变化而变化,通过与定值电阻分压即可,得到模拟电压输出,再通过与电压比较器进行二值化即可得到数字电压输出

按键电路

左边电路要求引脚时上拉或下拉输入的模式,右边电路允许引脚浮空输入模式(因为外置了上拉电阻和下拉电阻)

[)]

下接方式:
按下时低电平,松手时高电平(一般采用这种电路)

[)]

上接方式:
按下时高电平,松手时低电平

[]

3.4 按键控制LED&光敏传感器控制蜂鸣器

1.按键控制

[]

主函数:

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

uint8_t KeyNum; //定义全局变量,用以存一下键盘的返回值

int main(void)
{
	LED_Init ();
	Key_Init();
	
	while (1)
	{
//		LED1_ON();
//		LED2_OFF();
//		Delay_ms(500);
//		LED1_OFF();
//		LED2_ON();
//		Delay_ms(500);
		KeyNum = Key_GetNum();
//		if (KeyNum == 1)
//		{
//			LED1_ON();
//		}
//		if (KeyNum == 2)
//		{
//			LED1_OFF();
//		}
		if (KeyNum == 1)
		{
			LED1_Turn();
		}
		if (KeyNum == 2)
		{
			LED2_Turn();
		}
	}
}

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_1 | GPIO_Pin_11;  			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
}

uint8_t Key_GetNum(void)
{
	uint8_t KeyNum = 0;
	if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
	{
		Delay_ms(20);
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);
		Delay_ms(20);
		KeyNum = 1;
	}
	if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
	{
		Delay_ms(20);
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0);
		Delay_ms(20);
		KeyNum = 2;
	}
	return KeyNum;
}

2.光敏传感器控制蜂鸣器

当遮住光线时,输出指示灯灭,代表输出高电平;

当有光线时,输出指示灯亮,代表输出低电平。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RFxlmNYF-1680926987367)(32单片机.assets/image-20230404150544934.png)]

主函数:

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

int main(void)
{
	Buzzer_Init();
	LightSensor_Init();
	while (1)
	{
		if (LightSensor_Get() == 1)  //光线暗
		{
			Buzzer_ON();
		}
		else
		{
			Buzzer_OFF();
		}
	}
}

蜂鸣器函数:(.c)

#include "stm32f10x.h"                  // Device header

void Buzzer_Init(void)
{
	//step1:使用RCC开启GPIO的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
	//step2:使用GPIO_Init函数初始化GPIO
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;	//(Out_PP:Out Push Pull)推挽输出(高低电平都是有驱动能力)
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	
	GPIO_SetBits(GPIOA, GPIO_Pin_12);  
}

void Buzzer_ON(void)
{
	GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}

void Buzzer_OFF(void)
{
	GPIO_SetBits(GPIOB, GPIO_Pin_12);
}

void Buzzer_Turn(void)
{
	if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0) 
	{
		GPIO_SetBits(GPIOB, GPIO_Pin_12);
	}
	else
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);
	}
}

光敏传感器函数:(.c)

#include "stm32f10x.h"                  // Device header

void LightSensor_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_13;  			
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
}

uint8_t LightSensor_Get(void)	//返回端口值的函数
{
	return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值