How To: Digital/Analog Converter
Core Module contains two analog outputs: DAC0
and DAC1
. These are real analog outputs, not just PWM outputs.
Both channels can run completely separately with different sample rates.
References
- DAC SDK Module
- GitHub Repository Example
info
In this example, there’s a sine wave lookup table, you need to prepare the output buffer first.
The sampling is set on TWR_DAC_SAMPLE_RATE_16K
. This together generates 125 Hz sine-wave on DAC0 output.
Sine Wave Generator Code Example
#include <application.h>
const uint8_t sine_wave[] = {
0x80, 0x86, 0x8C, 0x93,
0x99, 0x9F, 0xA5, 0xAB,
0xB1, 0xB6, 0xBC, 0xC1,
0xC7, 0xCC, 0xD1, 0xD5,
0xDA, 0xDE, 0xE2, 0xE6,
0xEA, 0xED, 0xF0, 0xF3,
0xF5, 0xF8, 0xFA, 0xFB,
0xFD, 0xFE, 0xFE, 0xFF,
0xFF, 0xFF, 0xFE, 0xFE,
0xFD, 0xFB, 0xFA, 0xF8,
0xF5, 0xF3, 0xF0, 0xED,
0xEA, 0xE6, 0xE2, 0xDE,
0xDA, 0xD5, 0xD1, 0xCC,
0xC7, 0xC1, 0xBC, 0xB6,
0xB1, 0xAB, 0xA5, 0x9F,
0x99, 0x93, 0x8C, 0x86,
0x80, 0x7A, 0x74, 0x6D,
0x67, 0x61, 0x5B, 0x55,
0x4F, 0x4A, 0x44, 0x3F,
0x39, 0x34, 0x2F, 0x2B,
0x26, 0x22, 0x1E, 0x1A,
0x16, 0x13, 0x10, 0x0D,
0x0B, 0x08, 0x06, 0x05,
0x03, 0x02, 0x02, 0x01,
0x01, 0x01, 0x02, 0x02,
0x03, 0x05, 0x06, 0x08,
0x0B, 0x0D, 0x10, 0x13,
0x16, 0x1A, 0x1E, 0x22,
0x26, 0x2B, 0x2F, 0x34,
0x39, 0x3F, 0x44, 0x4A,
0x4F, 0x55, 0x5B, 0x61,
0x67, 0x6D, 0x74, 0x7A
};
void application_init(void)
{
twr_dac_init(TWR_DAC_DAC0);
twr_dac_config_t dac_config;
dac_config.buffer = (uint8_t*)sine_wave;
dac_config.length = sizeof(sine_wave);
dac_config.data_size = TWR_DAC_DATA_SIZE_8;
dac_config.mode = TWR_DAC_MODE_CIRCULAR;
dac_config.sample_rate = TWR_DAC_SAMPLE_RATE_16K;
twr_dac_async_config(TWR_DAC_DAC0, &dac_config);
twr_dac_async_run(TWR_DAC_DAC0);
}