tenbaht / sduino

An Arduino-like programming API for the STM8
http://tenbaht.github.io/sduino/
GNU Lesser General Public License v2.1
347 stars 213 forks source link

tone() support or workaround? #124

Open H3wastooshort opened 3 years ago

H3wastooshort commented 3 years ago

I would really like to have the tone() function or some way to replicate it's functionality, if it's not too much of a hastle to implement. AFAIK from the Datasheet, the STM8S003?3 series has a built-in tone generator.

maxgerhardt commented 3 years ago

It has a tone generator, the "BEEPER" periperheral. That thing however only has 3 possible output frequencies: 1, 2 or 4 kHz (refer datasheet / user manual and e.g. here). So it can't support arbitrary audible frequencies from like 0-44kHz with that.

But ofc it should be possible with a timer / PWM output. I've outlined some initial ideas some time ago there.

However a simple high-low write and a microsecond delay works on my STM8S103F3 (and pretty much every other). I've slightly adapted a sketch for a tetris theme and it plays nicely on a buzzer, with simulated 2-tone polyphony, with the play_one_note function as basis.

stefaandesmet2003 commented 3 years ago

for what it's worth, I added an implementation here : https://github.com/stefaandesmet2003/sduino It's using TIM2 (similar to AVR), but STM8 TIM2 is 16-bit. STM8S003 has TIM2 peripheral, so it should work too.

H3wastooshort commented 3 years ago

very cool! you can open a pull request

stefaandesmet2003 commented 3 years ago

agree, but it looks like this repo is a bit stalled for the moment