STM32自学教程:光敏电阻触发蜂鸣器报警实例

9e83eaaedc304951b9f6dac2956022e4.jpg

 特别说明:本程序是接DO口的

buzzer.c文件

#include "stm32f10x.h"

#include "buzzer.h"

 

void buzzer_Init(void)

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

 GPIO_InitTypeDef GPIO_InitStruct;

 GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;

 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;

 GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;

 GPIO_Init(GPIOB,&GPIO_InitStruct);

 GPIO_ResetBits(GPIOB,GPIO_Pin_12);

}

//蜂鸣器报警

void buzzer_Open(void)

{

 GPIO_SetBits(GPIOB,GPIO_Pin_12);

}

//蜂鸣器不报警

void buzzer_Off(void)

{

 GPIO_ResetBits(GPIOB,GPIO_Pin_12);

}

buzzer.h文件

#ifndef _BUZZER_H_

#define _BUZZER_H_

void buzzer_Init(void);

void buzzer_Open(void);

void buzzer_Off(void);

void buzzer_turn(void);

#endif

photosensitive.c文件

#include "stm32f10x.h"

#include "photosensitive.h"

void Photosensitive_Init(void)

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

 GPIO_InitTypeDef GPIO_InitStruct;

 GPIO_InitStruct.GPIO_Pin=GPIO_Pin_12;

 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IPU; //上拉输入

 GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;

 GPIO_Init(GPIOB,&GPIO_InitStruct);

}

//获取光敏电阻传感器的状态

uint16_t Photosensitive_Get(void)

{

 return GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13);

}

photosensitive.h文件

#ifndef _PHOTOSENSITIVE_H_

#define _PHOTOSENSITIVE_H_

#include "stdint.h"

void Photosensitive_Init(void);

uint16_t Photosensitive_Get(void);

#endif

main.c文件

#include "stm32f10x.h"

#include "buzzer.h"

#include "delay.h"

#include "photosensitive.h"

int main (void)

 buzzer_Init();

 Photosensitive_Init();

 while(1)

 {

  if(Photosensitive_Get()==1)

  {

   buzzer_Open();

  }else

  {

   buzzer_Off();

  }

 }

}

 

作者:鯨觞

物联沃分享整理
物联沃-IOTWORD物联网 » STM32自学教程:光敏电阻触发蜂鸣器报警实例

发表评论