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.

[PIC] ADC code for AC Voltage sampling

Status
Not open for further replies.

rajalakshmiA

Newbie level 6
Joined
Jun 23, 2015
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
130
Hi
I am using PIC32MX795f512l .I am new to this.. Anyone help me to find rms voltage and peak voltage for AC voltage as input for ADC

I am in urgent to close this ..
 

Hi,

First you have to specify a lot...
The most important is the AC signal.
* What waveform?
* What frequency (range) of the fundamental?
* Does it contain DC component?
* What is the highest frequency component of interest? Caused by typical waveform, noise, spikes...

How do you want to measure it?
An external, analog circuit for RMS to DC
Two extra circuits to catch the positive and negative peak voltage?

Or do you want a high speed ADC to capture even the smallest spikes?
...What processor do you have to calculate your data?
...Do you need real time data?

What voltage resolution do you need?
What time interval do you want for your RMS calculations?

Klaus

Do
 

Hi
I am using AC input of 50Hz .No DC Component.
I have Pic32mx795f512l microcontroller for ADC voltage sampling of AC input using ReadADC() function .From that i need to take samples at each 10ms of time interval and find peak voltage and RMS voltage.


Hi,

First you have to specify a lot...
The most important is the AC signal.
* What waveform?
* What frequency (range) of the fundamental?
* Does it contain DC component?
* What is the highest frequency component of interest? Caused by typical waveform, noise, spikes...

How do you want to measure it?
An external, analog circuit for RMS to DC
Two extra circuits to catch the positive and negative peak voltage?

Or do you want a high speed ADC to capture even the smallest spikes?
...What processor do you have to calculate your data?
...Do you need real time data?

What voltage resolution do you need?
What time interval do you want for your RMS calculations?

Klaus

Do
 

You'll surely need to take much more samples than one per halfwave to determine RMS and peak voltage. I would think about e.g. 2 to 5 kHz sample rate, then calculate RMS by applying the well-known relation (root of summed squares). No problem with a PIC32.

Using a summing interval of 20 ms will simplify things a lot.
 

Yes sir, i know i have to take so much samples for 20ms or 10ms time interval. the problem is how to take that samples with zero cross voltage over AC voltage in my c code... After taking that samples only i need to find peak+ and peak- voltage and RMS calculation .
 

The problem involves a bit of PIC32 related stuff (setting up periodical ADC conversion, possibly an ADC or timer interrupt, reading the results) and mostly general C programming questions.

How about starting to implement the solution by writing some lines of code?

- - - Updated - - -

In other words, you need minimal knowledge of PIC32 hardware programming and C coding. I believe you can acquire it while working on the project, but you should start soon.
 

I am good in C no problem in that area... I need Concept how to implement in Code..
 

What Compiler are you using like XC32, XC32++, mikroC PRO PIC32 ?
 

Hi Rajalakshmi,

There is so many method to read 50Hz sinewave amplitude.
sol 1. Simple solution is measure peak voltage of rectified DC (either half or fullwave) and divided by root 2.
you should make sure that adc_result should holding peak value. hope you know that how to get peak value form input.
rms_volt = adc_result/sqrt(2);
peak voltage you can calculate directly form adc_result.

sol 3. set -vref to eliminate rectification and use same logic as given above.

Sol 3 . use external HW to convert rms to dc and measure converted dc voltage through adc and calculate peak.

Thanks,
Jolly Jacob
 


Hi,

solution 4:
If you have a stable mains frequency to test ( +/- 0.5%) and a stable clock frequency (also +/-0.5%):
Then you can setup your ADC to sample with a fixed frequency.
f_sample = n x f_mains where n is an integer value >=3.
then do the RMS calculation over n samples and get very reliable values.
If possible work with n >= 16 (50Hz * 16 = 800Hz)
But you need precise timing... Very important.

Klaus
 

Frequency Measurement for AC Voltage

Hi
I am using PIC32MX795F512l ,with 10bit ADC code.Using ADC , measuring DC as well as AC voltage in my project .
Now i want to calculate frequency for AC voltage . Anyone help me how to measure the frequency in pic32 with xc32 compiler... I am newone to this ...
My code is given below

Code:
#include <plib.h>
#include<p32xxxx.h>
void AC_Handling();


#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
#pragma config DEBUG = ON              // Background Debugger Enable (Debugger is disabled)
#pragma config ICESEL = ICS_PGx1        // ICE/ICD Comm Channel Select (ICE EMUC1/EMUD1 pins shared with PGC1/PGD1)
#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF                 // Code Protect (Protection Disabled)
#define SYS_FREQ (8000000L)
void INIT_ADC();
#define config1     ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON | ADC_CONVERSION_DONE
#define config2     ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON
#define config3     ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
#define configscan  SKIP_SCAN_AN0 | SKIP_SCAN_AN1 | SKIP_SCAN_AN2 |SKIP_SCAN_AN5 |SKIP_SCAN_AN6 |SKIP_SCAN_AN7 | \
                      SKIP_SCAN_AN8 |SKIP_SCAN_AN9 |SKIP_SCAN_AN10 |SKIP_SCAN_AN11 | \
                      SKIP_SCAN_AN12 |SKIP_SCAN_AN13 |SKIP_SCAN_AN14 |SKIP_SCAN_AN15
