当前位置:   article > 正文

STM32单片机光敏电阻控制蜂鸣器_stm32pb12pb13

stm32pb12pb13

用的是面包板和最小系统板,线上接线图:

此实验需要让光敏电阻的信号灯熄灭时蜂鸣器响,当周围环境亮度低到一定值以下时,光敏电阻模块的输出信号,也就是PB13引脚的输入信号会由低电平变成高电平,当然光敏电阻模块检测的阈值是可调的,螺丝刀可调。我们只要让光敏电阻变成高电平时给蜂鸣器,也就是PB12高电平即可。既然要用到这些引脚,那就要初始化了。

光敏电阻初始化:

  1. #include "stm32f10x.h" // Device header
  2. void LightSensor_Init()
  3. {
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  5. GPIO_InitTypeDef GPIO_LightSensor_InitStructure;
  6. GPIO_LightSensor_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  7. GPIO_LightSensor_InitStructure.GPIO_Pin=GPIO_Pin_13;
  8. GPIO_LightSensor_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  9. GPIO_Init(GPIOB,&GPIO_LightSensor_InitStructure);
  10. }
  11. uint8_t Get_LightInformation()
  12. {
  13. return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);//将该引脚的输入信号返回(0/1)
  14. }

 蜂鸣器初始化:

  1. #include "stm32f10x.h" // Device header
  2. void Buzzer_Init()
  3. {
  4. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  5. GPIO_InitTypeDef GPIO_Buzzer_InitStructure;
  6. GPIO_Buzzer_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  7. GPIO_Buzzer_InitStructure.GPIO_Pin=GPIO_Pin_12;
  8. GPIO_Buzzer_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  9. GPIO_Init(GPIOB,&GPIO_Buzzer_InitStructure);
  10. }
  11. void Buzzer_Reply()
  12. {
  13. GPIO_ResetBits(GPIOB,GPIO_Pin_12);//蜂鸣器响
  14. }
  15. void Buzzer_Close()
  16. {
  17. GPIO_SetBits(GPIOB,GPIO_Pin_12);//蜂鸣器关
  18. }

主函数部分,初始化蜂鸣器和光敏电阻后在while()循环部分用Get_LightInformation()函数检测光敏电阻的输入,根据不同的输入开关蜂鸣器。

  1. #include "stm32f10x.h" // Device header
  2. #include "Buzzer.H"
  3. #include "LightSensor.h"
  4. int main()
  5. {
  6. Buzzer_Init();
  7. LightSensor_Init();
  8. while(1)
  9. {
  10. if(Get_LightInformation()==1)
  11. Buzzer_Reply();
  12. else
  13. Buzzer_Close();
  14. }
  15. }

以上内容经供参考,谢谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/755435
推荐阅读
相关标签
  

闽ICP备14008679号