How to: D/A Converter

Core Module contains two analog outputs: DAC0 and DAC1. These are real analog outputs, not just PWM outputs.

Example

Both channels can run completely separately with different samplerate. First you need to prepare the output buffer. In this example there’s a sine wave lookup table. The sampling is set on TWR_DAC_SAMPLE_RATE_16K. This together generates on DAC0 ouput 125 Hz sine-wave.

 1#include <application.h>
 2
 3const uint8_t sine_wave[] = {
 40x80, 0x86, 0x8C, 0x93,
 50x99, 0x9F, 0xA5, 0xAB,
 60xB1, 0xB6, 0xBC, 0xC1,
 70xC7, 0xCC, 0xD1, 0xD5,
 80xDA, 0xDE, 0xE2, 0xE6,
 90xEA, 0xED, 0xF0, 0xF3,
100xF5, 0xF8, 0xFA, 0xFB,
110xFD, 0xFE, 0xFE, 0xFF,
120xFF, 0xFF, 0xFE, 0xFE,
130xFD, 0xFB, 0xFA, 0xF8,
140xF5, 0xF3, 0xF0, 0xED,
150xEA, 0xE6, 0xE2, 0xDE,
160xDA, 0xD5, 0xD1, 0xCC,
170xC7, 0xC1, 0xBC, 0xB6,
180xB1, 0xAB, 0xA5, 0x9F,
190x99, 0x93, 0x8C, 0x86,
200x80, 0x7A, 0x74, 0x6D,
210x67, 0x61, 0x5B, 0x55,
220x4F, 0x4A, 0x44, 0x3F,
230x39, 0x34, 0x2F, 0x2B,
240x26, 0x22, 0x1E, 0x1A,
250x16, 0x13, 0x10, 0x0D,
260x0B, 0x08, 0x06, 0x05,
270x03, 0x02, 0x02, 0x01,
280x01, 0x01, 0x02, 0x02,
290x03, 0x05, 0x06, 0x08,
300x0B, 0x0D, 0x10, 0x13,
310x16, 0x1A, 0x1E, 0x22,
320x26, 0x2B, 0x2F, 0x34,
330x39, 0x3F, 0x44, 0x4A,
340x4F, 0x55, 0x5B, 0x61,
350x67, 0x6D, 0x74, 0x7A
36};
37
38void application_init(void)
39{
40    twr_dac_init(TWR_DAC_DAC0);
41
42    twr_dac_config_t dac_config;
43
44    dac_config.buffer = (uint8_t*)sine_wave;
45    dac_config.length = sizeof(sine_wave);
46    dac_config.data_size = TWR_DAC_DATA_SIZE_8;
47    dac_config.mode = TWR_DAC_MODE_CIRCULAR;
48    dac_config.sample_rate = TWR_DAC_SAMPLE_RATE_16K;
49
50    twr_dac_async_config(TWR_DAC_DAC0, &dac_config);
51
52    twr_dac_async_run(TWR_DAC_DAC0);
53}