using the fvr gets slightly better result [mine is a 3.3v system]
Code:mean 255 readings 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 3600 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 outliers 0000 mean 255 readings 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 3600 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 outliers 0000 mean 255 readings 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 3600 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 outliers 0000 mean 255 readings 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 3600 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 outliers 0000 mean 255 readings 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 3600 0000,0000,0000,0000,0000,0000,0000,0000,0000,0000 outliers 0000 mean 255
Code:#CONFIG __config _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON __config _CONFIG2, _MCLRE_ON & _PWRTE_OFF & _LPBOREN_OFF & _BOREN_ON & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_ON & _DEBUG_OFF __config _CONFIG3, _WDTCPS_WDTCPS_11 & _WDTE_ON & _WDTCWS_WDTCWS_7 & _WDTCCS_LFINTOSC __config _CONFIG4, _WRT_OFF & _SCANE_available & _LVP_ON __config _CONFIG5, _CP_OFF & _CPD_OFF #ENDCONFIG DEFINE OSC 32 DEFINE DEBUG_REG PORTD DEFINE DEBUG_BIT 2 ; if not used for pwr DEFINE DEBUG_BAUD 9600 DEFINE DEBUG_MODE 0 DEFINE ADC_BITS 10 ' 10-bit Analog to digital DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS ANSELA = % 00000001 ' Pin A0 = ADC TRISA = % 11101111 ' Pin A4 = LED TRISD = % 11111011 ' DEBUG LATD.2 = 1 ' DEBUG clear LED VAR LATA.4 i var byte j var word bucket var WORD ADCinput var WORD mean var WORD buckets var word[21] outlier var WORD LED = 1 'Proof of Life PAUSE 2000 DEBUG 13,10,"READY",13,10 FVRCON = % 10000010;2.048v ADCON0 = % 10000100 ADREF = % 00000011,fvr ADCLK = 16; faster clk LED=0 Mainloop: mean = 0 for i = 0 to 9 ADCIN 0, ADCinput mean = mean + ADCinput next mean = mean/40 DEBUG "mean ",DEC mean,13,10 for j = 0 to 3599 ADCIN 0, ADCinput ADCinput=ADCinput>>2 bucket = mean-ADCinput i = bucket + 10 if i>20 then outlier = outlier + 1 else buckets[i]=buckets[i] + 1 endif 'DEBUG 13,10,"mean ",DEC mean," i ",dec i," read ",dec ADCinput,13,10 PAUSE 5 next DEBUG "readings " ,13,10 DEBUG dec4 buckets[0],",", dec4 buckets[1],"," , dec4 buckets[2],",", dec4 buckets[3],"," , dec4 buckets[4],"," DEBUG dec4 buckets[5],",", dec4 buckets[6],"," , dec4 buckets[7],",", dec4 buckets[8],"," , dec4 buckets[9]," " DEBUG dec4 buckets[10]," " DEBUG dec4 buckets[11],",", dec4 buckets[12],"," , dec4 buckets[13],",", dec4 buckets[14],"," , dec4 buckets[15],"," DEBUG dec4 buckets[16],",", dec4 buckets[17],"," , dec4 buckets[18],",", dec4 buckets[19],"," , dec4 buckets[20],13,10 DEBUG "outliers ",dec4 outlier DEBUG 13,10 for i = 0 to 20 buckets[i] = 0 next outlier = 0 GOTO Mainloop end


Reply With Quote

Bookmarks