#include 'stm32f10x.h'

#define BEEP_PIN GPIO_Pin_8 #define BEEP_PORT GPIOC #define SWITCH_PIN GPIO_Pin_0 #define SWITCH_PORT GPIOA

void GPIO_Configuration(void); void BEEP_On(void); void BEEP_Off(void);

int main(void) { GPIO_Configuration();

while (1) { if(GPIO_ReadInputDataBit(SWITCH_PORT, SWITCH_PIN) == Bit_SET) { BEEP_On(); } else { BEEP_Off(); } } }

void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin = BEEP_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BEEP_PORT, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = SWITCH_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SWITCH_PORT, &GPIO_InitStructure); }

void BEEP_On(void) { GPIO_SetBits(BEEP_PORT, BEEP_PIN); }

void BEEP_Off(void) { GPIO_ResetBits(BEEP_PORT, BEEP_PIN); }

STM32 蜂鸣器开关控制:按键控制蜂鸣器响/静音

原文地址: https://www.cveoy.top/t/topic/btHl 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录