cancel
Showing results for 
Search instead for 
Did you mean: 

L031K6 : Please check my ADC-DMA code is correct !

DU2
Associate II

I wish to get proper ADC value by ADC-DMA multi channels.

These ADC-DMA has 4 channels : trying to get 3 voltage data from 3 external port , and temperature data from one internal port.

but I realize I get quite incorrect value from external port ,

and also, temperature value is not trustable.

I guess my receiving ADC-DMA value methode which is using Interrupt is not proper..

So I will be very glad if you see my codes and check some possible errors.

for rough explaination, I tried to make these steps for getting ADC-DMA multi channel data

  1. ADC DMA Init
  2. Timer allows DMA interrupt regularly
  3. When DMA interrupt, adding adcBuffer
  4. interrupt act 50 times, stop DMA interrupt and averaging of these added adcBuffer
  5. read & use these averaged value
  6. phase 2 runs again

void DMA1_Channel1_IRQHandler(void)

{

  

 HAL_DMA_IRQHandler(hadc.DMA_Handle); 

  

   

// these "_total" value is unsigned long, these value will be averaged by " /MEASURE_RATE "

    

   originalVolt_total+= adcBuffer[0];

   referenceVolt_total += adcBuffer[1];

    

   batteryVolt_total += adcBuffer[2];

    

   measuringTimes+=1;

    

   if(measuringTimes >= MEASURE_RATE ){ // If this Interrupt is occured 50 times then :

     

    HAL_NVIC_DisableIRQ(DMA1_Channel1_IRQn); //disable to adding adc data

    

    calculateADC(); // Averaging Phase

    inMeasuring=0; // if Averaging Phase done, Tim2 reInit the DMA interrupt.

    measuringTimes=1;

     

   }  

 }

  

}

1 ACCEPTED SOLUTION

Accepted Solutions
Cwoo.1
Associate II

​I saw your code.

If you want use DMA for record ADC, maybe use 'Interrupt Calibration Solution'.

View solution in original post

7 REPLIES 7
Uwe Bonnes
Principal III

I wonder, if anybody is willing to scan through your code. A more sensible way is to start a simple example and then extend it step by step.

ok I cut them off

Cwoo.1
Associate II

​I saw your code.

If you want use DMA for record ADC, maybe use 'Interrupt Calibration Solution'.

Use 'HAL_ADC_Start_IT'

TDK
Guru

Could be your sample times are just not long enough. The temperature channel has a minimum sample time requirement, see datasheet.

If you feel a post has answered your question, please click "Accept as Solution".
DU2
Associate II

It means trying to ' hadc.Init.SamplingTime ' longer makes temperature data more trustable ?​

DU2
Associate II

thanks for help.