ST AN3252 Application note

AN3252

Application note

Building a wave generator using STM8L-DISCOVERY

Application overview

This application note provides a short description of how to use the STM8L-DISCOVERY as a basic wave generator for various waveforms such as sine, sinc (sin(x)/x), square, ramp and sawtooth signals.

More generally, this document describes STM8L on-chip DAC (digital-to-analog converter) peripheral settings when used with the DMA (direct memory access) controller.

This application does not require any additional hardware. Once the STM8L-DISCOVERY is powered up through a USB cable connected to the host PC, the application starts: a waveform is generated and a message is displayed on the LCD.

Reference documents

STM8L-DISCOVERY evaluation board user manual (UM0970)

STM8L-DISCOVERY software user manual Developing and debugging your STMLDISCOVERY application code (UM0991)

Application note Using the STM8L15x/STM8L162 DMA controller (AN3117)

The above documents are available at http://www.st.com.

September 2010

Doc ID 17767 Rev 1

1/21

www.st.com

Contents

AN3252

 

 

Contents

1

Application description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.1

Hardware required . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.2

STM8L-DISCOVERY hardware settings . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.3

Application schematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

5

 

1.4

Application principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

6

1.4.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 1.4.2 Waveform generation and frequency ranges . . . . . . . . . . . . . . . . . . . . . . 7 1.4.3 Known limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

1.5 Using the wave generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2

Software description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

12

 

2.1

STM8L peripherals used by the application . . . . . . . . . . . . . . . . . . . . . . .

12

 

2.2

Configuring the STM8L standard firmware library . . . . . . . . . . . . . . . . . .

13

 

2.3

Application software flowcharts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

13

2.3.1 Main application flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 2.3.2 Appli() loop routine description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3.3 Interrupt subroutine EXTI1 flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 2.3.4 Waveform_Change Mode() flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . 15 2.3.5 Frequency_Change Mode() flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . 17

3

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

19

4

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

20

2/21

Doc ID 17767 Rev 1

AN3252

List of tables

 

 

List of tables

Table 1. Waveform types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Table 2. Frequency ranges and bar display . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Table 3. Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

Doc ID 17767 Rev 1

3/21

List of figures

AN3252

 

 

List of figures

Figure 1.

Application block diagram. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 6

Figure 2.

Waveforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

. 9

Figure 3.

Waveform change mode state diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

10

Figure 4.

Frequency change mode state diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

11

Figure 5.

Main application flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

14

Figure 6.

EXTI1 interrupt subroutine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

Figure 7.

Waveform_Change Mode() flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

16

Figure 8.

Frequency_Change Mode() flowchart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

4/21

Doc ID 17767 Rev 1

AN3252

Application description

 

 

1 Application description

1.1Hardware required

This application uses the STM8L-DISCOVERY on-board LEDs (green LD3 and blue LD4), 6-digit/4-bar LCD glass display and USER push-button.

1.2STM8L-DISCOVERY hardware settings

The IDD jumper JP1 must be placed in the OFF position.

The solder bridge SB11 must be removed.

Solder bridge SB11 is located at the bottom of the board, close to the LCD device footprint. This operation disconnects PF0/DAC_OUT from the existing IDD measurement circuitry and sets DAC peripheral output for best performance.

1.3Application schematics

Figure 1 shows the application electrical schematics.

The generated signal is output on the microcontroller PF0/DAC_OUT pin. It is also available on a pin-head connector for external connection.

As shown, an oscilloscope or a frequency meter can be connected to that pin to monitor the various waveforms and to measure the signal frequency.

Doc ID 17767 Rev 1

5/21

ST AN3252 Application note

Application description

AN3252

 

 

Figure 1. Application block diagram

 

 

6$$

 

 

2

 

0%

 

 

 

0#

5SER PUSH BUTTON

2

34-, # 4

Ω

#

 

 

N&

,$

 

 

'REEN

0#

 

 

 

 

2

 

 

Ω

 

 

$!#?/54

 

,$

0&

 

,#$##ONTROLLER

 

"LUE

 

 

 

 

,#$#/-; =

 

 

,#$3%'; =

 

 

 

)$$$JUMPER /&&

 

 

 

 

 

 

3" REMOVED

 

 

 

 

"!2

 

 

,#$

AI

1.4Application principle

1.4.1Overview

This example uses the DAC peripheral to generate various waveforms on DAC output.

As the waveforms generated by the wave generator are periodic signals, only one period of each signal needs to be stored in the device Flash memory.

Each signal period is recorded in a table of 65 and 130 samples stored in the microcontroller Flash memory, depending on the required resolution. Each sample corresponds to a digital value that the DAC converts to output voltage by linear conversion.

These samples are sent cyclically from the Flash memory to the DAC using a DMA transaction and synchronized by a hardware trigger event provided by TIM4 timer. The DMA is set to Circular mode so that the operation is repeated indefinitely and that the produced signal is transmitted continuously. Refer to the application note Using the STM8L15x DMA controller (AN3117) for more details.

The wave generator is intended to approximately cover the audio range, i.e. 20 Hz to 20 kHz. This example uses various resolution tables (65 and 130 samples) and different TIM4 prescaler values to illustrate DAC output frequencies from 30 Hz to 22.5 kHz.

6/21

Doc ID 17767 Rev 1

AN3252

Application description

 

 

1.4.2Waveform generation and frequency ranges

The DAC output value is computed from all the digital samples that make up a waveform period using the following formula:

DAC output = VREF x (DOR / 4096), where VREF = 3 V and DOR is the 12-bit digital sample value.

For example, to generate one period of a sine wave, the table should contain x samples calculated using the sin(x) function and calibrated by applying an offset to shift the signal generation into the positive area. For a ramp signal, the table is calculated by using the 'f(x)=x' function, where x varies from 0 to 4095 (12 bits) and by selecting only n samples per period.

The wave generator can deliver 5 different waveforms in 2 resolution groups, as shown in

Table 1 below.

Depending on the chosen output frequency, select the resolution which best meets your needs.

Table 1.

Waveform types

 

 

Wave_type #

Waveform type

 

 

 

 

0

Ramp

 

 

 

 

1

Square

 

 

 

 

2

SineC (sin(x)/x)

 

 

 

 

3

Sine

 

 

 

 

4

Sawtooth

 

 

 

Converting a 130-sample table takes twice as long as converting a 65-sample table. This means that waveforms sampled with 65 values have twice the frequency of waveforms sampled with 130 values (see ranges 1 and 3 in Table 2). Furthermore, TIM4 prescaler values can be modified to create 2 (or more) additional ranges (ranges 2 and 4).

A dedicated 4-bar display shows the current frequency range and resolution.

The corresponding bars display on the right side of the LCD glass (see Figure 1 for more details).

Doc ID 17767 Rev 1

7/21

Loading...
+ 14 hidden pages