DAC KL25Z

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

DAC KL25Z

3,482 Views
amirche
Contributor II

How can i generate simple sine wave with kl25z board on PTE30 pin by using kinetis design studio?

Tags (1)
0 Kudos
4 Replies

2,707 Views
Robin_Shen
NXP TechSupport
NXP TechSupport

Hi Amir Che,

You can refer this "DAC Example with the Freedom Board".

That project was created by CodeWarrior10.x with Processor Expert.

I think the code and configurations can be used in your project with KDS3.2.

Best Regards,

Robin

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
---------------------------------------------------------------------------------------------------------------------

0 Kudos

2,707 Views
amirche
Contributor II

Hi Robin,

Thanks for that advice. I will look foward to it and i will give you the

feedback as well as i manage to understand the code and try it.

0 Kudos

2,707 Views
mjbcswitzerland
Specialist V

Hi Amir

I think that you need to beware that the referenced DAC example looks to use 100% of the processor's power to generate a signal and also has a fixed frequency that can't really be controlled during operation. This may be OK for a school project to get to know the basic DAC's operation but will be (as also discussed at the link a little) of limited use in real developments.

Below is the code that I use, in order to show that it is very simple to control powerful DMA based operation that will allow the output to also continue running when the processor itself is stopped by the debugger, with almost no CPU performance impact. This code will in fact run on ANY Kinetis (K, KL, KV, KM, etc.) part as long as it has a DMA controller in it.

The only thing to be aware of if you use the KL25 is that the mask 2N97F has an errata with its PIT triggered DMA and the generated frequency will in fact be 2x the expected rate, but it otherwise still works....

Regards

Mark

#define LENGTH_OF_SINE_BUFFER    128                                     // number of points in the sine wave period (frequency = sample frequency / LENGTH_OF_SINE_BUFFER) 
                                                                         // LENGTH_OF_SINE_BUFFER shoudl be 64, 128, 256 etc. for automatic KL DMA circular buffer operation

// PIT configuration
//
static void fnConfigurePIT(void)
{
    static unsigned short *ptrSineBuffer = 0;
    DAC_SETUP dac_setup;
    PIT_SETUP pit_setup;                                                 // interrupt configuration parameters
    pit_setup.int_type = PIT_INTERRUPT;
    pit_setup.int_priority = PIT0_INTERRUPT_PRIORITY;                    // not used
    pit_setup.count_delay = PIT_US_DELAY(100);                           // 10kHz sample frequency
    pit_setup.mode = (PIT_PERIODIC);                                     // periodic DMA trigger
    pit_setup.int_handler = 0;                                           // no interrupt since the PIT will be used for triggering DMA
    ptrSineBuffer = uMallocAlign((LENGTH_OF_SINE_BUFFER * sizeof(unsigned short)), (LENGTH_OF_SINE_BUFFER * sizeof(unsigned short))); // get buffer aligned for KL DMA circular buffer operation
    // Define the waveform to be generated
    //
    {
        #define PI           (double)3.14159265
        #define ANGLE_STEP   (double)((double)(2 * PI)/(double)LENGTH_OF_SINE_BUFFER);
        int i;
        double angle = 0;
        for (i = 0; i < LENGTH_OF_SINE_BUFFER; i++) {                    // prepare a sine wave
            ptrSineBuffer[i] = (unsigned short)((sin(angle) * 0x7ff) + (0xfff/2)); // one period of sine wave with half-supply DC value
            angle += ANGLE_STEP;
        }
    }
    pit_setup.ucPIT = 0;                                                 // use PIT0
    fnConfigureInterrupt((void *)&pit_setup);                            // configure and start PIT
    // Configure DAC to use PIT0 triggered DMA
    //
    dac_setup.int_type = DAC_INTERRUPT;
    dac_setup.int_dac_controller = 0;                                    // DAC 0
    dac_setup.int_handler = 0;                                           // no interrupt used
    dac_setup.int_priority = 15;                                         // lowest priority (not used in this case)
    dac_setup.dac_mode = (DAC_CONFIGURE | DAC_REF_VDDA | DAC_NON_BUFFERED_MODE | DAC_FULL_BUFFER_DMA | DAC_ENABLE | DAC_BUFFER_DMA_START); // configure the DAC to use VDDA as reference voltage in non-buffered mode (using DMA)
    dac_setup.ptrDAC_Buffer = ptrSineBuffer;                             // the input buffer to be used
    dac_setup.ulDAC_buffer_length = (LENGTH_OF_SINE_BUFFER * sizeof(unsigned short)); // the number of bytes in the buffer
    dac_setup.ucDmaChannel = 0;                                          // DMA channel 0 used (PIT0 can onyl trigger DMA channel 0)
    dac_setup.ucDmaTriggerSource = DMAMUX0_DMA0_CHCFG_SOURCE_PIT0;       // PIT0 triggers the channel mux
    dac_setup.dac_mode |= DAC_HW_TRIGGER_MODE;                           // use HW trigger mode rather than SW triggered mode (this requires PIT to trigger it)
    fnConfigureInterrupt((void *)&dac_setup);                            // configure DAC and astart generation of repetitive output signal
}
0 Kudos

2,707 Views
mjbcswitzerland
Specialist V

Hi

See page 12 of http://www.utasker.com/docs/uTasker/uTaskerADC.pdf for details of generating a repetitive signal at the DAC output using DMA.


There is a binary that can be loaded to the FRDM-KL25Z that generates a 156Hz sine wave as reference at http://www.utasker.com/kinetis/FRDM-KL25Z.html#SIN
Note also the PWM outputs can be used in a similar fashion to generate repetitive analogue signals.

Full code available in the uTasker project for KDS, CW, IAR, Keil, Green Hills, Rowley, Atollic, CooCox, S32, GCC, VS.

Regards

Mark

0 Kudos