There are two ways for single chip microcomputer to drive the other buzzer: one is to drive directly through PWM output port, and the other is to use I/O timing to flip the level to generate driving waveform to drive the buzzer.
The direct drive of PWM output port is to directly drive the buzzer by using a certain square wave output from PWM output port itself. There are several system registers in the software setting of single chip microcomputer to set the output of PWM port, such as duty cycle and cycle. After setting these registers to generate a waveform with a frequency meeting the requirements of the buzzer, as long as the PWM output is turned on, the PWM output port can output a square wave with this frequency, and then the buzzer can be driven by this waveform. For example, if we drive a buzzer with a frequency of 2000Hz, we can know that the period is 500μs, then we only need to set the PWM period to 500μs and the duty cycle level to 250μs, and we can generate a square wave with a frequency of 2000Hz, and then drive the buzzer with a transistor through this square wave.
However, it will be a bit troublesome to use I/O timing to flip the level to generate the driving waveform. A timer must be used for timing, and a waveform that meets the requirements of the buzzer is generated by timing to flip the level, and the buzzer is driven by this waveform. For example, if the buzzer is driven at 2500Hz, we can know that the period is 400μs, so that every 200μs, the I/O port of the buzzer can generate a square wave with a frequency of 2500Hz and a duty ratio of 1/2duty, and then the buzzer can be driven by transistor amplification.