Welcome to Wheeled Chassis Kit repository
3. Drive the buzzer
Drive buzzer
1. Hardware wiring
This case uses the MSPM0 robot expansion board. The peripherals used in it do not need additional wiring. You only need to insert the Yabo MSPM0G3507 core board into the expansion board.
2. Code explanation
- empty.c
xxxxxxxxxxint main(void){ USART_Init(); PWM_Buzzer_Init(); while (1) { Buzzer_Toggle(); delay_ms(1000); }}- PWM_Buzzer_Init: Start the PWM timer to drive the buzzer.
- Buzzer_Toggle: The buzzer switch is set to ring for one second and stop for one second.
- buzzer.c
xxxxxxxxxxvoid PWM_Buzzer_Init(void) { DL_Timer_startCounter(BUZZER_INST);}void Buzzer_Toggle(void){ static int i=0; if(i==0) { Buzzer_ON(); i=1; } else { Buzzer_OFF(); i=0; }}void Buzzer_ON(void){ DL_TimerA_setCaptureCompareValue(BUZZER_INST, 30, GPIO_BUZZER_C3_IDX);}void Buzzer_OFF(void){ DL_TimerA_setCaptureCompareValue(BUZZER_INST, 0, GPIO_BUZZER_C3_IDX);}- Buzzer_ON: Adjust the buzzer volume by setting a certain PWM duty cycle
- Buzzer_OFF: Set the PWM duty cycle to 0 and the buzzer will not sound
3. Program phenomenon
After burning the program, press the NRST key to reset, and you can hear the buzzer on the expansion board ring for one second, stop for one second, and repeat this process.