Skip to main content
manoritesameer
Associate III
June 24, 2014
Question

Digital Value to DAC

  • June 24, 2014
  • 4 replies
  • 1239 views
Posted on June 24, 2014 at 21:06

Hello,

       I am using the STM32F4 discovery board. I have a project in which I have a timer which generate an interrupts @ 100Khz. In the ISR it stores a new value to a variable. 

       Now how can I use the DMA to send this value to the DAC which is updated at the same rate.

Thanks

Regards

Sameer

#stm32
    This topic has been closed for replies.

    4 replies

    warcatz
    Associate III
    June 24, 2014
    Posted on June 24, 2014 at 22:16

    hi ,

    i do it like this :

    #define BITDEPTH 12 // 12 bits DAC
    #define SAMPLERATE (SYSCLK / (1 << 
    BITDEPTH
    )) // Best audio quality 192MHz / (1 << 11) = 46875Hz
    // Initialise Timer2
    void Timer2_Init(void)
    {
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    // Clock enable
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
    // Timer2 init
    TIM_TimeBaseStructure.TIM_Period
    = 
    SYSCLK
    / SAMPLERATE;
    TIM_TimeBaseStructure.TIM_Prescaler
    = 
    0
    ; // 192Mhz tick (adjust per your clock)
    TIM_TimeBaseStructure.TIM_ClockDivision
    = 
    TIM_CKD_DIV1
    ;
    TIM_TimeBaseStructure.TIM_CounterMode
    = 
    TIM_CounterMode_Up
    ;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
    // Timer2 enable
    TIM_ARRPreloadConfig(TIM2, ENABLE);
    TIM_Cmd(TIM2, ENABLE);
    }
    // *********************************************************************************
    // Timer2 interrup and DAC2 init
    void Mod_Int_Init(void)
    {
    NVIC_InitTypeDef NVIC_InitStructure;
    GPIO_InitTypeDef DAC_GPIO_Init; 
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
    //---------------------------------------------
    // init from Timer2 Interrupt
    TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel
    = 
    TIM2_IRQn
    ;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority
    = 
    0
    ;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority
    = 
    0
    ;
    NVIC_InitStructure.NVIC_IRQChannelCmd
    = 
    ENABLE
    ;
    NVIC_Init(&NVIC_InitStructure);
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
    DAC_GPIO_Init.GPIO_Pin
    = 
    GPIO_Pin_5
    ;
    DAC_GPIO_Init.GPIO_Mode
    = 
    GPIO_Mode_AN
    ;
    DAC_GPIO_Init.GPIO_Speed
    = 
    GPIO_Speed_25MHz
    ;
    DAC_GPIO_Init.GPIO_OType
    = 
    GPIO_OType_PP
    ;
    DAC_GPIO_Init.GPIO_PuPd
    = 
    GPIO_PuPd_NOPULL
    ;
    GPIO_Init(GPIOA, &DAC_GPIO_Init);
    DAC_Cmd(DAC_Channel_2,ENABLE);
    }
    // *********************************************************************************
    // Timer2 interrupt
    void TIM2_IRQHandler(void)
    {
    if(File_Loaded != 0)
    if(SoundBuffer.writePos != SoundBuffer.readPos) 
    {
    // Output the Audio as mono (only DAC2 pin is free)
    Sound_Out = (uint16_t)(((SoundBuffer.left[SoundBuffer.readPos] + SoundBuffer.right[SoundBuffer.readPos])) >> 1);
    DAC_SetChannel2Data(DAC_Align_12b_R,Sound_Out);
    SoundBuffer.readPos++;
    SoundBuffer.readPos &= SOUNDBUFFERSIZE - 1;
    }
    Int_Count++;
    // Clear interrupt flag
    TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
    }

    I hope it can help you . It's a part from my st429 mod player. Tou can get the full source on this page :

    https://sites.google.com/site/suprabotics/stm429-discovery-expansion-board-v2-0/amiga-module-player-with-the-stm32f429

    Cheers.
    warcatz
    Associate III
    June 24, 2014
    Posted on June 24, 2014 at 22:17

    double post .. delete thisd one ...

    Tesla DeLorean
    Guru
    June 24, 2014
    Posted on June 24, 2014 at 23:02

    If you're computing it in a periodic interrupt, could you not just write it to the DAC then? Otherwise you've got to pay particular attention to keeping the IRQ and DMA synchronized.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    manoritesameer
    Associate III
    June 25, 2014
    Posted on June 25, 2014 at 20:04

    thank you all... U all guys are great. Also I would like to thank Clive separately for ur great help and amazing spirit.