Library 36- DAC Signal generator for STM32F4

For FFT project purpose, I needed simple, really simple signal generator. I don’t have separate device at home, so I made one with STM32F4.

DAC Signal library uses timer for output generation and DMA for transferring data from memory to DAC peripheral. So this library does not use processor for controlling. Everything is behind the scenes.

You can use 4 different signal waves:

  1. Square
  2. Sawtooth
  3. Triangle
  4. Sinus

They are simple, predefined signals. You can adjust signal frequency for specific wave. I tested some frequencies, and sinus worked just well at about 100kHz, but square works great to about 50kHz. Square wave has 50% duty cycle. If you need variable square wave duty cycle, then you should look at my PWM library or PWM tutorial.

This library is not a real signal generator, sinus signal is not going to negative. Signals have fake ground at the middle of DAC max value, so 2047 is fake ground, and sinus is going from 0 to 4095. If you want “negative” effect, then you should add a capacitor in series to the output, this will allow only AC component to go through. Capacitor varies between your signal frequency, but about 680nF should be just OK.

Library

Features

  • Supports 4 different simple predefined waves
  • Selectable signal frequency
  • Supports 2 DAC channels
  • Timer driven DMA
    • TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8 can be used

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx RCC
    • STM32F4xx GPIO
    • STM32F4xx DAC
    • STM32F4xx TIM
    • STM32F4xx DMA
  • TM
    • TM TIMER PROPERTIES
    • TM GPIO
    • defines.h

Pinout is simple. You only need pins for DAC outputs.

DACx STM32F4 Description
DAC1 PA4 DAC Channel 1
DAC2 PA5 DAC Channel 2

STM32F401 series does not have DAC output, so you can’t use this library there. But others should have it. Pins are fixed, analog, can not be changed.

Functions and enumerations

Example

Example below produces:

  • Triangle wave on DAC Channel 1, pin PA4, with signal frequency 5 kHz
  • Square wave on DAC Channel 2, pin PA5, with signal frequency 10kHz
  • Oscilloscope result with cursor measurements
Triangle wave 5kHz

Triangle wave 5kHz

Square wave 10kHz

Square wave 10kHz



Project is available on Github, download library below.

tilz0R

Owner of this site. Application engineer, currently employed by STMicroelectronics. Exploring latest technologies and owner of different libraries posted on Github.

You may also like...