Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Is that C# compiler? or do you mean CCS compiler...
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;
}
Do you need to measure up to 290vac or 290 vdc, on the RA0 pin of a pic16f676?
/***************************************************************************
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
.
.
.
.}
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);
}
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);
}
...
straight from C18.... This is interpolated 24 times then hacked back to 12 bit ( 0 - 4092 adc result )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; }
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.