#include "libmaple/dac.h"
void setup() {
dac_init(DAC, DAC_CH1);
// MAMP = 1010, WAVE = 10, TSEL = 111, TEN = 1, EN = 1
DAC_BASE->CR |= 2749;
pinMode(PA4, OUTPUT); // DAC_CH1 shares the same pin with SPI1_NSS pin (PA4). So it has to be initialized again, as output.
dac_write_channel(DAC, DAC_CH1, 0);
}
void loop() {
DAC_BASE->SWTRIGR = DAC_SWTRIGR_SWTRIG1;
}
I kept banging my head against the wall and eventually figured out the answer I was looking for, which was mentioned nowhere in those docs.
For anyone else with a similar problem who may find this thread, the key was to set the Master Mode Selection (MMS) bits in the timer’s Control Registers to ‘010’ for ‘Update’.
I kept banging my head against the wall and eventually figured out the answer I was looking for, which was mentioned nowhere in those docs.
For anyone else with a similar problem who may find this thread, the key was to set the Master Mode Selection (MMS) bits in the timer’s Control Registers to ‘010’ for ‘Update’.
…is it safe to connect an 8Ω speaker to the DAC pin(s)? if not, can I add resistors / capacitors to make it safe?
Thanks!

The maximum DAC output voltage is 3.3v the load resistance is 5k, so we will be talking mW of power coming from the speaker.
For any practical application you will need to amplify the output. Fortunately this is pretty easy these days… you could mess about and use a transistor … or you could cut out all the guesswork and fire the signal straight in to a ready made amplifier circuit designed for single rail 3v3 operation, which also happens to be as cheap as chips. I would suggest a PAM8403 board, since you can pick up 10 for £0.99
Search ebay or Ali for PAM8402
Typical vendor ->
http://www.ebay.co.uk/itm/10-PAM8403-2X … SwZQRYfbfP
Datasheet ->
http://www.allelectronics.com/mas_asset … P-8403.pdf
It will drive two off 4 or 8 Ohm 3W speakers, and claims to operate on anything from 2.5V to 5.5V
The reason these board are so cheap is that they are found in all manner of low cost bluetooth speakers and other cheap consumer items.
At 10 to 20 pence each in qty 10 for a 3W Class D amplifier board… why would you want to mess around with a couple of transistor and passives.
http://www.ebay.com/itm/AC-DC-12V-TDA72 … SwEzxYTiUA
They are quite powerful and will drive a decent size speaker.
But they need a lot of voltage.
They just about work on 5V, but I ended up running mine from 18V to really pump out the music !
I also bought a amplifer kit (cant find the link at the moment), which had separate left and right amplifier modules and more smoothing caps etc, but as far as I could tell, the audio quality wasnt anybetter than the prebuilt $2.50 module
Supposedly they work fine at 5 and 3.3V, so should be able to supply them from the maple mini unless requiring a lot of power.
For a small 8ohms speaker should be enough.
Also when writing my PWM wave code, I drove a small 8 or 3 ohms speaker directly from the pin with a small RC filter, and finally added a FET to avoid damaging the pin in case of short, but the volume was about the same when using the GPIO pin directly as when using the FET.
For such a small load I would think almost any small transistor he can find around could work.
I have some of these, which work from 5V even though their spec says 6V
http://www.ebay.com.au/itm/1PCS-DC-6-to … SwxvxW7LNJ
PS. Just both those modules andy linked to.
Strangely the 10 x 99p seems a OK only deal, ie. only in UKP, so I paid $1.63 AUD (roughly the same priced based on the current exchange rate)
I have some of these, which work from 5V even though their spec says 6V
http://www.ebay.com.au/itm/1PCS-DC-6-to … SwxvxW7LNJ
PS. Just both those modules andy linked to.
Strangely the 10 x 99p seems a OK only deal, ie. only in UKP, so I paid $1.63 AUD (roughly the same priced based on the current exchange rate)
eBay does strange things, though not as bad as Amazon
I mean I could not get a good price, unless I used Andy’s link which was priced in UKP
If I look for the same product listed with a AUD price, the cheapest is twice the price
http://www.ebay.com.au/itm/10x-PAM8403- … Sw9GhYmY3f
I must remember to search on ebay.co.uk as well as ebay.com.au as its sometimes cheaper on ebay.co.uk or ebay.com
I already had some 2222 NPN transistors around, so rather than wait for an eBay order to arrive I built this circuit, and it seems to work fine:

Theoretically, would something like this:
http://www.ebay.com.au/itm/152413164887
…work directly from the DAC pins to a 100Ω (in this case) speaker? or is that still more than the STM32 can cope with?

- Table.png (26.25 KiB) Viewed 487 times
Table.png
Take a listen:
Some of the notes are a little off, because we don’t really have enough resolution with the timer to get decimal point nanosecond accuracy… but it was a fun little exploration anyway! ![]()


