# STM32 DAC Trigger

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;
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.

• 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) – Marcus Müller Dec 10 '19 at 11:10