首页 > 其他分享 >04_GPIO输入

04_GPIO输入

时间:2024-01-13 17:36:55浏览次数:31  
标签:__ 04 Pin void Init InitStruct GPIO 输入

GPIO输入

按键介绍

image-20240106140218311

传感器模块介绍

image-20240106140154648

硬件电路

image-20240106141200885

C语言数据类型

image-20240106184424801

按键控制LED

接线图

image-20240107122808027

代码

LED.c

#include "stm32f10x.h"                  // Device header

void LED_Init(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_1 | GPIO_Pin_2;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}

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

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

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_ON(void)
{
	GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}

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

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

#ifndef __LED_H__
#define __LED_H__

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

#endif

Key.c

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

void Key_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_11;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
}

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

Key.h

#ifndef __KEY_H__
#define __KEY_H__

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

#endif

main.c

#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)
	{
		KeyNum = Key_GetNum();
		if(KeyNum == 1)
		{
			LED1_Turn();
		}
		if(KeyNum == 2)
		{
			LED2_Turn();
		}
	}
}

光敏传感器控制蜂鸣器

接线图

image-20240108124906696

代码

Buzzer.c

#include "stm32f10x.h"                  // Device header

void Buzzer_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	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);
}

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

#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

#include "stm32f10x.h"                  // Device header

void LightSensor_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);
}

uint8_t LightSensor_Get(void)
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);
}

LightSensor.h

#ifndef __LIGHT_SENSOR_H__
#define __LIGHT_SENSOR_H__

void LightSensor_Init(void);
uint8_t LightSensor_Get(void);

#endif

main.c

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

标签:__,04,Pin,void,Init,InitStruct,GPIO,输入
From: https://www.cnblogs.com/mzx233/p/17962636

相关文章

  • 03_GPIO输出
    GPIO输出简介GPIO基本结构GPIO位结构GPIO模式四种输入模式GPIO_Mode_IN_FLOATING浮空输入模式GPIO_Mode_IPU上拉输入模式GPIO_Mode_IPD下拉输入模式GPIO_Mode_AIN模拟输入模式四种输出模式GPIO_Mode_Out_OD开漏输出模式GPIO_Mode_Out_PP推挽输出模式G......
  • 解决VMware 虚拟机 ubuntu 20.04 异常关闭导致虚拟网卡 ens33 无法工作问题
    问题描述由于经常使用SSH远程链接VMware中的虚拟机ubuntu,每次关闭都是挂起,时间久了,虚拟机运行有些卡顿了,此时可以通过Linux命令重启或者关闭ubuntu,也可以之间使用VMWare中的【虚拟机】--【电源】->【关闭客户机】强行关闭正在运行的虚拟机但是这个强行关闭正在运行的......
  • VMware 虚拟机 ubuntu 20.04 硬盘扩容方法
    前言最近由于需要编译【RK3568】的LinuxSDK,发现虚拟机默认的200G空间不足了,因此想增加这个200G空间的限制,通过网络上查找了一些方法,加上自己亲自验证,确认硬盘扩容正常,方法也比较的容易,所以做个笔记记录下来。操作步骤如下首先VMware虚拟机ubuntu20.04需要【关机】,......
  • ubuntu 20.04 自由切换 python 的版本
    问题描述当前ubuntu20.04默认安装了多个python的版本,执行python时,默认版本是Python2.7.18zhangsz@zhangsz:~$pythonPython2.7.18(default,Jul12022,12:27:04)[GCC9.4.0]onlinux2Type"help","copyright","credits"or"license"......
  • Ubuntu 20.04版本安装k8s控制节点
    一、环境配置服务器配置:2核4GIP:192.168.10.23主机名:master4将改主机加入此集群#1.修改主机名hostnamectlset-hostnamemaster4&&bash#2.添加hosts127.0.1.1master4192.168.10.20master192.168.10.21master2192.168.10.22master3192.168.10.23master419......
  • 【APP逆向04】Frida的下载与安装
    HOOK是什么?Hook框架是一种技术,用于在运行时拦截和修改应用程序的行为。通过Hook,你可以劫持应用程序的方法调用、修改参数、篡改返回值等,以达到对应用程序的修改、增强或调试的目的。常见的hook框架XposedFramework:Xposed是一个功能强大的开源Hook框架,可以在不修改......
  • [oeasy]python0004_游乐场_和python一起玩耍_python解释器_数学运算
    和python玩耍......
  • Linux Shell接收键盘输入
    1.read命令格式read[选项][变量名]选项:-p“提示信息”:在等待read输入时,输出提示信息-t“秒数”:read命令会一致等待用户输入,使用此选项可以指定等待时间-n“字符数”:read命令只接受指定的字符数,就会执行-s:隐藏输入的数据,适用于机密信息的......
  • ORA-01041: internal error: hostdef extension doesn't exist错误侦察
    如果在使用netca工具安装监听时就发生了ORA-01041:internalerror:hostdefextensiondoesn'texist的错误,可能是由于配置或环境设置的问题。以下是一些建议的步骤:检查环境变量:确保ORACLE_HOME和ORACLE_SID等必要的环境变量已经正确设置。在使用netca工具时,确保使用了......
  • Powersehll交互式输入定义方法-03
    定义和使用交互式输入时,有一些注意事项需要考虑。以下是一些常见的注意事项:提示信息清晰:在提示用户输入时,确保提示信息清晰、明确,并提供必要的上下文说明,使用户知道预期的输入内容。输入验证和错误处理:对于用户输入的值,进行必要的验证和错误处理。确保输入符合预期的格式、类型或范......