Average ADC result with CSC C

I quickly looked to see who has attempted a C# compiler for small micro.....

I just use a for loop and sum / read the ADC about 24 times... then divide by 6 ..... 12bit result averaged out (to smooth further I place a small delay in between reads)
 
Surley

C:
long getfromAdc(unsigned char channel)
	{
	char x;
	long tempresult = 0;
	
	SetChanADC(channel);
	for(x=0;x<23;x++)
		{
		ConvertADC();
		while(BusyADC());
		tempresult += ReadADC();
		}
	return tempresult/6;	
	}

straight from C18.... This is interpolated 24 times then hacked back to 12 bit ( 0 - 4092 adc result )
 
if we want mange 5 led on\off. as require i\p ie.
115volt led_1=on
165volt led_2=on
215volt led_3=on
245volt led_4=on
290volt led_5=on
use 16f676 adc i\p RA0
 
Do you need to measure up to 290vac or 290 vdc, on the RA0 pin of a pic16f676?

measure up to 290vac using rectification & divider network (dividing factor =5 , which we use linear adc sensing technique )
 
this is right way for 2 channel adc reading Average ADC result with CSC C ?????
Code:
/*************************************************************************** 
   Function getchreading 
   Calculate minivolts on selected channel 
  ****************************************************************************/ 
 int16 getchreading(int channel) 
 { 
    SETUP_ADC_PORTS(sAN0|sAN1);               // Select Analog channels on PIC 
     SETUP_ADC(ADC_CLOCK_DIV_8);            // Select ADC conversion Clock 
     delay_ms(40);                        // give some time for charge capicitor to stabilize 

     int counter=0;                        // temporary counter 
     signed int16 temperature=0;           // temporary adc_value variable 
     int16 highest=0;                     // saves highest reading for filtering 
     int16 lowest=0xFFFF;                 // saves lowest reading for filtering 
     int16 temporary=0;    
         int32 temp_result, adc_value, tlong;; 

         set_adc_channel(channel); // Select Channel 
         // Average ADC value 
    delay_us(15);                        // small delay to settle 
     counter=0;                           // clear counter 
     lowest=0xFFFF;                        // reset lowest 
     highest=0;                           // reset highest 
     while(counter<18)                     // Take 18 readings and average (16+highest+lowest) 
     { 
        temporary=READ_ADC();               // take an ADC reading 
          if(temporary<lowest)               // check if smaller than lowest 
             lowest=temporary;               // save value if smaller 
            if(temporary>highest)               // check if larger than highest 
              highest=temporary;               // save value if larger    
        temp_result-=temporary;               // substract the ADC values for averaging (adc i\p) 
        counter++; 
     } 
     temp_result+=highest;                  // remove highest value 
     temp_result+=lowest;                  // remove lowest value 

     temp_result/=16;                     // average by 16 
         adc_value = temp_result 

         tlong = (int32)ADC_value*5000; //Convert the result in millivolts 
         tlong = tlong/1023; // 0..1023 -> 0-5000mV 
    SETUP_ADC(ADC_OFF);                     // turn off ADC module 
         return (int32) tlong; 
 } 
  /***************************************************************************** 
   Function ProcessMains 
   Reads Form AN0 ADC_Value and Reads Form AN1 ADC_Value 
  ****************************************************************************/ 
 int ProcessMains(void) 
 { 
    ch_0 = getchreading(0); 
    ch_1 = getchreading(1); 
 .here do somthing ch_0 & ch_1 
 . 
 . 
 . 
 .}
 
Here are two averaging functions I've used in CCS in the past.

Code:
int8 average()
{
	int32 result = 0;
	int8 m = 0;
	while (m < DATA_LENGTH)	// repeat running average 'm' times
	{
		result += (dataArray[m]);
		m++;
	}
	result = result / DATA_LENGTH;
	return((int8)result);
}

Code:
int8 runningAverage()
{
	int16 result = 0;
	int8 m = 0;
	while (m < DATA_LENGTH)	// repeat running average 'm' times
	{
		result += (((int16)dataArray[m]*100) / DATA_LENGTH);	// add two decimal places to measurement
		m++;
	}
	result = result / 100;
	return((int8)result);
}
 

Sorry to nitpick an old post, but this code;
for(x=0;x<23;x++)
makes the ADC event occur 23 times, not 24 times.
 
Sorry to nitpick an old post, but this code;
for(x=0;x<23;x++)
makes the ADC event occur 23 times, not 24 times.

Don't be sorry Roman... I'm the first to agree that typo's SHOULD be highlighted.....

The first time I did an interpolation, I couldn't understand WHY the ADC result was sooo choppy....


I apologize if this code has caused distress to anyone...

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