【stm32-2】按键控制LED&光敏传感器控制蜂鸣器

5 篇文章 0 订阅

1.按键控制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)主函数部分

#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
#include "LED.h"
#include "key.h"
uint8_t KeyNumber;
int main(void)
{
	LED_Init();
	Key_Init();
	while(1)
	{
		KeyNumber=Key_GetNumber();
		if(KeyNumber==1)
		{
			LED1_Turn();
		}
		if(KeyNumber==2)
		{
			LED2_Turn();
		}
	}
}

(2)key.h

#ifndef __KEY_H
#define __KEY_H

void Key_Init(void);
uint8_t Key_GetNumber(void);



#endif

(3)key.c

#include "Device/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_GetNumber(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;
}

(4)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

(5)LED.c 

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

void LED_Init(void)
{
	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_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)	//读取当前A口状态,为0,置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);
	}
}

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

(1)main.c

#include "Device/Include/stm32f10x.h"   // Device header
#include "Delay.h"
#include "buzzer.h"
#include "lighesensor.h"
int main()
{
	Buzzer_Init();
	Ligntsensor_Init();
	while(1)
	{
		if(Ligntsensor_Get()==1)
		{
			Buzzer_ON();
		}
		else
		{
			Buzzer_OFF();
		}
	}
}

(2)buzzer.c

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

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

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)	//读取当前A口状态,为0,置1.否则,置0。
	{
		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)ligntsesor.c

#include "Device/Include/stm32f10x.h"   // Device header
void Ligntsensor_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 Ligntsensor_Get(void)
{
	return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);//读取光敏传感器输入位高低电平
}

(5)lightsensor.h 

#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H
void Ligntsensor_Init(void);
uint8_t Ligntsensor_Get(void);

#endif

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STM32指南者开发板中,可以使用光敏传感器控制蜂鸣器的代码如下: ```c #include "stm32f4xx.h" void ADC_Configuration(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; // 开启 ADC1 时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // 开启 GPIOA 时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // 配置 ADC1 通道1 对应的 GPIOA 引脚为模拟输入 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); // ADC1 配置 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); // 配置 ADC1 触发源为软件触发 ADC_ExternalTrigConvCmd(ADC1, DISABLE); } void Buzzer_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; // 开启 GPIOB 时钟 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); // 配置 GPIOB 引脚为推挽输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); } uint16_t ReadLightSensorValue(void) { uint16_t adc_value; // 配置 ADC1 通道1 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles); // 启动 ADC1 转换 ADC_Cmd(ADC1, ENABLE); // 等待转换完成 while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); // 读取转换结果 adc_value = ADC_GetConversionValue(ADC1); // 关闭 ADC1 ADC_Cmd(ADC1, DISABLE); return adc_value; } int main(void) { // 初始化 ADC ADC_Configuration(); // 初始化蜂鸣器 Buzzer_Configuration(); while (1) { // 读取光敏电阻传感器的模拟值 uint16_t light_value = ReadLightSensorValue(); // 判断光照强度是否超过阈值 if (light_value > 1000) { // 触发蜂鸣器 GPIO_SetBits(GPIOB, GPIO_Pin_8); } else { // 关闭蜂鸣器 GPIO_ResetBits(GPIOB, GPIO_Pin_8); } } } ``` 在上述代码中,首先通过配置GPIO和ADC初始化函数来设置ADC和蜂鸣器的引脚和参数。然后,在`ReadLightSensorValue`函数中,通过配置ADC的通道和采样时间,启动和等待ADC转换完成,并读取转换结果。在主函数中,使用`ReadLightSensorValue`函数读取光敏电阻传感器的模拟值,并根据阈值判断是否触发蜂鸣器。 请注意,此代码仅为示例,具体的配置和使用方法可能会因STM32型号和使用的开发环境而有所不同。在实际应用中,请参考相关的STM32文档和示例代码进行适当的修改和调整。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值