Timer Controlled DAC

uXe
Thu Feb 09, 2017 6:51 am
Using a Maple RET6, have gotten it nicely humming along, software-triggering the STM32’s Triangle Wave generator:

#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;
}


stevestrong
Thu Feb 09, 2017 9:28 am
See the original doc: http://docs.leaflabs.com/static.leaflab … dwaretimer

uXe
Thu Feb 09, 2017 11:12 am
Thanks for the ‘RTFM’ reply. ;)

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’.


ahull
Thu Feb 09, 2017 12:12 pm
uXe wrote:Thanks for the ‘RTFM’ reply. ;)

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’.


uXe
Thu Feb 09, 2017 1:14 pm
ahull wrote:Do you have a code example you can share?

uXe
Fri Feb 10, 2017 11:38 am
Further to this – the datasheet lists the DAC’s “Minimum Resistive Load” (with buffer on) as 5kΩ.

…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!


ahull
Fri Feb 10, 2017 12:57 pm
Could you drive the speaker through a 5k resistor.. Yes… would you be able to hear it… probably not.

Image

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.


RogerClark
Fri Feb 10, 2017 8:07 pm
I have used amplifiers like this

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


victor_pv
Fri Feb 10, 2017 11:05 pm
I’ve got a few of the PAM8403, but haven’t used them yet.

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.


RogerClark
Sat Feb 11, 2017 3:15 am
Edit

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)


victor_pv
Sat Feb 11, 2017 4:54 am
RogerClark wrote:Edit

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)


RogerClark
Sat Feb 11, 2017 6:13 am
Victor

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


uXe
Sat Feb 11, 2017 6:28 am
Thank you for all of the input – appreciated! :)

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:

Image

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?


uXe
Fri Feb 17, 2017 10:12 am
Have now got the STM32’s internal Triangle Wave generator playing musical notes, the table below shows the values used – by taking the time period of a waveform (in milliseconds) at the given frequency, dividing that into 4094 steps (amplitude of 2047 means 2047 steps to the peak of the Triangle, and then another 2047 steps back down again = 4094) this gives a value in nanoseconds. And then choosing an Overflow / ‘Auto-Reload’ timer value to suit.

Table.png
Table.png (26.25 KiB) Viewed 487 times

victor_pv
Fri Feb 17, 2017 7:46 pm
uXe wrote:Have now got the STM32’s internal Triangle Wave generator playing musical notes, the table below shows the values used – by taking the time period of a waveform (in milliseconds) at the given frequency, dividing that into 4094 steps (amplitude of 2047 means 2047 steps to the peak of the Triangle, and then another 2047 steps back down again = 4094) this gives a value in nanoseconds. And then choosing an Overflow / ‘Auto-Reload’ timer value to suit.

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! :)


uXe
Fri Feb 17, 2017 11:33 pm
victor_pv wrote:I have been reading this thread for the knowledge, and I was wondering what is the advantage of generating the notes with the triangular wave generator, over generating them by sending 12bit samples at say 44.1Khz? is it because is simpler at the code level, or the result is better?

RogerClark
Sat Feb 18, 2017 12:43 am
If you want to do wavetable synthesis, you should probably PM @sheepdoll as I think she has done some projects (probably on other processors) using that method.

victor_pv
Sat Feb 18, 2017 4:05 am
RogerClark wrote:If you want to do wavetable synthesis, you should probably PM @sheepdoll as I think she has done some projects (probably on other processors) using that method.

Leave a Reply

Your email address will not be published. Required fields are marked *