光敏电阻控制蜂鸣器
This commit is contained in:
27
BSP/Buzzer.c
Normal file
27
BSP/Buzzer.c
Normal 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
12
BSP/Buzzer.h
Normal 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
16
BSP/LightSensor.c
Normal 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
8
BSP/LightSensor.h
Normal 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
|
||||||
@@ -32,6 +32,10 @@ add_executable(${CMAKE_PROJECT_NAME}
|
|||||||
BSP/Delay.c
|
BSP/Delay.c
|
||||||
BSP/LED.c
|
BSP/LED.c
|
||||||
BSP/Key.c
|
BSP/Key.c
|
||||||
|
BSP/Buzzer.c
|
||||||
|
BSP/Buzzer.h
|
||||||
|
BSP/LightSensor.c
|
||||||
|
BSP/LightSensor.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
|
target_include_directories(${CMAKE_PROJECT_NAME} PRIVATE
|
||||||
|
|||||||
25
User/main.c
25
User/main.c
@@ -1,22 +1,17 @@
|
|||||||
#include "stm32f10x.h"
|
#include "stm32f10x.h"
|
||||||
#include "Delay.h"
|
#include "Delay.h"
|
||||||
#include "LED.h"
|
#include "Buzzer.h"
|
||||||
#include "Key.h"
|
#include "LightSensor.h"
|
||||||
|
|
||||||
uint8_t KeyNum;
|
uint8_t KeyNum;
|
||||||
int main(void){
|
|
||||||
|
|
||||||
LED_Init();
|
int main(void) {
|
||||||
Key_Init();
|
Buzzer_Init();
|
||||||
while(1){
|
LightSensor_Init();
|
||||||
|
while (1) {
|
||||||
KeyNum = Key_GetNum();
|
if (LightSensor_Read() == 1) {
|
||||||
if (KeyNum == 1) {
|
Buzzer_ON();
|
||||||
LED0_TOGGLE();
|
} else {
|
||||||
|
Buzzer_OFF();
|
||||||
}
|
}
|
||||||
if (KeyNum == 2) {
|
|
||||||
LED1_TOGGLE();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user