Welcome to MSPM0G3519 Development Board repository
3. Drive Buzzer
Drive Buzzer
1. Hardware Wiring
This case uses the MSPM0 Robot Expansion Board. The peripherals used do not require additional wiring. You only need to insert the Yahboom MSPM0G3519 core board into the expansion board.
2. Code Explanation
empty.c
xint main(void){ USART_Init();
PWM_Buzzer_Init(); while (1) { Buzzer_Toggle(); delay_ms(1000); }}PWM_Buzzer_Init: PWM timer enable, used to drive the buzzer.
Buzzer_Toggle: Buzzer on/off toggle. Here set to sound for one second, 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 buzzer volume by setting a certain PWM duty cycle
Buzzer_OFF: Set PWM duty cycle to 0, buzzer does not sound
3. Program Phenomenon
After burning the program, press the NRST key to reset. You can hear the buzzer on the expansion board sound for one second and stop for one second, repeatedly.