cancel
Showing results for 
Search instead for 
Did you mean: 

DAC output result input to ADC

Ken_102633
Associate II

I'm new at STM32 MCU.

Is it possible that on STM32MP157D-DK1?

I first set a value to DAC and then use its output as input to ADC then finally get a digital output which must same as the value i set to DAC. (Without DMA)

If I can do that , how can I do this connection in between the DAC output and ADC input ?

Thank you all in advance.

Best Regards,

Ken

static void MX_DAC1_Init(void)
{

  DAC_ChannelConfTypeDef sConfig;

  hdac1.Instance = DAC1;
  if (HAL_DAC_Init(&hdac1) != HAL_OK)
  {
    Error_Handler();
  }
  sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC;
  sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
  sConfig.DAC_Trigger = DAC_TRIGGER_NONE;
  sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
  sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
  if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
}
__STATIC_INLINE void Configure_ADC(void)
{
  ADC_ChannelConfTypeDef   sConfig;

  hadc2.Instance = ADC2;

  hadc2.Init.ClockPrescaler        = ADC_CLOCK_ASYNC_DIV1;
  hadc2.Init.Resolution            = ADC_RESOLUTION_12B;
  hadc2.Init.ScanConvMode          = ADC_SCAN_DISABLE;              
  hadc2.Init.EOCSelection          = ADC_EOC_SINGLE_CONV;
  hadc2.Init.LowPowerAutoWait      = DISABLE;
  hadc2.Init.ContinuousConvMode    = DISABLE;                       
  hadc2.Init.NbrOfConversion       = 1;                             
  hadc2.Init.DiscontinuousConvMode = DISABLE;                       
  hadc2.Init.NbrOfDiscConversion   = 1;                            
  hadc2.Init.ExternalTrigConv      = ADC_SOFTWARE_START;            
  hadc2.Init.ExternalTrigConvEdge  = ADC_EXTERNALTRIGCONVEDGE_NONE; 
  hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR;
  hadc2.Init.Overrun               = ADC_OVR_DATA_OVERWRITTEN;
  hadc2.Init.OversamplingMode      = DISABLE;

  if (HAL_ADC_Init(&hadc2) != HAL_OK)
  {
    Error_Handler();
  }
 
  sConfig.Channel      = ADC_CHANNEL_DAC1CH1_ADC2;  
  sConfig.Rank         = ADC_REGULAR_RANK_1;      
  sConfig.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;   
  sConfig.SingleDiff   = ADC_SINGLE_ENDED;            
  sConfig.OffsetNumber = ADC_OFFSET_NONE;             
  sConfig.Offset       = 0;                           
  
  if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
  {
    Error_Handler();
  }
}
void Dac_to_Adc(void)
{   
   if (HAL_DAC_SetValue(&hdac1, DAC_CHANNEL_1, DAC_ALIGN_12B_R, DIGITAL_SCALE_12BITS) != HAL_OK)
   {
	 Error_Handler();
   }
   HAL_Delay(1);

   /* Enable DAC Channel: channel corresponding to ADC channel ADC2_CHANNEL_16 */
   if (HAL_DAC_Start(&hdac1, DAC_CHANNEL_1) != HAL_OK)
   {
     /* Start Error */
     Error_Handler();
   }
   DAC_Buffer1 = HAL_DAC_GetValue (&hdac1, DAC_CHANNEL_1);  // Get: 4095

   if (HAL_ADC_Start(&hadc2) != HAL_OK)
   {
     Error_Handler();
   }

   if (HAL_ADC_PollForConversion(&hadc2, 2000) != HAL_OK)
   {
     Error_Handler();
   }

   ADC_Buffer = HAL_ADC_GetValue(&hadc2);      // Get: 2047

   if (HAL_ADC_Stop(&hadc2) != HAL_OK)
   {
     Error_Handler();
   }
}

 

0 REPLIES 0