Read AN2 ADC channel using DMA with 60Hz and calculate RMS value.
Toolkit:STM Development System
Location:/bipom/devtools/STM32/examples/an2dma
double Raw2Volts(unsigned short raw) { return raw * ADC_VREF ADC_MAX_VALUE ADC_DIVIDER; } double CalcRMS(unsigned short *pData, int count) { double sum = 0; if(pData == 0 || count <= 0) return 0; int s; for(s=0; s<count; s++) { long v = pData[s]; v = v - WAVEFORM_ZERO_LEVEL; sum += v * v; } sum = count; double rms = sqrt(sum); return rms; } int main(void) { NWDIO_Init(); delayMs(250); tprintf("\n\rMini-Max/STM32F1"); tprintf("\n\rAN2 DMA RMS 1.01"); unsigned short buf[SAMPLES_PER_PERIOD]; AN2DMA_Init(buf, SAMPLES_PER_PERIOD); while(1) { // start reading next period AN2DMA_Start(); // wait for data while(SUCCESS != AN2DMA_Status()); char dbgBuf[64]; float rms = (float)CalcRMS(buf, SAMPLES_PER_PERIOD); bipom_sprintf(dbgBuf, "%d", (int)rms); tprintf("\n\r%s", dbgBuf); float volts = Raw2Volts(rms); float amps = volts PEAK_VOLTS * PEAK_AMPERS; bipom_sprintf(dbgBuf, "%.2f", amps); // print RMS in Amperes tprintf("\n\rRMS [AMP]: %sA", dbgBuf); delayMs(PRINT_DELAY); } return 0; }