(STM32-GPIO输入)按键控制LED、光敏传感器控制蜂鸣器

读取输入数据寄存器某一端口的输入值:

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

1.按键控制LED

LED端口:GPIOA_Pin_1 | GPIOA_Pin_2

按键端口:GPIOB_Pin_1 |GPIOB_Pin_11

程序使用模块化编程(LED.c、Key.c)

(1)初始化LED

void GPIO_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

    GPIO_InitTypeDef GPIO_InitStructure;
    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_Pin_2);
}

(2)定义LED1亮、灭、反转

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_ReadInputDataBit(GPIOA, GPIO_Pin_1) == 0) //读取LED的GPIO口状态并进行判断
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_1);
		
	}
	else
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_1);
	
	}

}

(2)定义LED2亮、灭、反转

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_ReadInputDataBit(GPIOA, GPIO_Pin_2) == 0)
	{
		GPIO_SetBits(GPIOA, GPIO_Pin_2);
		
	}
	else
	{
		GPIO_ResetBits(GPIOA, GPIO_Pin_2);
	
	}

}

(3)配置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);


#enif

(4)初始化按键

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

}

(5)获取按键值

uint8_t Key_GetNum(void) //unsigned char类型 
{
	uint8_t KeyNum = 0;
	if(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)//如果按键按下
	{
		Delay_ms(20);//延时消抖
		while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0);//读取按键GPIO口状态,判断按键是否松手
		Delay_ms(20);//延时消抖
		KeyNum = 1;//GPIOB_Pin_1 为按键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;//GPIOB_Pin_11 为按键2
	
	}	
	
	return KeyNum;
}

(6)配置Key.h文件

#ifndef __KEY_H
#define __KEY_H
void Key_Init(void);
uint8_t Key_GetNum(void);

#endif

(7)主函数

#include "stm32f10x.h"   
#include "Delay.h"
#include "LED.h"
#include "Key.h"

uint8_t KeyNum;

 int main(void)
 {
	LED_Init(); //LED初始化
	Key_Init(); //按键初始化

	while(1)
	{
		KeyNum = Key_GetNum(); //获取按键编号
		if (KeyNum == 1) //如果按键1按下
		{
			LED1_Turn(); //LED1状态反转
		}
		if (KeyNum == 2 ) //如果按键2按下
		{
			LED2_Turn(); //LED2状态反转
		}
	}
	
 }
 

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

光敏传感器:GPIOB_Pin_12

蜂鸣器:GPIOB_Pin_13

(1)初始化蜂鸣器

void Buzzer_Init(void)
{
	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);
	
	GPIO_SetBits(GPIOB, GPIO_Pin_12);
}

(2)定义蜂鸣器开启、关闭、状态反转

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口状态
	{
		GPIO_SetBits(GPIOB, GPIO_Pin_12);
	}
	else
	{
		GPIO_ResetBits(GPIOB, GPIO_Pin_12);
	}
}

(3)配置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

(4)初始化光敏传感器


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); //读取传感器GPIO口状态值,并返回
}

(5)配置LightSensor.h文件

#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H

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

#endif

(6)主函数

#include "stm32f10x.h"                 
#include "Delay.h"
#include "Buzzer.h"
#include "LightSensor.h"

int main(void)
{
	Buzzer_Init();      //初始化蜂鸣器
	LightSensor_Init(); //初始化光敏传感器
	
	while (1)
	{
		if (LightSensor_Get() == 1)  //若读取光敏传感器状态为1,既被遮挡,则蜂鸣器开启
		{
			Buzzer_ON();
		}
		else
		{
			Buzzer_OFF();
		}
	}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值