Untitled
unknown
plain_text
8 days ago
1.7 kB
4
Indexable
Never
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc) { // ADC conversion complete, buffer should have the latest value adc_val = buffer[0]; // Read ADC value from buffer // Process adc_val as needed // Update the min value if (adc_val < minValue) { minValue = adc_val; } // Update the max value if (adc_val > maxValue) { maxValue = adc_val; } // Check if ADC value is within the desired range if ((adc_val > 2050) && (adc_val < 2100)) { collectingData = 1; } else { collectingData = 0; readingIndex = 0; // Reset index if value goes out of range } // Collect ADC readings if within range if (collectingData && (readingIndex < 500)) { adcReadings[readingIndex++] = adc_val; } // Once 500 readings are collected, calculate RMS if (readingIndex == 500) { uint64_t sumOfSquares = 0; for (uint16_t i = 0; i < 500; i++) { sumOfSquares += adcReadings[i] * adcReadings[i]; // Sum of squares } rmsValue = sqrt((float)sumOfSquares / 500); // Calculate RMS // Reset the index and cycle readingIndex = 0; collectingData = 0; } // Restart ADC conversion if needed // HAL_ADC_Start_DMA(&hadc1, (uint32_t*)buffer, 1); }
Leave a Comment