0

I was wondering how to create an array on STM32cubeIDE. I like to fill this array with values from an independent LDR.

Below is the set-up code for the ADC:

/*User Begin PV*/
uint16_t value_DAC = 0;uint16_t value_ADC =0;uint32_t state_DAC = 0;
/*User Begin 2*/
HAL_ADCEx_Calibration_Start(&hadc1, ADC_SINGLE_ENDED); // Calibrate ADC
HAL_ADC_Start_DMA(&hadc1, (uint32_t *) &value_ADC, 1); // Start ADC with DMA
HAL_DAC_Start(&hdac1, DAC_CHANNEL_2); // Start the DAC

Any help would be much appreciated.

I haven't tried anything yet because I really don't know where to start.

1 Answer 1

0

your question is a bit unclear. if you could already read the adc value, the only thing to do on top is to make a simple c array like int value_ADC[20]; and then read and store the adc value in it periodically with your desired interval. to do so you can either use blocking method with a loop if the only thing you wanna do is to read adc like this:

    while (1)
    {
        //shifting each array element to right by one
        //so we can save the new value on index 0
        for (int i = 20 - 1; i > 0; i--) {
            value_ADC[i] = value_ADC[i - 1];
        }

        //read adc value and save it on index 0 of array
        HAL_ADC_Start(&hadc1);
        HAL_ADC_PollForConversion(&hadc1, 1);
        value_ADC[0] = HAL_ADC_GetValue(&hadc1);

        //whatever you wanna do with adc values

        HAL_Delay(10);//change to desired interval
    }

or non-blocking if you have parallel tasks, with "timer interrupts" that you can learn more about by searching the term. if the speed of timer was too high you can use a counter to read adc value every 100th interrupt for example and also reset the counter.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for your help this really simplifies it from what I have now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.