cancel
Showing results for 
Search instead for 
Did you mean: 

DAC-DMA problem

marcogrossi89
Associate II
Posted on January 04, 2016 at 10:58

I am using a STM32L152RE Nucleo Board and programming with EmBitz using the Standard Peripheral Library.

I am trying to generate a ramp signal using DAC channel 1 and DMA Channel 2 using Timer2 as trigger. The code does not work. However if I change to DAC channel 2, DMA channel 3 (changing DAC_DHR12R1_Address      0x40007408 to DAC_DHR12R2_Address      0x40007414) the code works. I really do not understand.

Following is the code:

#include ''stm32l1xx_conf.h''

#include ''math.h''

#define DAC_DHR12R1_Address      0x40007408

#define WAVEPOINTS 200

void GPIO_initialization(void);

void DMA_DAC_initialize(void);

void TIM2_initialize(void);

uint16_t i;

float j;

uint16_t waveform[WAVEPOINTS];

int main(void)

{

    for(i = 0; i < WAVEPOINTS; i++)

    {

       waveform[i]= (uint16_t)(807 + 12*i);

    }

    GPIO_initialization();

    DMA_DAC_initialize();

    TIM2_initialize();

  while(1)

  {

  }

}

void GPIO_initialization(void)

{

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  GPIO_InitTypeDef gpioStructure;

     gpioStructure.GPIO_Pin = GPIO_Pin_4;

     gpioStructure.GPIO_Mode = GPIO_Mode_AN;

     gpioStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

     gpioStructure.GPIO_Speed = GPIO_Speed_2MHz;

     GPIO_Init(GPIOA, &gpioStructure);

}

void DMA_DAC_initialize(void)

{

   RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);

   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);

   DAC_InitTypeDef DAC_Structure;

DAC_Structure.DAC_Trigger = DAC_Trigger_T2_TRGO;

DAC_Structure.DAC_WaveGeneration = DAC_WaveGeneration_None;

DAC_Structure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;

DAC_Init(DAC_Channel_1, &DAC_Structure);

DMA_InitTypeDef DMA_Structure;

DMA_DeInit(DMA1_Channel2);

    DMA_Structure.DMA_PeripheralBaseAddr = DAC_DHR12R1_Address;

    DMA_Structure.DMA_MemoryBaseAddr = (uint32_t) &waveform;

    DMA_Structure.DMA_DIR = DMA_DIR_PeripheralDST;

    DMA_Structure.DMA_BufferSize = WAVEPOINTS;

    DMA_Structure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

    DMA_Structure.DMA_MemoryInc = DMA_MemoryInc_Enable;

    DMA_Structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

    DMA_Structure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;

    DMA_Structure.DMA_Mode = DMA_Mode_Circular;

    DMA_Structure.DMA_Priority = DMA_Priority_High;

    DMA_Structure.DMA_M2M = DMA_M2M_Disable;

    DMA_Init(DMA1_Channel2, &DMA_Structure);

    DMA_Cmd(DMA1_Channel2, ENABLE);

    DAC_Init(DAC_Channel_1, &DAC_Structure);

    DAC_Cmd(DAC_Channel_1, ENABLE);

    DAC_DMACmd(DAC_Channel_1, ENABLE);

}

void TIM2_initialize(void)

{

   // 100Hz sinewave -> period = 10ms, 200 samples each period -> DAC must be updated every 50 us

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseInitTypeDef timerInitStructure;

timerInitStructure.TIM_Prescaler = 32-1;  // frequency 1MHz -> 1us each step

timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

timerInitStructure.TIM_Period = 50-1;

timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

TIM_TimeBaseInit(TIM2, &timerInitStructure);

TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);

TIM_Cmd(TIM2, ENABLE);

}

0 REPLIES 0