PIC RMS calculation error

Status
Not open for further replies.

Prince Vegeta

Member level 5
Joined
May 29, 2012
Messages
84
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
2,011
Hi... I made a circuit that rectify an AC signal into DC voltage equals the peak of that AC signal (typical usage).

I need to calculate the RMS voltage of that AC sinewave (only sinewave, for ease) signal and the specs are like the following:

- 4 diodes for full-bridge rectifier.
- 100uF/400v filtering capacitor.
- a voltage divider with 115k and 1.47k... this converts a maximum of 396v peak into 5v so it can be measured safely. Nominal voltage will never exceed 220-230v rms which is about 310v peak but I couldn't find any suitable resistor values and so I used those. They give a nice safety for PIC MCU too.

anyway, I used Proteus ISIS for simulation (never tried it in real-world yet) and here is the calculation portion of the code (MikroC):

Code:
unsigned int ADCRead(unsigned short channel){

        if (channel == 0) {ADCON0 = 0x81;}          // convert from channel0
        if (channel == 1) {ADCON0 = 0x89;}          // convert from channel1

        delay_us(80);                               // Acquisition Delay
        GO_DONE_bit = 1;                            // Set GO_DONE bit to start conversion
        while (GO_DONE_bit == 1);                   // Wait for bit to be cleared
                                                    // If bit is cleared, this means conversion is over
        l_byte = ADRESL;
        h_byte = ADRESH;
        ADR = (h_byte<<8)|l_byte;
        return ADR;
}

unsigned int RMS_calculator (unsigned short source) {

                channel = source;
                adc = ADCRead(channel);
                v_peak = adc*396;
                v_peak = v_peak/1023;
                v = v_peak/1.414;                      // rms value for sinewaves

               return v;
}

As you can see, I should call RMS_calculator(0) to measure channel0 at RA0 (PIC16F877A) and then I do whatever I need.

if I make my calculation based on ADC value, the program works... but when I make it based on the rms value it won't!

Here is the Full code: https://pastebin.com/ffXPiVyu || here is a highlighted one for C: https://pastebin.com/txUjAgMD

in short, it's an automatic transfer switch which measures 2 voltage sources and then activate one of them depending on RMS voltage of them...

as you can see, i commented out the check_status() code of using adc values and wrote a new one that uses rms voltage... it's the same but never worked the same! and from here I concluded that it's a calculation issue.


I need RMS value cuz i plan on displaying it on an LCD for both sources...

what is your suggestion? should I use ADC values in control and rms calculation in display?

I'm looking forward to your help.

thanks
 

your definition for RMS_calculator is faulthy...

it return a type of unsigned int, but the variable v that it returns is of type float..

What C compiler are you using? It should give out at least a warning for illegal variable conversion or something...

fix the function definition first..

Then, in addition, you might also want to cast your variables correctly inside this function..
 

No need to clear GO_DONE bit. It is cleared automatically by the hardware after A/D conversion is finished.

Correct.

As I recently tried, the error was about making ADR and adc variables integer. The correct thing to do is to make them float and make v,v1,v2 float.

that seemed to fix it with few enhancements here and there.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…