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.
War_Babylon! said:Please can someone here help me out to port this code to Keil uVision
So what have you tried so far? Where are you stuck?
It looks like the original is Arduino - yes?
Lots of Keil & ST learning resources:
https://community.arm.com/developer/tools-software/tools/f/keil-forum/35918/not-able-to-see-variables-in-memory-window/109675#109675
As always, the key is to start simple - solve only 1 problem at a time.
Microcontroller "Getting Started" tips: https://www.avrfreaks.net/comment/1138166#comment-1138166
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.