I'm not sure I understand. Are you saying you take two measurements on the same pin with a two second gap betwen them then compare the values?
Brian.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 while(1) { ADC_value = GetADCValue(AN2); // Read ADC value from GP4 pin // ADC_value can have a value from 0 (0v) to 1023(5v) only. __delay_ms(50); { flag=1; if ((ADC_value>=700) && (flag==1)) { LED=1; GP0=1; __delay_ms(2000); //Refernce is obtained Buzzer ON LED1=0; //GREEN GP0=0; } flag=0; if ((ADC_value>=700) && (ADC_value>=500) && (flag==0)) { LED=1; __delay_ms(50); LED1=1; } else if ((ADC_value>=400) && (ADC_value<=500) && (flag==0)) { LED=1; __delay_ms(50); LED1=0; } else { LED=0; LED1=0; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 unsigned int SampleCount; unsigned int ADCSamples[10]; unsigned int ADC_value = 0; unsigned int Average,Average1; unsigned int flag; InitADC(AN2); // Initialize GP2 as ADC input pin CHANNEL 2 while(1) { // ADC_value = GetADCValue(AN2); // Read ADC value from pin GP2 pin __delay_ms(2000); for(SampleCount = 0; SampleCount <=10; SampleCount++) { __delay_ms(250); LED=1; ADCSamples[SampleCount] = GetADCValue(AN2); } Average =((ADCSamples[SampleCount])/20); __delay_ms(2000); LED1=0; LED=0; for(SampleCount = 0; SampleCount <=10; SampleCount++) { __delay_ms(250); LED=1; ADCSamples[SampleCount] = GetADCValue(AN2); } Average1 =((ADCSamples[SampleCount])/20); __delay_ms(2000); flag=1; } if((Average == Average1) && (flag==1)) { LED=1; LED1=1; } else if((Average >= Average1) && (flag==1)) { LED=0; LED1=1; }
pointer = 20;
while(pointer)
{
total += array[pointer];
pointer--;
}
average = total / 20;
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // Main function void main() { InitADC(AN2); // Initialize GP2 as ADC input pin CHANNEL 2 while(1) { // ADC_value = GetADCValue(AN2); // Read ADC value from pin GP2 pin __delay_ms(2000); unsigned int ADCSamples[3]; unsigned int i ; for(i = 0; i < 3; i++) { ADCSamples[i] = GetADCValue(AN2); } } }
unsigned int ADCSamples[3];
unsigned int i ;
I do not have the Hitech C compiler but basically it looks OK although very inefficient. You have a never ending loop in which you redeclare the array and 'i' over and over again, it would be more sensible to move the two linesoutside the loop as there is no need to repeat them. Move them to either before the "void main()" or on the line after it.Code:unsigned int ADCSamples[3]; unsigned int i ;
Brian.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 unsigned int ADCSamples[3]; unsigned int first =0 ; unsigned int ref,ref1; unsigned int ADC_value; // Main function void main() { InitADC(AN2); // Initialize GP2 as ADC input pin CHANNEL 2 while(1) { // ADC_value = GetADCValue(AN2); // Read ADC value from pin GP2 pin __delay_ms(2000); if(first==0) { ADC_value = GetADCValue(AN2); ref=ADC_value; __delay_ms(50); first++; } if(first==1) { ADC_value = GetADCValue(AN2); __delay_ms(50); ref1=ADC_value; } if(ref==ref1) { LED=1; LED1=1; first=0; } else if(ref<=ref1) { LED=1; LED=0; } else { LED=0; LED1=0; } } }
#define mVLimit 0.03
#define AveragingTime 2000 (2 Secs) mS time to take average over
#define SampleRate 10 mS between averaging samples
#define Tolerance = (mVLimit*1024)/5 //the ADC value for 30mV
#define NumberOfSamples AveragingTime/SampleRate
#define OFF 0
#define ON 1
unsigned int ADCSamples[NumberOfSamples];
unsigned int ADC_Value;
unsigned int ADC_Total, ADC_Average;
unsigned int UpperLimit, LowerLimit;
unsigned char index = 0;
// Main function
void main()
{
RedLED = OFF;
GreenLED = OFF;
InitADC(AN2); // Initialize GP2 as ADC input pin CHANNEL 2
ADC_Value = GetADCValue(AN2); // need a first value to make calculation
// get 20 consecutive samples (2 seconds at 10mS intervals) within 'Tolerance' of each other.
do
{
__delay_ms(SampleRate);
ADCSamples[index] = GetADCValue(AN2); // get next value
if((ADCSamples[index] > ADC_Value + Tolerance) || (ADCSamples[index] < ADC_Value - Tolerance)) index = 0;
else index++;
}while(index < NumberOfSamples);
// now find their average
index = NumberOfSamples;
do
{
ADC_Total += ADC_Samples[index--];
}while(index > 0);
ADC_Average = ADC_Total / NumberOfSamples;
RedLED = ON; // show the average has been found
// now loop to see if the present result is close to the average
UpperLimit = ADC_Average + Tolerance;
LowerLimit = ADC_Average - Tolerance;
while(1)
{
ADC_Value = GetADCValue(AN2);
if((ADC_Value > UpperLimit) || (ADC_Value < LowerLimit)) GreenLED = OFF;
else GreenLED = ON;
__delay_ms(SampleRate);
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?