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:
- Square
- Sawtooth
- Triangle
- 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
- TM TIMER PROPERTIES
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
/** * Signal result enumeration * * Parameters: * - TM_DAC_SIGNAL_Result_Ok: * Everything OK * - TM_DAC_SIGNAL_Result_Error: * An error occured * - TM_DAC_SIGNAL_Result_TimerNotValid: * Used timer for DMA and DAC request is not valid */ typedef enum { TM_DAC_SIGNAL_Result_Ok = 0x00, TM_DAC_SIGNAL_Result_Error, TM_DAC_SIGNAL_Result_TimerNotValid } TM_DAC_SIGNAL_Result_t; /** * Signal enumeration * * Parameters: * - TM_DAC_SIGNAL_Signal_Sinus: * Select sinus signal on DAC output * - TM_DAC_SIGNAL_Signal_Triangle: * Select triangle signal on DAC output * - TM_DAC_SIGNAL_Signal_Sawtooth: * Select sawtooth signal on DAC output * - TM_DAC_SIGNAL_Signal_Square: * Use square signal on DAC output */ typedef enum { TM_DAC_SIGNAL_Signal_Sinus = 0x00, TM_DAC_SIGNAL_Signal_Triangle = 0x01, TM_DAC_SIGNAL_Signal_Sawtooth = 0x02, TM_DAC_SIGNAL_Signal_Square = 0x03 } TM_DAC_SIGNAL_Signal_t; /** * DAC used for signal * * Parameters: * - TM_DAC1: * Use DAC1 for specific settings * - TM_DAC2: * Use DAC2 for specific settings */ typedef enum { TM_DAC1 = 0x00, TM_DAC2 = 0x01 } TM_DAC_SIGNAL_Channel_t; /** * Initialize DAC output pin and prepare to work * * Parameters: * - TM_DAC_SIGNAL_Channel_t DACx: * DAC channel you will use. Parameter is member of TM_DAC_SIGNAL_Channel_t enumeration * - TIM_TypeDef* TIMx: * Timer used for DMA and DAC output generation * * Member of TM_DAC_SIGNAL_Result_t is returned */ extern TM_DAC_SIGNAL_Result_t TM_DAC_SIGNAL_Init(TM_DAC_SIGNAL_Channel_t DACx, TIM_TypeDef* TIMx); /** * Set output signal type with specific frequency * * Parameters: * - TM_DAC_SIGNAL_Channel_t DACx: * DAC channel you will use. Parameter is member of TM_DAC_SIGNAL_Channel_t enumeration * - TM_DAC_SIGNAL_Signal_t signal_type: * Signal type you will set for specific dac channel. Parameter is member of TM_DAC_SIGNAL_Signal_t enumeration * - uint32_t frequency: * Signal's frequency * * Member of TM_DAC_SIGNAL_Result_t is returned */ extern TM_DAC_SIGNAL_Result_t TM_DAC_SIGNAL_SetSignal(TM_DAC_SIGNAL_Channel_t DACx, TM_DAC_SIGNAL_Signal_t signal_type, uint32_t frequency); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
/** * Keil project for DAC with DMA signal feature * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_dac_signal.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_disco.h" int main(void) { /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize LEDs on board */ TM_DISCO_LedInit(); /* Initialize DAC1, use TIM4 for signal generation */ TM_DAC_SIGNAL_Init(TM_DAC1, TIM4); /* Initialize DAC2, use TIM5 for signal generation */ TM_DAC_SIGNAL_Init(TM_DAC2, TIM5); /* Output predefined triangle signal with frequency of 5kHz */ TM_DAC_SIGNAL_SetSignal(TM_DAC1, TM_DAC_SIGNAL_Signal_Triangle, 5000); /* Output predefined square signal with frequency of 10kHz */ TM_DAC_SIGNAL_SetSignal(TM_DAC2, TM_DAC_SIGNAL_Signal_Square, 10000); while (1) { } } |
Project is available on Github, download library below.
Recent comments