February 15, 2016

Pattern generator tool for DAC in embedded applications

I wrote this small app intended to generate waveforms patterns used in many Analog-to-Digital converters. It can generate sine, triangle, sawtooth and square patterns for DAC with a resolution between 8-bit and 24-bit. It is also possible to set pattern's amplitude by providing the DAC reference voltage and pattern's extremums.

A demonstration example using circular DMA transfer is included, based on the STM32Discovery board and its integrated 12-bit DAC.

The frequency of the generated signal depends on the speed at which the pattern is read, and of the number of samples the pattern is made of. In the case of the STM32Discovery, you can use this formula to set the desired frequency where $ftimer$ is the clock frequency set for the DMA transfer, $Ptimer$ the timer's period and $N$ the number of samples in the pattern :

$$f_{Pattern}=\frac{ftimer}{N \times Ptimer}$$

Here are the generated patterns for sine, triangle and sawtooth functions with 64 samples and a 12-bit resolution :

uint32_t SineWavePattern[64] = {2048, 2248, 2447, 2642, 2831, 3013,
3185,3346,3495,3630, 3750, 3853, 3939, 4007, 4056, 4085, 4095, 4085, 4056, 4007,
3939, 3853, 3750, 3630,3495, 3346, 3185, 3013, 2831, 2642, 2447, 2248, 2048, 1847,
1648, 1453, 1264, 1082, 910, 749, 600, 465, 345, 242, 156, 88, 39, 10, 0, 10, 39,
88, 156, 242, 345, 465, 600, 749, 910, 1082, 1264, 1453, 1648, 1847};

uint32_t TriangleWavePattern[64] = {2048, 2175, 2303, 2431, 2559, 2687, 2815, 2943,
3071, 3199, 3327, 3455, 3583, 3711, 3839, 3967, 4095, 3967, 3839, 3711, 3583, 3455,
3327, 3199, 3071, 2943, 2815, 2687, 2559, 2431, 2303, 2175, 2048, 1920, 1792, 1664,
1536, 1408, 1280, 1152, 1024, 896, 768, 640, 512, 384, 256, 128, 0, 128, 256, 384,
512, 640, 768, 896, 1024, 1152, 1280, 1408, 1536, 1664, 1792, 1920};

uint32_t SawtoothWavePattern[64] = {2048, 2111, 2175, 2239, 2303, 2367, 2431, 2495,
2559, 2623, 2687, 2751, 2815, 2879, 2943, 3007, 3071, 3135, 3199, 3263, 3327, 3391,
3455, 3519, 3583, 3647, 3711, 3775, 3839, 3903, 3967, 4031, 0, 64, 128, 192, 256, 320,
384, 448, 512, 576, 640, 704, 768, 832, 896, 960, 1024, 1088, 1152, 1216, 1280, 1344,
1408, 1472, 1536, 1600, 1664, 1728, 1792, 1856, 1920, 1984};


You can get this tool here, and the example (STM32Discovery board + STM32CubeMx) generating a sine wave with 64 samples at 1000Hz is also available on this repository.