cancel
Showing results for 
Search instead for 
Did you mean: 

Audio output via onboard DAC's

PAdam.6
Associate II

Hi guys, I've never done this before but has anyone used the onboard DACS to produce sound?

Playing a .wav file.

I know the dacs are only 12bit and it should play 8bit wave samples fine but what about 16bit samples?

I'll be receiving my MCU's next week so I can't run a test.

If anyone has done it please let me know if 12bit audio is close to 16bit audio.

Thanks guys, hope to hear from you soon.

Pete. ​

8 REPLIES 8

You'll need to move the 16-bit signed samples in the .WAV into the 12-bit unsigned range of DAC, obviously you'll truncate the low order precision you were holding, but the wave form will be the same, just rougher.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
PAdam.6
Associate II

Thanks Clive. I knew I had to mask out the upper 4 bits.

Now I cant wait for my MCU's​ to arrive next week. Lol.

​Pete.

Piranha
Chief II

Not upper but lower 4 bits.

int16_t sample;
uint32_t dac = ((sample >> 4) + 0x800) & 0xFFF;

The DAC has left and right aligned registers to pull the top or bottom 12-bits, what's more important is the number space conversion, and the fact the output on the DAC will have a DC offset because all the outputs will be between 0 and 3V, not +/-1.5V

From 16-bit you want to use the TOP 12 most significant bits, and lose the 4 least significant bits

if (x < -32767) x = -32767; or if (x == 0x8000) x = 0x8001; // keep within +/-32767

RIGHT ALIGNED: y = (((x + 0x8000) >> 4) & 0xFFF) or

LEFT ALIGNED: y = ((x + 0x8000) & 0xFFF0)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
PAdam.6
Associate II

Oops, so its the lower 4 bits I have to mask out.

So I just do this ROR R0, R1​, #4 /* R1 has the 16bit sample */

Unless its negative then ROL instead.​

I'm an assembler guy, C wastes to many cycles for me.

Thank you guys for the help. I'll do a test next week when my hardware arrives.

Pete.

Not exactly, the samples are 16-bit signed, you need to adjust the zero point to get to a 12-bit unsigned range.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Also remember you may need an audio amplifier to produce sound: The DAC strength even with buffer might be limited.

Hi, Yep I 'll be using a 5v USB powered external PC Speakers. I think they're 5watts per channel.

Pete