cancel
Showing results for 
Search instead for 
Did you mean: 

ADC to DAC

meriarajagukguk
Associate III

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Sarra.S
ST Employee

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);
    }
}

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

4 REPLIES 4
Sarra.S
ST Employee

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);
    }
}

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

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.

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

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

but why do the values ​​produced by the oscilloscope not match the results even at only 35-40 hz?
Are there any conditions that must be met apart from the prescaler and the period?