[warning: untested code ahead] ; Find the minimum and maximum of a vector (array) of numbers ; ; Input: FSR points to first byte in the array ; W number of elements in the array (>= 1) ; ; Output: ; HIPTR pointer to largest element in the array ; LOPTR pointer to smallest element in the array ; HIVAL value of largest element in the array ; LOVAL value of smallest element in the array ; FSR points just past the array ; W destroyed ; ; Note: ; If more than one value in the array is the largest (or smallest) ; then HIPTR (LOPTR) will point to the last such element. ; ; Uses: COUNT ; get_min_max: movwf COUNT ; size of array clrf HIVAL ; use 0 as highest value so far clrf LOVAL ; use 255 as lowest value so far decf LOVAL loop: incf FSR ; point to next slot in array ; if INDF >= HIVAL ; HIVAL = INDF ; HIPTR = FSR ; end if movf HIVAL,W ; Compute INDF-HIVAL and set carry if subwf INDF,W ; ...INDF is greater or equal to HIVAL skpc goto not_higher movf INDF,W movwf HIVAL movf FSR,W movwf HIPTR not_higher: ; if INDF <= LOVAL ; LOVAL = INDF ; LOPTR = FSR ; end if movf INDF,W ; Compute LOVAL-INDF and set carry if subwf LOVAL,W ; ...LOVAL is greater than or equal to INDF skpc goto not_lower movf INDF,W movwf LOVAL movf FSR,W movwf LOPTR not_lower: ; See if there are any more to do decfsz COUNT goto loop return ;=============================================== You can call this routine like this: CBLOCK ADC_0 ADC_1 ADC_2 ADC_3 ENDC movlw ADC_0 ; point to first ADC's value movwf FSR movlw 4 ; number of values call get_min_max ; Now, lets say we want to know which ADC was lowest: movf LOPTR,W ; pointer to lowest addlw 0-ADC0 ; minus pointer to first one W is now 0,1,2,3 ; And similarly for highest Bob Ammerman RAm Systems (contract development of high performance, high function, low-level software) -- http://www.piclist.com hint: PICList Posts must start with ONE topic: [PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads