DAC Example with the Freedom Board

After working on an ADC example, a DAC one was missing. Julio E. Fajardo is a reader of this blog, and he was so kind to send me an example project for the FRDM-KL25Z. The example is able to produce produce different waveforms with the DAC. The project has two examples which they are enabled/disabled in main(). One example waveform the sawtooth:



And the other example waveform is a sinus:



The project sources are available on GitHub. It is using tables with the values so they are not computed at runtime. I tweaked the code a bit, and have defined the table as const array so it ends up in FLASH.

Many thanks again to Julio for that great example and screenshots!

Happy ADCing 🙂

25 thoughts on “DAC Example with the Freedom Board

  1. Do the Kinetis MCUs like the KL05 have the ability to gate the PWM signal with the DAC output in hardware? I would like to have the ability to have a PWM signal from my DAC while also setting the output voltage on the ON Duty times without the use of software interrupts or the need to put an external transistor on the DAC output with the PWM on the gate. Following me?


  2. Thanks to Erich for a GREAT blog (it’s my go-to resource for learning the KL25Z) and to Julio for the DAC example.

    My long-term goal is to create a simple triggerable music player using the KL25Z (mostly as a learning exercise). The idea is to read in from an SD card or SPI flash and then output via the DAC to a small amp.

    Whilst playing about with the DAC component I noticed that there is a D/A buffer option (along with DMA access). Does anyone know if the KL25Z supports this and could push me in the right direction as to implementing it? The datasheet I have doesn’t really cover this in any detail – is there a better datasheet anyone could point me to?

    All help gratefully received – if I get the buffer going I am happy to share an example project with you all.


    • Hi Stefan,
      many thanks for that positive feedback! 🙂
      I had this idea of such a music player in my mind for a long time too, but never had the time. I have not used that buffer option, but if I remember right, it allows to store conversion values in a fifo, so it can be retrieved later on. Maybe somebody else has more knowledge in this area?


      • Thanks for your reply Erich. I’m developing this VERY slowly as (a) I’m a hardware engineer really and (b) CodeWarrior has a steep learning curve for me.

        2 quick questions:

        1. Is there a good resource for learning CodeWarrior (except for this site). Like a reference book I can read? The built-in help is actually very good but it would be good to have a continuous reference I can access.

        2. Is there a “proper” datasheet for the MKL25Z128VLK4 that I can access? I can’t find one that details each hardware element individually and gives a register-by-register breakdown of what’s going on…

        Sorry if either of these have obvious answers – I’m drowning in new information at the minute!


  3. Thanks for your advice Erich – the reference manuals are about right – not sure I’m ready for a full-on ARM assault yet! In the end I managed to get DAC output driven by a Timer Interrupt which was OK-ish. I am happy to make that example available to people if you like – it’s a very straightforward derivative of Julio’s however.

    Now I’m having trouble getting everything together with SD reads, but that’s another story…


    • I had this idea too, but I have not time to work on this now. I greatly appreciate the example you will realize.


  4. Hi Erich,

    I tried you code but something seems off at the output:

    Sinewave has overshooting periodically, not smooth like yours;
    Sawtooth wave wasn’t generated at all, some gibberish was generated instead.

    Is my DAC configuration wrong? Can you go into more details on how to configure the DAC please?


      • Hey Erich,
        Yes I’m sure. I have posted the issue at the freescale community as well to try to get this issue sorted out. Its here in case you want to get updated:

        Best regards,


  5. Hi Erich! Is there any way to reach a higher frequency than 1Khz? I did everything this tutorial say, and i can’t get any freq higher than 1Khz.
    Maybe changing the setting on CPU clocks? If it’s possible, could you help me on that?

    Thank you in advice!


    • the maximum frequency depends on your sample frequency and how many samples (oversampling) you take. The higher the bus clock, the less time you need. So increase the bus clock to the maximum value on your board. Beyond that, you would need to use DMA. With DMA, you free up the processor moving the data.


      • Do you have a clear example of using DMA with DAC? I’ve tried to do that, even having read the help on component but no results 😦 I’ve not found the correct configuration to make it work.



  6. I had some troubles to get it work with the FRDM-K64F. However, there is an example from freescale for the K64F: freescale community thread/325233 (at the bottom of the page). Petr_H an employee of Freescale wrote: “We have reproduced the problem with DAC component and it is a defect related to the internal value computation which we will fix for next release. We are sorry for the inconvenience.”

    The example he posted on the webpage worked fine with the K64F.


  7. I downloaded the project today, In the current build of Code Warrior (10.6), the DA1:DAC_LDD component DMA is enabled in the Component Inspector. Project is not working with this setting. When the DMA is disabled project is working fine.

    And thanks for the project



  8. Hi Erich,
    The link to downlad the DAC project on GitHub doesn’t look to work anymore.
    Soonst, thanks for all your great examples and tutorials on this website!! Very usefull 😉


  9. How can I change the amplitude, frequency and offset base on a table of the wave?. I need to make a wave generator. For the amplitude I think that I can use conventional electronic for that but the rest of option I don’t know how.

    Please help me


What do you think?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.