#define configport  ENABLE_AN3_ANA | ENABLE_AN4_ANA
//#define	GetPeripheralClock()		(SYS_FREQ/(1 << OSCCONbits.PBDIV))

unsigned char offset;
unsigned int pot1=0,tempSensor=0,temp0=0,temp1=509;
float VPk=0,VNk=0,Vrms=0,ABSAVG=0,acoffset=0,ACCoupling=0,DCCoupling=0,samp=0;

int InitADCRead(void)
{
   
    INIT_ADC();
    AC_Handling();
     
      
}
void INIT_ADC()
{
    TRISB = 0b00011000;
    AD1PCFG = 0xFFE7;
    CloseADC10();   // Ensure the ADC is off before setting the configuration
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN3);
    OpenADC10( config1, config2, config3, configport,configscan);
    EnableADC10();
}

void AC_Handling()
{
     int index = 0,index1 =0;
   
    for(index=8000;index>=0;index--)
        {
            offset = 8 * ((~ReadActiveBufferADC10() & 0x01));
            pot1=ReadADC10(offset+1);
            if(pot1>=518 && temp0<pot1)
                temp0=pot1;
            if(pot1< 518 && temp1>pot1)
                temp1=pot1;
        }
        VPk = (temp0-510)/5.12;
        VNk = (510-temp1)/5.12;
        Vrms=((VPk+VNk)/2)*0.707;
        Freq = ??????????
        acoffset=(VPk-VNk)/2;
        ABSAVG=VPk+VNk;
        for(index1=0;index1<3000;index1++);


}
 
Last edited by a moderator:

Re: Frequency Measurement for AC Voltage

You could determine if the samples are increasing or decreasing and, if (say) increasing and they get above a set value (perhaps half way between the minimum and maximum) to mark the start of a cycle. When you detect the start of a cycle, see how many samples there have been since the start of the previous cycle and assuming that the samples are evenly spaced with a known sampling frequency, it is simple to calculate the time of the last cycle and hence the frequency.
You could also use other techniques to identify a known part of the cycle such as a running average of a few samples that indicates a peak. If you have previously calculated the DC component (perhaps by using the average of all samples over a sufficiently large number of samples) then you might use that as the threshold
The exact techniques you need to use will depend on how noisy the signal is as well and if the signal you are measuring is fairly steady in frequency or is modulated in some way.
Susan
 

Re: Frequency Measurement for AC Voltage

Hi
How to do with samples tel me if anybody knows... ? Howmany samples i have to take ?
if any ADC code with frequency pls share forr pic32 microcontroller ?
 

Re: Frequency Measurement for AC Voltage

Hi,

Do you need the ADC values for other calculations?

If NO, then I recommend to measure the frequency with analog comparator and a capture feature of timer/counter periferal of the PIC.
The timer has a far better timing resolution than the ADC and the software is much more easy.

Klaus
 

Re: Frequency Measurement for AC Voltage

Aussie Susan's suggestion is already taking account for deviations from ideal input waveform, which makes the processing a little bit complicated. Perhaps you should start with the obvious simple solution, determine frequency from time between two rising or falling zero crossing. To give noise immunity, implement a time filter.

I notice that your voltage measurement, which calculates Vrms from Vpp, is assuming a pure sine voltage. Don't know if this assumption is realistic for your application, but if so, a simple frequency measurement method is appropriate as well.
 

Re: Frequency Measurement for AC Voltage

One reason we are not providing you with code or direct answers is that the questions you are asking are what "design" is all about.
How many samples : how fast are you sampling relative to the frequency you want to measure? If it takes (say) 5 samples to go from peak to peak then your answers is something less than 5, but also needs to be above 2 to be sensible.
How much noise is there on the signal? You said that you have a 10-bit ADC but don't forget that the bottom 2 or more bits will be pretty random - 8 bits is about the best you can practically expect. However, if the signal itself has noise on it, you may have even fewer 'valid' bits in each sample.
Of course the alternative is, if you have the samples in memory, then do an FFT (or DFT) and look for the largest 'peak' in the frequency domain as this will also help to remove noise. However it is also a more complex solution.
Susan
 

Re: Frequency Measurement for AC Voltage

Ya i need ADC values to calculate those peak+ and Peak - @ Klaus.

Hi i dont know how many samples is needed ,because in my project frequency will vary from 50 hz to max 1khz. In this case how to measure the frequency in software .

for 5o Hz ,i am taking 300 samples with respect to 8Mhz crstal
 

Sol 3 . use external HW to convert rms to dc and measure converted dc voltage through adc and calculate peak.(I cant use this solution ).......
Sol 4: In my project the freq will vary upto 1khz from 50hz.
I need to write software code for calculate peak + and Peak - from sine wave
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top