EDF - OnePIC MCU
v1.1.0
|
00001 00011 #include "OnePIC_pot.h" 00012 #include "TimeDelay.h" 00013 #include "bsp.h" 00014 #include "OnePIC_int.h" 00015 #include "Compiler.h" 00016 00017 #define FILTER_MEDIAN_LENGTH 3 00018 #define FILTER_MIDDLE_VALUE (FILTER_MEDIAN_LENGTH-1)/2 00019 00020 00030 static int comp(const void *e1, const void *e2) 00031 { 00032 const int * a1 = e1; 00033 const int * a2 = e2; 00034 if (*a1 < *a2) 00035 return -1; 00036 else if (*a1 == *a2) 00037 return 0; 00038 else 00039 return 1; 00040 } 00044 void EDF_potInit(void) 00045 { 00046 } 00047 00053 uint16_t filterMedian(uint16_t d) 00054 { 00055 static uint16_t mVal[FILTER_MEDIAN_LENGTH]; 00056 static uint8_t mIndex = 0; 00057 00058 mVal[mIndex++] = d; 00059 00060 if(mIndex == FILTER_MEDIAN_LENGTH) { 00061 mIndex = 0; 00062 } 00063 00064 qsort(mVal, FILTER_MEDIAN_LENGTH, sizeof(uint16_t), comp); 00065 00066 return mVal[FILTER_MIDDLE_VALUE]; 00067 } 00068 00073 uint16_t EDF_potRead(void) 00074 { 00075 uint16_t filteredPotReading; 00076 AD1CON1bits.ON = 0; 00077 TRISBbits.TRISB11 = 1; 00078 asm("di"); // disable interrupts 00079 AD1CHSbits.CH0SA = 11; 00080 AD1CON1bits.ON = 1; 00081 //Bring up ADC in known state 00082 AD1CON1bits.SAMP = 1; //Start Chold charging Process 00083 asm("nop"); asm("nop"); asm("nop"); asm("nop"); 00084 asm("nop"); asm("nop"); asm("nop"); asm("nop"); 00085 asm("nop"); asm("nop"); asm("nop"); asm("nop"); 00086 asm("nop"); asm("nop"); asm("nop"); asm("nop"); 00087 AD1CON1bits.SAMP = 0; //Start ADC conversion 00088 asm("ei"); //re-enable interrupts 00089 while ( !AD1CON1bits.DONE ); 00090 AD1CON1bits.DONE = 0; 00091 filteredPotReading = ADC1BUF0; 00092 return filteredPotReading; 00093 }