光敏电阻控制蜂鸣器

This commit is contained in:
2026-08-11 23:41:24 +08:00
parent beea93edb8
commit ded38a7c44
6 changed files with 77 additions and 15 deletions

27
BSP/Buzzer.c Normal file
View File

@@ -0,0 +1,27 @@
#include "stm32f10x.h"
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_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
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_TOGGLE(void) {
if (GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_12) == 0) {
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}else {
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
}

12
BSP/Buzzer.h Normal file
View File

@@ -0,0 +1,12 @@
#ifndef __BUZZER_H
#define __BUZZER_H
void Buzzer_Init(void);
void Buzzer_ON(void);
void Buzzer_OFF(void);
void Buzzer_TOGGLE(void);
#endif

16
BSP/LightSensor.c Normal file
View File

@@ -0,0 +1,16 @@
#include "stm32f10x.h"
void LightSensor_Init(void) {
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
uint8_t LightSensor_Read(void) {
return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13);
}

8
BSP/LightSensor.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __LIGHT_SENSOR_H
#define __LIGHT_SENSOR_H
void LightSensor_Init(void);
uint8_t LightSensor_Read(void);
#endif //STM32F103_SPL_CLION_LIGHTSENSOR_H

View File

@@ -32,6 +32,10 @@ add_executable(${CMAKE_PROJECT_NAME}
BSP/Delay.c
BSP/LED.c
BSP/Key.c
BSP/Buzzer.c
BSP/Buzzer.h
BSP/LightSensor.c
BSP/LightSensor.h
)
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE

View File

@@ -1,22 +1,17 @@
#include "stm32f10x.h"
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "Buzzer.h"
#include "LightSensor.h"
uint8_t KeyNum;
int main(void){
LED_Init();
Key_Init();
while(1){
KeyNum = Key_GetNum();
if (KeyNum == 1) {
LED0_TOGGLE();
int main(void) {
Buzzer_Init();
LightSensor_Init();
while (1) {
if (LightSensor_Read() == 1) {
Buzzer_ON();
} else {
Buzzer_OFF();
}
if (KeyNum == 2) {
LED1_TOGGLE();
}
}
}