EDF - OnePIC MCU
v1.1.0
|
00001 00010 #include "OnePIC_pot.h" 00011 #include "TimeDelay.h" 00012 #include "bsp.h" 00013 #include "OnePIC_int.h" 00014 00015 #define FILTER_MEDIAN_LENGTH 3 00016 #define FILTER_MIDDLE_VALUE (FILTER_MEDIAN_LENGTH-1)/2 00017 00027 static int comp(const void *e1, const void *e2) 00028 { 00029 const int * a1 = e1; 00030 const int * a2 = e2; 00031 if (*a1 < *a2) 00032 return -1; 00033 else if (*a1 == *a2) 00034 return 0; 00035 else 00036 return 1; 00037 } 00038 00042 void EDF_potInit(void) 00043 { 00044 // _pbState = 1; 00045 AD1PCFGL &= ~0x10; // Clear PCFG8 00046 } 00047 00048 00054 uint16_t filterMedian(uint16_t d) 00055 { 00056 static uint16_t mVal[FILTER_MEDIAN_LENGTH]; 00057 static uint8_t mIndex = 0; 00058 00059 mVal[mIndex++] = d; 00060 00061 if(mIndex == FILTER_MEDIAN_LENGTH) { 00062 mIndex = 0; 00063 } 00064 00065 qsort(mVal, FILTER_MEDIAN_LENGTH, sizeof(uint16_t), comp); 00066 00067 return mVal[FILTER_MIDDLE_VALUE]; 00068 } 00073 uint16_t EDF_potRead(void) 00074 { 00075 uint16_t filteredPotReading; 00076 EDF_INT_SourceDisable ( INT_TMR_1 ); 00077 AD1CHS = POT_CHANNEL; 00078 AD1CON1bits.SAMP = 1; 00079 Delay10us(10); 00080 AD1CON1bits.SAMP = 0; 00081 while(!AD1CON1bits.DONE); 00082 filteredPotReading = /*filterMedian*/(ADC1BUF0); 00083 EDF_INT_SourceEnable ( INT_TMR_1 ); 00084 return filteredPotReading; 00085 } 00086