Hello all! this is a working (below at the end), code for using ZMPT to measure Ac mains 250VAC, The code is a working code and it is fairly accurate but my problem is that I have been trying quite for some time now to translate it to STM32 Keil uVision but I can't seem to do it. I want to start My ADC learning base on this. Please can someone here help me out to port this code to Keil uVision, I want the RMS to display on serial monitor via UART1 TX_Only.
void setup(void) { Serial.begin(9600); // initialize serial communication at 9600 bits per second: //analogReference(INTERNAL); // set ADC positive reference voltage to 1.1V (internal) } // get maximum reading value uint16_t get_max() { uint16_t max_v = 0; for(uint8_t i = 0; i < 100; i++) { uint16_t r = analogRead(A0); // read from analog channel 3 (A0) if(max_v < r) max_v = r; delayMicroseconds(200); } return max_v; } // main loop void loop() { // get amplitude (maximum - or peak value) uint32_t v = get_max(); // get actual voltage (ADC voltage reference = 1.1V) v = v * 5000/1023; // v = v * 5000/1023; // calculate the RMS value ( = peak/√2 ) v /= sqrt(2); v= v+2; uint32_t v2 = (v*1.414); // print out the value you read: Serial.print("AC Volt1 = "); Serial.print(v); Serial.print(" "); Serial.print("AC Volt2 = "); Serial.println(v2); //Serial.print(v); //Serial.println("\r\n "); delay(500);
Sorry! In case you have viewed this code few minutes ago, I posted a wrong code but a have corrected it now.
It is working Arduino code. As part of learning keil with STM32, I wanted to translate this same Arduino code to stm32f103 Keil and display the result in serial monitor just as I did on Arduino and CCS C for PIC.