AnsweredAssumed Answered

STM32F3 - Sending Sine Wave with DAC and DMA

Question asked by Aurora on Nov 29, 2016
Latest reply on Dec 10, 2016 by Clive One
Hello!

I am currently trying to send a sine wave through the DAC. I've tried both using the std peripheral libraries and stm32cube, but to no avail. The frequencies and amplitude of the measured sine wave don't correspond to the values that I have calculated. I've been referring to note AN3126 and have modified the example provided by the std peripheral libraries. Would anyone have any advice for this? Thank you!


/* Includes ------------------------------------------------------------------*/
#include "main.h"
 
/** @addtogroup STM32F30x_StdPeriph_Examples
  * @{
  */
 
/** @addtogroup DAC_SignalsGeneration
  * @{
  */
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define DAC_DHR12R2_ADDRESS      0x40007414
#define DAC_DHR8R1_ADDRESS       0x40007410
 
 
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
DAC_InitTypeDef            DAC_InitStructure;
DMA_InitTypeDef            DMA_InitStructure;
const uint16_t Sine12bit[128] = {
                                                                            2048, 2145, 2242, 2339, 2435, 2530, 2624, 2717, 2808, 2897,
                                      2984, 3069, 3151, 3230, 3307, 3381, 3451, 3518, 3581, 3640,
                                      3696, 3748, 3795, 3838, 3877, 3911, 3941, 3966, 3986, 4002,
                                      4013, 4019, 4020, 4016, 4008, 3995, 3977, 3954, 3926, 3894,
                                      3858, 3817, 3772, 3722, 3669, 3611, 3550, 3485, 3416, 3344,
                                      3269, 3191, 3110, 3027, 2941, 2853, 2763, 2671, 2578, 2483,
                                      2387, 2291, 2194, 2096, 1999, 1901, 1804, 1708, 1612, 1517,
                                      1424, 1332, 1242, 1154, 1068, 985, 904, 826, 751, 679,
                                      610, 545, 484, 426, 373, 323, 278, 237, 201, 169,
                                      141, 118, 100, 87, 79, 75, 76, 82, 93, 109,
                                      129, 154, 184, 218, 257, 300, 347, 399, 455, 514,
                                      577, 644, 714, 788, 865, 944, 1026, 1111, 1198, 1287,
                                      1378, 1471, 1565, 1660, 1756, 1853, 1950, 2047 };  
 
 
 
/* Private function prototypes -----------------------------------------------*/
static void DAC_Config(void);
static void TIM_Config(void);
static void GPIO_Config(void);
static void DMA_Config(void);                                                                          
/* Private functions ---------------------------------------------------------*/
 
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f30x.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f30x.c file
     */
 
  /* Preconfiguration before using DAC----------------------------------------*/
  GPIO_Config();
  DMA_Config();
    DAC_Config();
  /* TIM2 configuration to trigger DAC */
  TIM_Config();
   
  /* Configures Key Button EXTI Line */
  /* Infinite loop */
  
    
          /* The sine wave and the escalator wave has been selected */
          /* Sine Wave generator ---------------------------------------------*/
           
          /* DAC channel2 Configuration */
while (1)
  {  
    }
}
 
 
/**
  * @brief  TIM2 configuration to trigger DAC conversion
 
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  TIM_TimeBaseInitTypeDef    TIM_TimeBaseStructure;
   
  /* TIM2 Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
   
  /* Time base configuration */
  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
  TIM_TimeBaseStructure.TIM_Period = (800000/(1000*128))-1; // originally 0xFF         
  TIM_TimeBaseStructure.TIM_Prescaler = 0x0;      
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;   
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
 
  /* TIM2 TRGO selection */
  TIM_SelectOutputTrigger(TIM2, TIM_TRGOSource_Update);
   
  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}
 
/**
  * @brief  DAC channels configurations (PA4 and PA5 in analog,
  *                           enable DAC clock, enable DMA2 clock)
  * @param  None
  * @retval None
  */
static void GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  
 
  
  /* GPIOA clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
   
  /* Configure PA.04 (DAC_OUT1) as analog */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
}
 
static void DAC_Config(void){
     /* DAC Periph clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
                 
                    DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
          DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
          DAC_InitStructure.DAC_Buffer_Switch = DAC_BufferSwitch_Disable;
                 
          /* DAC Channel2 Init */
          DAC_Init(DAC1, DAC_Channel_2, &DAC_InitStructure);
          
                    /* Enable DAC Channel2 */
          DAC_Cmd(DAC1, DAC_Channel_2, ENABLE);
           
          /* Enable DMA for DAC Channel2 */
          DAC_DMACmd(DAC1, DAC_Channel_2, ENABLE);
            }
 
static void DMA_Config(void){
   /* DMA2 clock enable (to be used with DAC) */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);       
    /* DMA2 channel3 configuration */
          DMA_DeInit(DMA1_Channel5);
          DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_DHR12R2_ADDRESS;
          DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&Sine12bit;
          DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
          DMA_InitStructure.DMA_BufferSize = 128;
          DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
          DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
          DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
          DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
          DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
          DMA_InitStructure.DMA_Priority = DMA_Priority_High;
          DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
          DMA_Init(DMA1_Channel5, &DMA_InitStructure);
 
          /* Enable DMA2 Channel3 */
          DMA_Cmd(DMA1_Channel5, ENABLE);
}  

Outcomes