Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

instantaneous Vrms using microcontroller 16F887 and 2 lines LCD

Status
Not open for further replies.

Sarah Emran

Newbie level 4
Joined
May 17, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,337
screenshot.png

URGENT! please. i need to do the calculation of Vrms by considering the frequency as they are not the same for every sample. the input signal is AC and not pure sine and it may has distortion. so i want to do as much sampling as i can in order to get more accurate calculation. im using 4Mhz crystal. can i make the wave sampling for up to 100 samples r even more?? help me please :-(
 
Last edited:

take many samples within time period value and get average of them(sum of samples/total samples added)
 

thank u for the answer. i know how to do the calculation but i dont know how to develop the coding. im using microC PRO for PIC
 

sarah its very simple
void main(void)
{
//initialiaze timer to overflow after 20ms and disable interrupt
//initialize adc
for(;;)
{
i=0;
while(!intcon.t0if)
{
//sample=adc_read(0);
i++;
}
k=0;
rms =0;
while(k<i)
{
rms=rms+sample[k];
}
rms=(float)rms/(i-1);
}
}
 

thank u so much zia. i will try this. but can u help me list the function of each line? (i mean each line is for what) i am so very new to this and im still learning.
 

Hi sarah
I will say you something that can help you about simulation of proteus! :

1-you don't need to put voltage on lcd in proteus and GND too.
2-the pin of reset is enough if you put power flag on it.

and about your circuit :
1-adc_read is 10bit value...for example if your voltage be 5v and your vref be 5v , it is 1023...so you must make a formula like this that can earn 5v from 1023 for show on lcd:
sample=adc_read(0).....0 mean channel 0 of adc ...(RA.0)
v=(sample*5)/1023....so when your input voltage is 5 v so sample is 1023 thereupon V=5.

but this is about dc voltage!

for ac i think you must put a diode to forbid to come negative voltage to your MCU.

best regards
Green Tree
 

Be aware that the ADC in these PICs is not very fast and to take an accurate reading you need lots of samples within one cycle of your signal. If you need a true RMS reading at more than low frequency, I suggest you look at using an external RMS to DC converter and read it's output voltage into the ADC. The PIC ADC conversion speed depends on it's clock speed, the number of bits you want in the result and the source impedance.

If you use the PIC alone, you will have to scale the input signal voltage and offset it's voltage so it's most negative points do not go below the negative ADC reference and it's most positive points do not go above the positive ADC reference. You cannot add a diode in series with the input as this would remove parts of the signal you are trying to measure.

Brian.
 

Hi dear brian
as i could understand with pic we cann't measure negative and positive voltage together , yes?
if we use two channel , can we measure them ?

Best Regards
Green Tree
 

No. The PIC only has one ADC converter, each of the inputs pins can be internally connected to the one ADC but you cannot measure two inputs at the same time. If you change inputs you also have to wait a short time for the voltage inside the ADC sampling circuit to adapt to the new voltage and this makes it even slower.

The silicon inside the PIC cannot withstand negative voltages or voltages higher than it's supply at all, this is why the signal has to be scaled and offset so it fits within those limits.

Brian.
 

thank you so much dear Brian...
i test it before and see i can use two formula ...so we can measure 8 voltage but with one formula(i did it)
so what is your suggestion about ac voltmeter?

Best Wishes
Green Tree
 

Be aware that the ADC in these PICs is not very fast and to take an accurate reading you need lots of samples within one cycle of your signal. If you need a true RMS reading at more than low frequency, I suggest you look at using an external RMS to DC converter and read it's output voltage into the ADC. The PIC ADC conversion speed depends on it's clock speed, the number of bits you want in the result and the source impedance.

If you use the PIC alone, you will have to scale the input signal voltage and offset it's voltage so it's most negative points do not go below the negative ADC reference and it's most positive points do not go above the positive ADC reference. You cannot add a diode in series with the input as this would remove parts of the signal you are trying to measure.

Brian.

thank u brian. im calculating true RMS reading. but i can still measure the voltage without RMS to DC converter right? because my supervisor wants me to do the calculation or sampling using the ac signal. and how do i pick up both positive and negative signal?

- - - Updated - - -

Hi sarah
I will say you something that can help you about simulation of proteus! :

1-you don't need to put voltage on lcd in proteus and GND too.
2-the pin of reset is enough if you put power flag on it.

and about your circuit :
1-adc_read is 10bit value...for example if your voltage be 5v and your vref be 5v , it is 1023...so you must make a formula like this that can earn 5v from 1023 for show on lcd:
sample=adc_read(0).....0 mean channel 0 of adc ...(RA.0)
v=(sample*5)/1023....so when your input voltage is 5 v so sample is 1023 thereupon V=5.

but this is about dc voltage!

for ac i think you must put a diode to forbid to come negative voltage to your MCU.

best regards
Green Tree

thank u green tree :-D fyi i've try this method and it works well. my problem is when it comes to sampling as i need true RMS calculation.

- - - Updated - - -

thank all :smile: i need the program to calculate the true RMS including the sampling. i just knew how to calculate it manually and how the process go. i just do not know how to code it.

- - - Updated - - -

20130517_175437.jpg

i wanted to do this calculation where i will have around 50 samples of v2.
 

It is impossible to measure the negative signal directly, the PIC will be damaged if you try.

Imagine your signal is from -1V to +1V peak to peak. What you have to do is shift the whole signal, outside the PIC, by adding at least +1V to it. If you do that, the signal will then go from 0V to +2V which you can measure with the ADC.

Another example:
If your signal was between -30V and +30V, you would first have to scale it so the difference between positive and negative is less than the ADC range (usually 5V). So first electrically divide the signal (two resistors may be enough) so the 60V peak to peak is no more than 5V peak to peak. In other words divide by 12. This gives you a signal from -2.5V to +2.5V. Next, add 2.5V to it so the negative part becomes positive and the voltage now goes from 0V to 5V. you can then read it into the ADC and use software to re-scale the reading to take into account the original division. The easiest way to add a voltage is to use an op-amp at unity gain with one input set to the voltage you want to add.

Brian.
 

OK!:grin: now as i understood , this way is good :
you must use two channel and then as Brian said shift your voltage up (so you haven't negative voltage) then with resistance division your voltage become 0 to 5 volt...and now you can put it to pic.
i think it's best way.:wink:
and you can shift voltage with a diode and a capacitor.

Best Wishes
 

i have tried this program and it runs well with my circuit. can u please modify the calculation for me? i wanted to do the calculation for true Vrms. take 20 samples of v2. then sum all the samples and multiplied by sampling duration which is 1millisecond. then divide it with 20. lastly square root the value. thank u for your help

// Lcd module connections
sbit LCD_RS at latD1_bit;
sbit LCD_EN at latD0_bit;
sbit LCD_D4 at latD4_bit;
sbit LCD_D5 at latD5_bit;
sbit LCD_D6 at latD6_bit;
sbit LCD_D7 at latD7_bit;
sbit LCD_RS_Direction at TRISD1_bit;
sbit LCD_EN_Direction at TRISD0_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
void main() {

float adc_val = 0.0, old_val = 0.0;
unsigned char str_adc_val[17];
unsigned int i = 0;
float val2avg = 0.0;

TRISA = 0x04;
PORTA = 0x00;
TRISB = 0x00;
PORTB = 0x00;
TRISC = 0x80;
PORTC = 0x00;
TRISB = 0x00;
PORTB = 0x00;
ADCON1 = 0b00001100;
CMCON = 0x07;
CVRCON.CVROE = 0;



LCD_Init();
LCD_Cmd(_LCD_CLEAR);
LCD_Cmd(_LCD_CURSOR_OFF);
LCD_Out(1,1,"VRMS");


while(1){
Delay_ms(1000);
for(i = 0; i< 20; i++){
adc_val = ADC_Read(2);
val2avg = val2avg + (adc_val * 5 / 1023) * 60;
}
val2avg = val2avg / 20;

if(val2avg != old_val){
old_val = val2avg;
FloatToStr(val2avg, str_adc_val);
LCD_Out(2,1,str_adc_val);
}


}}
 
Last edited:

Take no. of samples 2*f . if u dont know frequency find it first or take samples 2*highest frequency for good results.
don't use delay (Delay_ms(1000) ) any more .
Here is code possibly it help u


const long NUM_DATA_POINTS = 3000;

for(i=0; i<NUM_DATA_POINTS; ++i)
{
value = ADC_Read(2);
voltage += (float)value*(float)value;
}

Voltage=ADC VALUE * (5 Volts)/(1023) * (5 Volts)/(1023);
voltage = sqrt(voltage/(NUM_DATA_POINTS));


If your input wave is symmetric about time axis don't use clamp ckt to shift signal simple multiply Voltage by 2
 

Take no. of samples 2*f . if u dont know frequency find it first or take samples 2*highest frequency for good results.
don't use delay (Delay_ms(1000) ) any more .
Here is code possibly it help u


const long NUM_DATA_POINTS = 3000;

for(i=0; i<NUM_DATA_POINTS; ++i)
{
value = ADC_Read(2);
voltage += (float)value*(float)value;
}

Voltage=ADC VALUE * (5 Volts)/(1023) * (5 Volts)/(1023);
voltage = sqrt(voltage/(NUM_DATA_POINTS));


If your input wave is symmetric about time axis don't use clamp ckt to shift signal simple multiply Voltage by 2

thank u Mr Imran Ali. can this code be simulated in the proteus software? because i noticed that there is three algorithms over there.
 
Last edited:

Yes it can be simulated on Proteus and also on hardware i made it and it is running well with genset at home
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top