cancel
Showing results for 
Search instead for 
Did you mean: 

How to Set DAC_SWTRGR bit using HAL Library for STM32L432KC?

ControlFreak
Associate III

I am using STM32L432KC-Nucleo board for my project.I am programming DAC channel 1 on PA4 for fixed output voltage. I am employing software trigger mode. With code given below I am not getting any voltage on PA4. On debugging the code I found that DAC_SWTRGR bit 0 is not getting set resulting in DAC_DHR12R1 not getting transferred to DAC_DOR1 RM0394 Page 464. How do you set this bit in HAL Library? (I was unable to find any independent function performing this task )

int main(void)

{

 HAL_Init();

 SystemClock_Config();

// DACx_FORCE_RESET();

// DACx_RELEASE_RESET();

 /*START of Toggle LED program Initialization code*/

 __HAL_RCC_GPIOB_CLK_ENABLE();

  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

   GPIO_InitStruct.Pull = GPIO_PULLUP;

   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;

   GPIO_InitStruct.Pin = LED3_PIN;

   HAL_GPIO_Init(LED3_GPIO_PORT, &GPIO_InitStruct);

   /*Configuring PA4 as DAC_Out1*/

 __HAL_RCC_GPIOA_CLK_ENABLE();

 GPIO_InitStruct.Mode=GPIO_MODE_ANALOG ;

 GPIO_InitStruct.Pull=GPIO_NOPULL;

 GPIO_InitStruct.Speed=GPIO_SPEED_FREQ_VERY_HIGH;

 GPIO_InitStruct.Pin= GPIO_PIN_4;

 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 HAL_GPIO_WritePin(GPIOB, LED3_PIN,GPIO_PIN_SET); // Error Handling

   HAL_Delay(100);

 /*Configuring of DAC Registers */

 __HAL_RCC_DAC1_CLK_ENABLE();      //RCC APB1- DAC Peripheral clock

 HAL_DAC_DeInit(&DAC_OUT1);

 DAC_OUT1.Instance=DAC1;

 DAC_OUT1_NoDMA.DAC_SampleAndHold=DAC_SAMPLEANDHOLD_DISABLE;

 //DAC_OUT1_NoDMA.DAC_Trigger=DAC_TRIGGER_NONE;

DAC_OUT1_NoDMA.DAC_Trigger=DAC_TRIGGER_SOFTWARE;

 DAC_OUT1_NoDMA.DAC_OutputBuffer=DAC_OUTPUTBUFFER_ENABLE; //DAC output buffered for low impedance loads

 //DAC_OUT1_NoDMA.DAC_ConnectOnChipPeripheral=DAC_CHIPCONNECT_DISABLE;

 //DAC_OUT1_NoDMA.DAC_UserTrimming=DAC_TRIMMING_FACTORY;

 if(HAL_DAC_Init(&DAC_OUT1) != HAL_OK)

  {

 HAL_GPIO_WritePin(GPIOB, LED3_PIN,GPIO_PIN_RESET); // Error Handling

   HAL_Delay(100);

      }

 if(HAL_DAC_ConfigChannel(&DAC_OUT1, &DAC_OUT1_NoDMA, DAC_CHANNEL_1) != HAL_OK)

  {

 HAL_GPIO_WritePin(GPIOB, LED3_PIN,GPIO_PIN_RESET); // Error Handling

  HAL_Delay(100);

        }

 if( HAL_DAC_Start(&DAC_OUT1, DAC_CHANNEL_1) != HAL_OK)

    {

 HAL_GPIO_WritePin(GPIOB, LED3_PIN,GPIO_PIN_RESET); // Error Handling

    HAL_Delay(100);

          }

 if( HAL_DAC_SetValue(&DAC_OUT1, DAC_CHANNEL_1, DAC_ALIGN_12B_R,0xFFF) != HAL_OK)

    {

 HAL_GPIO_WritePin(GPIOB, LED3_PIN,GPIO_PIN_RESET); // Error Handling

    HAL_Delay(100);

          }

 //SET_BIT(DAC_OUT1->DAC1->SWTRIGR, DAC_SWTRIGR_SWTRIG1);

 HAL_Delay(100);

while(1);

}

1 REPLY 1
ControlFreak
Associate III

I found one way to do it ADD

SET_BIT(DAC_OUT1.Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);

If some has better way Please let me know it will be helpful