ST AN4058 Application note

AN4058

Application note

Audio and waveform generation using the DAC in STM32F0xx microcontroller families

Introduction

This application note gives examples for generating audio output signals using the Digital to Analog Converter (DAC) peripheral embedded in the STM32F0xx microcontroller family.

A digital to analog converter, DAC, is a device that has the opposite function to an analog to digital converter, it converts a digital word to a corresponding analog voltage.

The STM32 DAC module is a 12-bit word converter, with one output channel for supporting mono audio.

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, DAC is used to generate a sine wavefom.

In the second example, the DAC is used to generate audio from .WAV files.

May 2012

Doc ID 022846 Rev 1

1/18

www.st.com

Contents

AN4058

 

 

Contents

1

DAC main features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.1

Data format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.2

Dedicated timers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.3

DMA capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

 

1.4

DMA underrun error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

 

1.5

Buffered output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

7

2

Application examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

9

 

2.1 Using the DAC to generate a sine waveform . . . . . . . . . . . . . . . . . . . . . . .

9

2.1.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.1.2 Digital Sine waveform pattern preparation . . . . . . . . . . . . . . . . . . . . . . . . 9 2.1.3 Fixing the sine wave frequency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.2 Using the DAC to implement an audio wave player . . . . . . . . . . . . . . . . . 12

2.2.1 Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12 2.2.2 Audio wave file specifications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.2.3 .WAV file format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

2.3 Audio wave player implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

3

Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

4

Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

17

2/18

Doc ID 022846 Rev 1

AN4058

List of tables

 

 

List of tables

Table 1. Digital and analog sample values of the Sine wave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Table 2. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Doc ID 022846 Rev 1

3/18

List of figures

AN4058

 

 

List of figures

Figure 1. DAC data format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 2. STM32F0xx DAC trigger channel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Figure 3. DAC interaction without DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Figure 4. DAC interaction with DMA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Figure 5. Non buffered channel voltage (with and without load) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 6. Buffered channel voltage (with and without load) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Figure 7. Sine wave model samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Figure 8. Sine wave generated with ns = 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 9. Sine wave generated with ns = 255 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Figure 10. Flow of data from MicroSD Flash memory to external speakers . . . . . . . . . . . . . . . . . . . . 12 Figure 11. Wave Player flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Figure 12. CPU and DMA activities during wave playing process . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

4/18

Doc ID 022846 Rev 1

ST AN4058 Application note

AN4058

DAC main features

 

 

1 DAC main features

1.1Data format

The DAC accepts data in 3 integer formats: 8-bit, 12-bit right aligned and 12-bit left aligned. A 12-bit value can range from 0x000 to 0xFFF, with 0x000 being the lowest and 0xFFF being the highest value.

Figure 1. DAC data format

ai18300

1.2Dedicated timers

In addition to the software and External triggers, the DAC conversion can be triggered by different timers.

TIM6 is a basic timer and is basically designed for DAC triggering.

Each time a DAC interface detects a rising edge on the selected Timer Trigger Output (TIMx_TRGO), the last data stored in the DAC_DHRx register is transferred to the DAC_DORx register.

Figure 2. STM32F0xx DAC trigger channel

TSELx[2:0] bits

SWTRIGx

TIM6_TRGO

TIM3_TRGO

TIM15_TRGO

TIM2_TRGO

external trigger

Trigger selector x

DAC Channel 1 Trigger

MS30315V1

Doc ID 022846 Rev 1

5/18

DAC main features

AN4058

 

 

1.3DMA capabilities

The STM32 microcontrollers have a DMA module with multiple channels. The DAC channel is connected to an independent DMA channel. In the case of STM32F0xx Microcontrollers, the DAC channel is connected to the DMA channel 3.

When DMA is not utilized, the CPU is used to provide DAC with the pattern waveform. Generally the waveform is saved in a memory (RAM), and the CPU is in charge of transferring the data from RAM to the DAC.

Figure 3. DAC interaction without DMA

CPU

RAM

(Pattern Table 1)

(Pattern Table 2)

DAC

DAC Triggers

Channel 1

 

Output

MS30316V1

When using the DMA, the overall performance of the system is increased by freeing up the core. This is because data is moved from memory to DAC by DMA, without needing any actions by the CPU. This keeps CPU resources free for other operations.

6/18

Doc ID 022846 Rev 1

Loading...
+ 12 hidden pages