Avrage from ADC (code help)

Status
Not open for further replies.

Kryten

New Member
Iv got some code but im not shure how to get the code to work complitly..

Code:
void analog_sample(char *sampin, char *sampout, char *sampv){
 while(1){
     sampin = (adc_read(0)*50)/1023;
     sampout = (adc_read(1)*25)/1023;
     sampv = (adc_read(4)*15)/1023;
     }
}

void avrage(char *energyin, char *energyout, char *voltage){
     energyin = (sampin1+sampin2+sampin3+sampin4+sampin5+sampin7+...+sampin600)/600
     energyout = (sampout1+sampout2+sampin3+sampout4+sampout5+sampout7+...+sampout600)/600
     voltage = (sampv1+sampv2+sampv3+sampv4+sampv5+sampv7+...+sampv600)/600
}

I need some help whit adding the "sample number" to the samples
and a little help setting the adc to work once every 1/10 second
 
You will run out of memory if you try to do it that way on a PIC

One way of smoothing a signal is like this:-
Code:
sampin_average = (sampin_average * 599 + sampin) / 600

You have to call that a fixed interval, such as the 1/10th of a second that the ADC is sampled at.

This code smoothes the signal in a similar way that a massive resistor - capacitor network would do. The time constant is 1 minute in this case.

The simplest way to get the ADC to sample every 1/10th second is to set a timer running. When the timer overflows, the interupt flag is set.

You can get the code to look at the interupt flag more often then 1/10th second, and sample the ADC (and clear the interupt flag) if the flag is set.

Alternatively, you can turn on interupts, and get the timer interupt service routine to sample the ADC
 
A simple method is to add the 8bit sample to a zeroed 16bit number 256 times. Then just use the upper 8bits of the 16bit number as your result.
 
Well im going to measure the energy going in and out of a battery bank.. So i need to add up all the measure points and the divide them by the number of measure ponits.

I cant smooth it out because it will give me the wrong results. (loots of brainwork behind this way of getting it to be accurate)
 
Can I maybe use the SD card as a extra storage of the smaples? (and as far as i know it will just add the samples together so i wouldn't think it took that much memmoryspace
 
Doesn't that code give an error, you're passing a pointer to a char to the function and then setting the pointer to the ADC value.

Why not do a simple average, something like,
Code:
average=0;
for(i=0;i<10;i++){
    average+= (adc_read(0)*50)/1023;
}
average/=10;

//or a more accurate and faster way

average=0;
for(i=0;i<10;i++){
    average+= adc_read(0);
}
average*=5;		//instead of /10*50
average/=1023;

Mike.
 
Thanks Pommie but the *50 is to set that 5V=50A..

And yes it gives an error on that whrn compiling.. SO thanks very much..
I was hoping on something like that

Edit:
And ename to energy in / out and volt instead of average (so that i have the differnent channels separate)
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…