Library 07- DA Converter on STM32F4

Digital to Analog Converter has inverse function as ADC. It converts digital value in microcontroller to analog value.

Library

Features

  • Output analog value from STM32F4x

Dependencies

  • CMSIS
    • STM32F4xx
    • STM32F4xx GPIO
    • STM32F4xx DAC
  • TM
    • TM GPIO
    • defines.h

STM32F4 has 1 DAC with 2 channels output. Table below shows pins for DAC.

DAC pins
DAC Channel ALIAS Pin APB
DAC_Channel_0 TM_DAC1 PA4 1
DAC_Channel_1 TM_DAC2 PA5 1

Alias is used in my library for easier use in library.

Example

Project is available on Github Github, download library below.

Icon
TM STM32F4 DAC Library

DAC library to convert digital value to analog voltage

tilz0R

Owner of this site. Also electronic enthusiasts, web developer, 3D printer fan, handball player and more. Big fan of STM32F4 devices. In anticipation of the new Discovery board for STM32F7 lines.

You may also like...

Read before commenting!

Before you make a new comment, make sure you agree with things listed below:

  • - Read post to make sure if it is already posted what you are asking for,
  • - Make sure you have the latest version of libraries used in your project,
  • - Make a clean and grammatically correct written message,
  • - Report as many details as possible, including what have you done so far,
  • - Do NOT post any code here. Use Pastebin,
  • - Do NOT post any error codes here. Use Pastebin,
  • - Specify STM32Fxxx family and used Discovery/EVAL/Nucleo or custom made board,
  • - Make sure your clock is set correct for PLL,
  • - If you are using my HAL drivers, please check this post how to start.
Comment will be deleted on breaking these rules without notification!
  • Michele

    I tested the outputs of the DAC of my board (STM32F429-Discovery), with the code in this library.

    The output voltages of the two channels are not correct. The power supply of the board has been done with USB, the output voltage are measured with a multimeter.

    The output voltage of the board on pins PA4 and PA5 are:
    – with set of 4096 –> 2.900 V, and not 3.3 V
    – with set of 2048 –> 1.470 V (PA4) and 1.463 (PA5)

    Can be the problem associated to the usb power supply?
    On VDD pin I have 2.939 V.

    Thanks

    • Yeah, output voltage from STM @ 3V3 power supply is about 3V output, 2.9 is just real value.
      Using internal DAC for something serious is bad idea for my opinion.

  • Michele

    Thanks for the answer.

    I want to have a very precise outputs from my board, but also very fast outputs for acoustic application like active noise cancellation or similar projects. These my application are based mainly on FIR and IIR filter. In the past I used the DACs channel of an industrial board (like keithley Adwin Light), but I want to test my code in the discovery board to verify the performance. I’m sorry but I am not so expert in this type of board. Which channels do you think to be the most suitable?

    • I’m not sure if I understand you OK. Channels? STM has 2 DAC channels output.

      • Michele

        Hi Majerle, I’m sorry I would like to know if it is possible to adjust the full scale of the dac channels in order to be sure that at the max output (4096) I will obtain a value that I know.
        The same issue for the adc.
        Thank u very much for your support.
        Very good work your libraries! Congratulations!

        • I would say that this is possible if you have really stable voltage on VDDA pin.
          But I’m not sure. You should read errata and application notes about that.

  • Aaditya Tiwari

    I am testing this program and it is showing stm32f4xx_rcc.h not found and when I included this one somehow still it is giving lots of error such as uint32_t, uint8_t, STATIC_INLINE etc.. not defined. whats wrong with my project. I am building it with Keil v5

    some more errors:
    .Objectsdac_ramp.axf: Error: L6218E: Undefined symbol DAC_Cmd (referred from tm_stm32f4_dac.o).
    .Objectsdac_ramp.axf: Error: L6218E: Undefined symbol DAC_Init (referred from tm_stm32f4_dac.o).
    .Objectsdac_ramp.axf: Error: L6218E: Undefined symbol DAC_SetChannel1Data (referred from tm_stm32f4_dac.o).
    .Objectsdac_ramp.axf: Error: L6218E: Undefined symbol DAC_SetChannel2Data (referred from tm_stm32f4_dac.o).
    .Objectsdac_ramp.axf: Error: L6218E: Undefined symbol GPIO_Init (referred from tm_stm32f4_dac.o).
    .Objectsdac_ramp.axf: Error: L6218E: Undefined symbol RCC_AHB1PeriphClockCmd (referred from tm_stm32f4_dac.o).
    .Objectsdac_ramp.axf: Error: L6218E: Undefined symbol RCC_APB1PeriphClockCmd (referred from tm_stm32f4_dac.o).

    • You don’t have included standard peripheral drivers.

      • Aaditya Tiwari

        Thanx for a quick reply but how can i include it?

        • For first time, you can download my repository from Github and open project for DA directly from there.
          You will see how drivers are included.

          • Aaditya Tiwari

            thanx

  • Pingback: ARM: DAC | Electr√≥nica en casa, en el sonido e industrial()

  • Nguyen Master

    Hi Tilen, i tried this code but it didn’t work.
    TM_DAC_SetValue(TM_DAC1, dac1);
    dac1++;
    But when i change variable dac1 by a number, it works.
    TM_DAC_SetValue(TM_DAC1, 1234);
    or when i add some more, it works too.

    TM_DAC_SetValue(TM_DAC1, dac1+1);
    dac1++;
    I dont know why, can you explain for me please.
    Thank you.
    Hoang

    • Show more code how you use it. In your case, you increase variable after you set value.

  • Zack

    Any clue to why when I try to implement the DAC, the pin is pulled up to vdd? should it be biased at vdd/2? I get very little output, about 10 mV.

  • palaima

    Hey thanks for this example project. I found it really helpful. I’m getting signal input and output now with the adc and dac (just basic dc values at this point). I was wondering if you had any pointers about how I could go about setting up a sample rate so I could begin doing audio processing.

    • Use timer for triggering frequency for sample and use DMA to transfer data from ADC to you memory.
      These options are the best.