江科大stm32学习笔记——【3-3】GPIO输入:按键控制led&光敏传感器控制蜂鸣器

一.原理

1.传感器模块

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

2.硬件电路

1)按键电路

上面两个是下接按键,下面两个是上接按键。++一般用下接按键的方式++。

图一:当按键按下时,PA0被直接下拉到GND,此时读取PA0口的电压就是低电平。

当按键松手时,PA0被悬空,引脚电压不确定,所以必须要求PA0是上拉输入的模式,否则会出现引脚电压不确定的情况,这样引脚再悬空,PA0就是高电平。

图二:外部接了上拉电阻。

当按键松手时,引脚由于上拉作用,自然保持高电平。

当按键按下时,引脚直接接到GND。

2)传感器模块电路

二.按键控制LED

1.硬件连接

2.程序代码

在工程文件夹里新建一个"Hardware"文件夹来存放LED和按键的驱动程序

LED.c:

cs 复制代码
#include "stm32f10x.h"                  // Device header

void LED_Init(void)//初始化LED
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOA,&GPIO_InitStructure);
	
	GPIO_SetBits(GPIOA,GPIO_Pin_1);
	GPIO_SetBits(GPIOA,GPIO_Pin_2);//都设为高电平关掉LED方便后续亮灯

}

void LED1_ON(void)
{
	GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}

void LED1_OFF(void)
{
	GPIO_SetBits(GPIOA,GPIO_Pin_1);
}

void LED2_ON(void)
{
	GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}

void LED2_OFF(void)
{
	GPIO_SetBits(GPIOA,GPIO_Pin_2);
}

void LED1_Turn(void)//灯取反
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_1);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_1);
	}
}

void LED2_Turn(void)//灯取反
{
	if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
	{
		GPIO_SetBits(GPIOA,GPIO_Pin_2);
	}
	else
	{
		GPIO_ResetBits(GPIOA,GPIO_Pin_2);
	}
}

LED.h:

cs 复制代码
#ifndef __LED_H
#define __LED_H

void LED_Init(void);
void LED1_ON(void);
void LED2_ON(void);
void LED1_OFF(void);
void LED2_OFF(void);
void LED1_Turn(void);
void LED2_Turn(void);
#endif

Key.c:

cs 复制代码
#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;//没有按按键时默认读取为0
	//读取PB1端口的值:
	if (GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0)
	{
		Delay_ms(20);//刚按下按键时会抖动
		while(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) == 0);//直到松手
		Delay_ms(20);//再消一下抖动
		KeyNum = 1;
	}
	
	//读取PB11端口的值:
	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;
}

Key.h:

cs 复制代码
#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);
uint8_t Key_GetNum(void);

#endif

main.c:

cs 复制代码
#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"

uint8_t KeyNum;//这里的KeyNum是全局变量,作用域不同

int main(void)
{
	LED_Init();
	Key_Init();
	
	//按一下按键亮灭变一下
	while(1)
	{
		KeyNum = Key_GetNum();
		if(KeyNum == 1)
		{
			LED1_Turn();
		}
		if(KeyNum == 2)
		{
			LED2_Turn();
		}
	}
}

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

1.硬件

2.程序

蜂鸣器的光敏传感器的驱动程序(和LED类似)

Buzzer.c:

cs 复制代码
#include "stm32f10x.h"                  // Device header

void Buzzer_Init(void)//初始化LED
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	
	GPIO_Init(GPIOB,&GPIO_InitStructure);
}

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);
	}
}

Buzzer.h:

cs 复制代码
#ifndef __BUZZER_H
#define __BUZZER_H

void Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);
void Buzzer_Turn(void);

#endif

LightSensor.c:

cs 复制代码
#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);
}

LightSensor.h:

cs 复制代码
#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H

void LightSensor_Init(void);
uint8_t LightSensor_Get(void);
#endif

main.c:

cs 复制代码
#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();
		}
	}
}
相关推荐
小白黑_2166 分钟前
设计模式笔记
笔记·设计模式
新手unity自用笔记27 分钟前
项目-坦克大战学习笔记-按键按下控制方向
笔记·学习·c#
调了个寂寞39 分钟前
INS风格时尚自拍人像摄影后期Lr调色,手机滤镜PS+Lightroom预设下载!
笔记
瞌睡不来40 分钟前
(刷题记录5)盛最多水的容器
c++·笔记·学习·题目记录
一 乐1 小时前
考研论坛平台|考研论坛小程序系统|基于java和微信小程序的考研论坛平台小程序设计与实现(源码+数据库+文档)
java·数据库·学习·考研·微信·小程序·源码
武汉芯景科技有限公司2 小时前
关于武汉芯景科技有限公司的IIC电平转换芯片XJ9509开发指南(兼容PCa9509)
科技·stm32·单片机·嵌入式硬件
William_Edmund2 小时前
C++ 算法学习——1.8 悬线法
学习
朝九晚五ฺ2 小时前
【Linux探索学习】第三弹——Linux的基础指令(下)——开启新篇章的大门
linux·运维·学习
IM_DALLA2 小时前
【Verilog学习日常】—牛客网刷题—Verilog企业真题—VL74
学习·fpga开发·verilog学习
王俊山IT3 小时前
C++学习笔记----8、掌握类与对象(五)---- 嵌套类与类中枚举
开发语言·c++·笔记·学习