This example demonstrates how to use an DAC Audio output.

Dependencies:   mbed

Intro

This example demonstrates how to use an DAC Audio output.

Parts

STM32 Nucleo F446RE
Audio jack(TS or TRS)
Register 10k, 4.7k
Capacitor 0.01uF, 2.2uF
Breadboard
Wires
Computer Speakers

Wiring diagram

/media/uploads/beaglescout007/nucleo_ex02_dac.png This circuit diagram was created by fritzing.

https://youtu.be/coA-wrBVpG8

main.cpp

Committer:
beaglescout007
Date:
2016-03-10
Revision:
1:b018dbb87a88
Parent:
0:4df3adbe8435

File content as of revision 1:b018dbb87a88:

#include "mbed.h"

// DAC 20khz
#define DAC_Frequency 20000

AnalogOut dac(PA_4);

DigitalIn btn(USER_BUTTON);

Timer tim;
Ticker tic;

// C D E F G A B C
int freq_table[] = {262, 293, 330, 349, 392, 440, 493, 523};
int sel;

void DAC_Int(void)
{
    int period = 1000000 / freq_table[sel];
    int pos = tim.read_us() % period;

    // Square wave
    //dac = pos > (period / 2) ? 1 : 0;
    
    // Sine wave
    dac = sin(pos * 3.14f * 2 / period) / 2.0f + 0.5f;
}

int main()
{
    sel = 0;

    tim.start();

    tic.attach_us(&DAC_Int, 1000000 / DAC_Frequency);

    while(1)
    {
        if (btn == 0)
        {
            sel++;
            if (sel > 7) sel = 0;
            wait(0.5);
        }
    }
}