Search code examples
arraysglobal-variablesadcstm32cubeide

Creating an Array and filling it with ADC values - STM32L476G


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.


Solution

  • 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.