Skip to main content
Associate III
July 29, 2024
Solved

ADC to DAC

  • July 29, 2024
  • 1 reply
  • 1477 views

hallo

can anyone help me about how to program for convert adc to dac ini stm32cubemx with program keil U5, have you guys can help to bring me an example of your code or tips and step. i used the potentiometer for adc value and the value used to dac and read by osciloscop

can you help me? thanks

 

Best answer by Sarra.S

Hello @meriarajagukguk 

For ADC config: 

  • Resolution: 12-bit
  • Scan Conversion Mode: Disabled
  • Continuous Conversion Mode: Enabled
  • External Trigger Conversion: Software Start
  • Data Alignment: Right

For DAC config: 

  • Trigger: None
  • Output Buffer: Enabled

In your while (1), you should set the DAC output to the ADC value

while (1)
{
 // Poll for ADC conversion completion
 if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK)
 {
 // Read the ADC converted value
 uint32_t adcValue = HAL_ADC_GetValue(&hadc1);
 
 // Set the DAC output to the ADC value
 HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, adcValue);
 }
}

1 reply

Sarra.SBest answer
ST Employee
July 29, 2024

Hello @meriarajagukguk 

For ADC config: 

  • Resolution: 12-bit
  • Scan Conversion Mode: Disabled
  • Continuous Conversion Mode: Enabled
  • External Trigger Conversion: Software Start
  • Data Alignment: Right

For DAC config: 

  • Trigger: None
  • Output Buffer: Enabled

In your while (1), you should set the DAC output to the ADC value

while (1)
{
 // Poll for ADC conversion completion
 if (HAL_ADC_PollForConversion(&hadc1, HAL_MAX_DELAY) == HAL_OK)
 {
 // Read the ADC converted value
 uint32_t adcValue = HAL_ADC_GetValue(&hadc1);
 
 // Set the DAC output to the ADC value
 HAL_DAC_SetValue(&hdac, DAC_CHANNEL_1, DAC_ALIGN_12B_R, adcValue);
 }
}
Associate III
July 29, 2024

Hello sarra thanks for your reply.

i want to ask again about how to set value freq value, if i have stm32nucleo with 72 MHz CPU, CCM, 12-bit ADC 5 MSPS. if i want output of the freq value in osciloscop is 200 Hz. 

f=F_CLK / ((Prescaler+1)×(Period+1))

I have used the formula above but when I apply it to htim2.Init.Prescaler and htim2.Init.Period but the results I get are only in the range of 35-40 hz, am I using the wrong formula or is there a step I missed please help me find the way.

ST Employee
July 29, 2024

The formula us correct, 

Re-arranging the formula; (PSC+1)(Period+1) = F_CLK/f = (72.10^6)/200= 360 000

Now, you can choose a prescaler value and calculate the corresponding period value, or vice versa

For simplicity, let's choose a prescaler value of 359 (since prescaler is typically a small value)

For PSC=359 => Period= 999