AN3126
Application note
Audio and waveform generation using the DAC in
STM32 microcontrollers
Introduction
This application note provides some examples for generating audio waveforms using the
Digital to Analog Converter (DAC) peripheral embedded in the microcontrollers of the
STM32Fx and STM32Lx series.
This document applies to products listed in
Table 1,
and should be read in connection with
application note AN4566 “Extending the DAC performance of STM32 microcontrollers”.
A digital to analog converter, DAC, is a device that has the opposite function to that of an
analog to digital converter, i.e. it converts a digital word to a corresponding analog voltage.
The STM32 DAC module is a 12-bit word converter, with up to three output channels to
support audio functions.
The DAC can be used in many audio applications such as security alarms, Bluetooth
headsets, talking toys, answering machines, man-machine interfaces, and low-cost music
players
STM32 DAC can also be used for many other analog purposes, such as analog waveform
generation and control engineering.
The application note is organized in two main sections:
•
Section 1
describes the main features of the STM32 DAC module.
•
Section 2
presents two examples.
– In the first example, the DAC is used to generate a sine wavefom.
– In the second example, the DAC is used to generate audio from .WAV files.
Table 1. Applicable products
Type
Product series
STM32F0
STM32F1
STM32F2
STM32F3
Microcontrollers
STM32F4
STM32F7
STM32L0
STM32L1
STM32L4
April 2015
Doc ID16895 Rev 2
1/24
www.st.com
1
Contents
AN3126
Contents
1
DAC main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1
1.2
1.3
1.4
1.5
1.6
1.7
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Data format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Dual channel mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Dedicated timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
DMA capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
DMA underrun error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
White noise generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.7.1
1.7.2
Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Typical applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Typical applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.8
Triangular wave generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11
1.8.1
1.8.2
1.9
Buffered output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2
Application examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1
Using the DAC to generate a sine waveform . . . . . . . . . . . . . . . . . . . . . . 15
2.1.1
2.1.2
2.1.3
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Digital Sine waveform pattern preparation . . . . . . . . . . . . . . . . . . . . . . . 15
Fixing the sine wave frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Audio wave file specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
.WAV file format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.2
Using the DAC to implement an audio wave player . . . . . . . . . . . . . . . . . 18
2.2.1
2.2.2
2.2.3
2.3
Audio wave player implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
3
4
Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2/24
Doc ID16895 Rev 2
AN3126
List of tables
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Applicable products . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
DAC configurations for STM32 microcontrollers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Preprogrammable triangular waveform amplitude values. . . . . . . . . . . . . . . . . . . . . . . . . . 12
Digital and analog sample values of the sine wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Doc ID16895 Rev 2
3/24
3
List of figures
AN3126
List of figures
Figure 1.
Figure 2.
Figure 3.
Figure 4.
Figure 5.
Figure 6.
Figure 7.
Figure 8.
Figure 9.
Figure 10.
Figure 11.
Figure 12.
Figure 13.
Figure 14.
Figure 15.
Figure 16.
Figure 17.
DAC data format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
STM32F100x DAC trigger channels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
DAC interaction without DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
DAC interaction with DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Pseudo random code generator embedded in the DAC . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Noise waveform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Noise waveform with changeable offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Triangular waveform . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Triangular waveform with changeable offset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Non buffered channel voltage (with and without load) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Buffered channel voltage (with and without load) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Sine wave model samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Sine wave generated with ns = 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Sine wave generated with ns = 255 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Flow of data from MicroSD Flash memory to external speakers . . . . . . . . . . . . . . . . . . . . 18
Wave Player flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
CPU and DMA activities during wave playing process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4/24
Doc ID16895 Rev 2
AN3126
DAC main features
1
1.1
DAC main features
Overview
STM32 microcontrollers integrate DAC with different configurations and features:
•
•
•
•
•
1 to 3 DAC output channels
Noise waveform generation
Triangular waveform generation
DMA under run flag
Dedicated analog clock
Table 2
summarizes the different STM32 DAC configuration.
Table 2. DAC configurations for STM32 microcontrollers
Series
Product RPN
STM32F030xx
STM32F031xx
STM32F038xx
STM32F042xx
STM32F048xx
STM32F070xx
F0
STM32F051xx
STM32F058xx
STM32F071xx
STM32F072xx
STM32F078xx
STM32F091xx
STM32F098xx
STM32F101x4/6/8B
STM32F102xx
STM32F103x4/6/8B
F1
STM32F100xx
STM32F101xC/D/E/F/G
STM32F103xC/D/E/F/G
STM32F105xx
STM32F107xx
STM32F2xxxx
DAC
outputs
White noise
generator
Triangular
wave
generator
DMA
capability
DMA
underrun
error
0
-
-
-
-
1
No
No
Yes
No
2
Yes
Yes
Yes
Yes
0
-
-
-
-
2
Yes
Yes
Yes
Yes
F2
2
Yes
Yes
Yes
Yes
Doc ID16895 Rev 2
5/24
23