EFM32 peripherals-DMA Timer + DMA + DAC

Source: Internet
Author: User

 

In some cases, for example, if you want to use DAC to output a sine wave, the simplest idea is to use a Timer for an interruption and assign a new value to the DAC during the interruption, and the DAC conversion is triggered. In this way, there will be some drawbacks. For example, in a complex system, there will be many interruptions, and Timer's interruption priority cannot always be the highest. Therefore, there may be problems in phase. In addition, interruption increases the power consumption of the entire system. Because it is necessary to constantly wake up the MCU to handle interruptions. The power consumption is closely related to the timing cycle of Timer. Finally, if you want to perform a frequent interruption, it will also increase the burden on the entire system. MCU will be busy handling this timer interrupt. The execution efficiency will decrease.

Given that we have a DMA controller in the chip, we can use DMA to implement this function.

Implementation result: the 1 ms Timer of Timer is used to trigger the DMA to send data to the DAC, so that the DAC outputs a specified waveform.

Hardware preparation: Use STK of TG and CH0 of DAC0 to output a waveform. GPIO port corresponds to PB11

Software preparation:

Timer: uses the default 14 MHz clock speed. Therefore, the TOP value for 1 ms is 14000. In addition, you need to use DMA to clear the timer overflow flag. Therefore, timerInit. dmaClrAct = true;

DMA: the source address is data in RAM and the target address is DAC. The trigger condition is chnlCfg. select = DMAREQ_TIMER1_UFOF; // indicates the overflow interruption of Timer1.

DAC: Default Configuration

The routine is as follows:

# Include <stdint. h>
# Include <stdbool. h>
# Include "efm32.h"
# Include "efm32_chip.h"
# Include "efm32_cmu.h"
# Include "efm32_gpio.h"
# Include "efm32_dac.h"
# Include "efm32_prs.h"
# Include "efm32_dma.h"
# Include "efm32_timer.h"

# Define DMA_CHANNEL_DAC 2
# Define SPITXSAMPLES 9

# Pragma data_alignment = 256
DMA_DESCRIPTOR_TypeDef dmaControlBlock [DMA_CHAN_COUNT * 2];

/* DMA init structure */
DMA_Init_TypeDef dmaInit;
/* DMA callback structure */
DMA_CB_TypeDef cb [DMA_CHAN_COUNT];

Unsigned short ucDACBuffer [SPITXSAMPLES] = {0,400,800,120 };

Void DAC_setup (void)
{
/* Use default settings */
DAC_Init_TypeDef init = DAC_INIT_DEFAULT;
Dac_initchannel_typedef initchannel = dac_initchannel_default;

/* Enable the DAC clock */
Cmu_clockenable (cmuclock_dac0, true );

/* Calculate the DAC clock prescaler value that will result in a DAC clock
* Close to 500 kHz. Second parameter is zero, if the hfperclk value is 0,
* Function will check what the current value actually is .*/
Init. prescale = dac_prescalec1c (500000, 0 );

/* Set reference voltage to 1.25 v */
Init. Reference = dacref2v5;

/* Initialize the DAC and DAC channel .*/
DAC_Init (DAC0, & init );
DAC_InitChannel (DAC0, & initChannel, 0 );
}

Void Timer_Setup (void)
{
CMU_ClockEnable (cmuClock_TIMER1, true );
/* Select TIMER0 parameters */
TIMER_Init_TypeDef timerInit =
{
. Enable = true,
. DebugRun = true,
. Prescale = timerPrescale1,
. ClkSel = timerClkSelHFPerClk,
. FallAction = timerInputActionNone,
. Riseaction = timerinputactionnone,
. Mode = timermodeup,
. Dmaclract = false,
. Quadmodex4 = false,
. Oneshot = false,
. Sync = false,
};

Timerinit. dmaclract = true;
/* Enable overflow interrupt */
// Timer_intenable (timer1, timer_if_of );

/* Enable timer0 interrupt vector in Objective C */
// NVIC_EnableIRQ (TIMER1_IRQn );

/* Set TIMER Top value */
TIMER_TopSet (TIMER1, 14000 );

/* Configure TIMER */
TIMER_Init (TIMER1, & timerInit );
}

Void DACTransferComplete (unsigned int channel, bool primary, void * user)
{
DMA_ActivateBasic (DMA_CHANNEL_DAC,
True,
False,
NULL,
NULL,
SPITXSAMPLES-1 );
}

Void DMA_Setup (void)
{
DMA_CfgChannel_TypeDef chnlCfg;
Dma_includescr_typedef descrCfg;

/* Setting up call-back function */
Cb [DMA_CHANNEL_DAC]. cbFunc = DACTransferComplete;
Cb [DMA_CHANNEL_DAC]. userPtr = NULL;

/* Setting up channel */
ChnlCfg. highPri = false;
ChnlCfg. enableInt = true;
ChnlCfg. select = DMAREQ_TIMER1_UFOF;
ChnlCfg. cb = & (cb [DMA_CHANNEL_DAC]);
Dma_channelchannel (DMA_CHANNEL_DAC, & chnlCfg );

/* Setting up channel descriptor */
DescrCfg. dstInc = dmaDataIncNone;
DescrCfg. srcInc = dmaDataInc2;
DescrCfg. size = dmaDataSize2;
DescrCfg. arbRate = dmaArbitrate1;
DescrCfg. hprot = 0;
Dma_includescr (DMA_CHANNEL_DAC, true, & descrCfg );

/* Starting transfer. Using basic since every transfer must be initiated
By the ADC .*/
Dma_activatebasic (dma_channel_dac,
True,
False,
(Void *) & (dac0-> ch0data ),
(Void *) & ucdacbuffer,
Spitxsamples-1 );
}

Void dmainit (void)
{
Cmu_clockenable (cmuclock_dma, true );
/* Initializing the DMA */
Dmainit. hprot = 0;
Dmainit. controlblock = dmacontrolblock;
Dma_init (& dmainit );
}

/*************************************** ***********************************//**
* @ Brief Main Function
**************************************** *************************************/
Int main (void)
{
/* Chip errata */
Chip_init ();

Cmu_clockenable (cmuclock_hfper, true );
Cmu_clockenable (cmuclock_gpio, true );

Dmainit ();
Dac_setup ();
/* Enable DAC channel 0, located on Pin pb11 */
Dac_enable (dac0, 0, true );

Timer_setup ();
Dma_setup ();

Unsigned short usdacvalue = 0 ;;
/* Infinite blink loop */
While (1)
{

}
}

Final effect:

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.