Buzzer: electronic Xiangqi an integrated structure. Divided into partial pressure buzzer and electromagnetic buzzer two types.
An active / passive buzzer ( not refer to whether the power band, but there is no built-oscillating circuit )
1, active buzzer : active buzzer own oscillator circuit, a sound will be energized.
2, passive buzzer : oscillator circuit does not own, requires externally provided about 2 ~ 5Khz square wave drive, too can sound.
Second, the hardware design
Because a single STM32 IO port can provide 25mA maximum current, drive current buzzer is about 30mA, the two are very similar, but the entire chip STM32 current, the maximum it is about 150mA, if the direct drive with the IO port buzzer shortage may occur when using other functions current. So we then flows through the drive transistor comprises a buzzer (S8050), so STM32 IO ports need only provide current of less than 1mA is sufficient.
R33 is mainly used to prevent accidental sounding the buzzer , when PB.8 outputs a high level when the buzzer will sound when PB.8 outputs a low level when the buzzer stops sounding .
Third, software design
1 // Functions 2 #include " beep.h " . 3 #include " stm32f10x.h " . 4 . 5 . 6 void BEEP_Init ( void ) . 7 { . 8 GPIO_InitTypeDef GPIO_BEEP; // create a structure . 9 10 GPIO_BEEP.GPIO_Mode = GPIO_Mode_Out_PP; / / set the port to push-pull outputs . 11 GPIO_BEEP.GPIO_Pin = GPIO_Pin_8; // PB.8 port 12 is GPIO_BEEP.GPIO_Speed = GPIO_Speed_50MHz; // 50MHz 13 is 14 RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB, the ENABLE); // initialize GPIOB clock 15 16 GPIO_Init (GPIOB, & GPIO_BEEP); // The parameters are initialized GPIO.8 . 17 }
The main function
. 1 #include " sys.h " 2 #include " delay.h " . 3 #include " beep.h " . 4 . 5 int main ( void ) . 6 { . 7 BEEP_Init (); // buzzer initialization . 8 delay_init (); / / delay function initializes . 9 the while ( . 1 ) 10 { . 11 GPIO_ResetBits (GPIOB, GPIO_Pin_8); // Close the buzzer 12 is Delay_ms ( 1500); // delay of 1.5 seconds 13 is 14 GPIO_SetBits (GPIOB, GPIO_Pin_8); // beeper 15 Delay_ms ( 100 ); // delay of 0.1 seconds 16 } 17 }