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