diff --git a/BSP/Buzzer.c b/BSP/Buzzer.c new file mode 100644 index 0000000..4b8ec41 --- /dev/null +++ b/BSP/Buzzer.c @@ -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); + } +} \ No newline at end of file diff --git a/BSP/Buzzer.h b/BSP/Buzzer.h new file mode 100644 index 0000000..dbba512 --- /dev/null +++ b/BSP/Buzzer.h @@ -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 diff --git a/BSP/LightSensor.c b/BSP/LightSensor.c new file mode 100644 index 0000000..2667688 --- /dev/null +++ b/BSP/LightSensor.c @@ -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); +} \ No newline at end of file diff --git a/BSP/LightSensor.h b/BSP/LightSensor.h new file mode 100644 index 0000000..f40a3d0 --- /dev/null +++ b/BSP/LightSensor.h @@ -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 diff --git a/CMakeLists.txt b/CMakeLists.txt index 91600c1..13eb9f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/User/main.c b/User/main.c index 2a74e6b..612dab5 100644 --- a/User/main.c +++ b/User/main.c @@ -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(); - } - } }