2024-07-31 11:59 AM - edited 2024-07-31 12:00 PM
Hello guys !
I am new at the embedded systems and I am trying figure it out !
I am using STM32F446VET7.
I connected my buzzer to an GPIO pin(PB12) which has no pwm speciliaties.
But my buzzer is not working when I connected to the DC signal source like 5V. So I need an PWM kinda source.
When I resarch I only found source codes for PWM Generation channels with Timer. My GPIO has no timer and I cannot change my design right now.
Can I do the same job with Systick timer or something like that. I just need a 1 second beep sound, nothing more.
Here is my design in hardware
and the datasheet of my buzzer
Do I need to generate a signal with 50% duty cycle at a frequency of 2048 Hz, if you can just give me an idea, I can research and learn how to do it. Thanks
Solved! Go to Solution.
2024-07-31 12:07 PM - edited 2024-07-31 12:08 PM
Sound is vibration, so yes you need a square wave or other waveform to drive the buzzer.
You can toggle the GPIO pin in an interrupt which is called at a regular rate. This will probably give fine results, although a dedicated timer PWM channel would be better.
Set up a timer to interrupt at 2*2048Hz and toggle the pin in the PeriodElapsed callback.
Another method would be to set up a DMA transfer triggered by a timer which toggles the pin at a regular rate by writing to the GPIOB->BSRR register. This will free up CPU resources, but is a bit more complicated to implement.
2024-07-31 12:07 PM - edited 2024-07-31 12:08 PM
Sound is vibration, so yes you need a square wave or other waveform to drive the buzzer.
You can toggle the GPIO pin in an interrupt which is called at a regular rate. This will probably give fine results, although a dedicated timer PWM channel would be better.
Set up a timer to interrupt at 2*2048Hz and toggle the pin in the PeriodElapsed callback.
Another method would be to set up a DMA transfer triggered by a timer which toggles the pin at a regular rate by writing to the GPIOB->BSRR register. This will free up CPU resources, but is a bit more complicated to implement.
2024-07-31 12:49 PM - edited 2024-07-31 12:56 PM
@testbenchmark wrote:my buzzer is not working when I connected to the DC signal source like 5V. So I need an PWM kinda source
You don't need PWM - you just just need an oscillating signal; ie, something which switches between low and high at a suitable frequency.
You could do that in a simple loop, toggling a pin - just like blinking an LED, but at a much higher frequency (an audio frequency).
Or a simple timer which gives a frequency output.
PWM = Pulse Width Modulation; ie, varying the width of a pulse - you don't need that.
EDIT:
@testbenchmark wrote:Do I need to generate a signal with 50% duty cycle at a frequency of 2048 Hz,
No, you don't need to - those just happen to be the conditions for which they've stated the power consumption.
The power consumption will vary with both frequency and duty cycle.
However, as those are the conditions they've chosen to use as the basis of their specification, it likely that those are conditions under which the thing will work well !
2024-07-31 01:16 PM
Hi TDK.
I use TİM2 for interrupt.
and there is my codes :
and this is worked well. I can hear my beep sound now. Thank you.
One last question : Does the timer counting and interrupt generation consume CPU cycles? Can I avoid this with HAL_TIM_Base_Stop_IT(&htim2); when buzzer not in use?
2024-07-31 01:40 PM
@testbenchmark wrote:Does the timer counting and interrupt generation consume CPU cycles?
The timer counting doesn't, and the generation of the interrupt doesn't, but the code that runs in your interrupt handler does.
Just for driving a buzzer, you don't need any interrupt at all - the timer itself can drive an IO pin. That would consume no CPU cycles at all (apart from the initial configuration).
2024-07-31 03:36 PM
Yes, and yes. :)