0
\$\begingroup\$

I am Trying to use the DAC output on my STM32L053R8 NUCLEO Board. I generate the corresponding Sin-Wave values to set the DAC Values like this.

    // Resolution for DAC
        for(i=0; i<= 249; i++)
        {
                dacVal[i]= 2048*(sin(i*2*pi/250)+1);
        }           

In the Main function i use the while loop to write the values to the DAC.

  while (1)
  { 
        for(i=0; i<=249; i++)
        {           
            HAL_DAC_SetValue(&hdac, DAC1_CHANNEL_1,DAC_ALIGN_12B_R, dacVal[i]);
        }
        i=0;        
  } 

The Result is a nice Sine Wave with a peak-peak Voltage of 3.3V and a Frequency of 200Hz. I generated the init code with CubeMX.

Now i want to increase the Frequency of the DAC output. I initialized the DAC with this Function:

static void MX_DAC_Init(void)
{


  DAC_ChannelConfTypeDef sConfig = {0};

  /*DAC Initialization*/

  hdac.Instance = DAC;
  if (HAL_DAC_Init(&hdac) != HAL_OK)
  {
    Error_Handler();
  }
  /*DAC channel OUT1 config*/

  sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  if (HAL_DAC_ConfigChannel(&hdac, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
}

And my TIM6 like this:

static void MX_TIM6_Init(void)
{
  TIM_MasterConfigTypeDef sMasterConfig = {0};

  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 2;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 1000;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
}

The code generates a Sin-Wave like i need it. But if i change

sConfig.DAC_Trigger = DAC_TRIGGER_NONE;

to

sConfig.DAC_Trigger = DAC_TRIGGER_T6_TRGO;

There is no Output anymore. I Assume that the Output is triggered by the Timer TIM6 but its somehow not working like that. I think there is something wrong with my Timer init function. At the end i just want to put my ouput frequency of the Sin-Wave to 40khz.

\$\endgroup\$
  • 1
    \$\begingroup\$ hint: 3/4 of your table are superfluous, since sin is odd-symmetric to \$\pi\$ and within \$[0;\pi]\$ even-symmetric to \$\frac\pi2\$. Use the extra space for a better resolution, or reduce the necessary memory bandwidth if you have caches (the stm32l0 probably has no caches, though) \$\endgroup\$ – Marcus Müller Dec 10 '19 at 11:10
0
\$\begingroup\$

STMCubeMX has generated the TIM6 Init code for you but it has not started the timer, you need to call the HAL_TIM_Base_Start() function after calling MX_TIM6_Init so that TIM6 CR1 has CEN bit set which starts the timer running.

\$\endgroup\$
  • \$\begingroup\$ I did start the Timer and the DAC after i initialized all the peripheral. HAL_TIM_Base_Start_IT(&htim6); HAL_DAC_Start(&hdac, DAC_CHANNEL_1); \$\endgroup\$ – Walter Nazarenus Dec 10 '19 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.