On Sat, 14 Oct 2000, Scott Dattalo wrote: > Also, as a challenge, what do you think this does: > > > clrc > btfsc IOPORT,IOBIT > setc > > rlf x,w > andlw 0xf > movwf x > > skpnc > bsf x,7 > > xorlw 0xf > > skpz > return > > btfsc x,7 > return > > incf pulse_counter,f > > bsf x,7 > > return > > ? Since no one answered, I follow up. This code performs the pulse counting and filtering like the others. However, upon closer examination I found a couple of subtle bugs. (That's why no one could figure how it worked :). Here's the comment and presumably correct, (but untested) version: cblock sample ; The most significant bit contains the most recently filtered ;state. The other bits contain the last N samples, where N is ;the filter width. (and N must be less than 8). pulse_counter ; each time a pulse is discovered, this is incremented endc ;-------------------------- ; constants: ; ; Number of consecutive samples of the same state required to declare ;an input as filtered #define STEADY_STATE_SAMPLES 4 ; The FILTER_MASK is a constant with the least significant ;STEADY_STATE_SAMPLES bits set. e.g. if STEADY_STATE_SAMPLES is 4 ;then the FILTER_MASK is (1<<4)-1 = 16-1 = 15 = 00001111b #define FILTER_MASK ( (1 << STEADY_STATE_SAMPLES) - 1) count_pulses: clrc ;Copy the I/O pin state into the carry btfsc IOPORT,IOBIT ;First, it's assumed the io pin is low setc ;If it isn't then we set the carry bit ;NOTE, if the I/O pin is either the most ;or least significant bit, then a rlf ;or rrf instruction accomplishes the ;same thing in 2 fewer cycles. ; The next 4 instructions copy the new sample to the lsb and shifts ;the previous samples left one position. However, the msb (which ;contains the filtered state) is left unchanged. ; starting with sample == abcdefg and C=s (carry is equal to newest ;sample): rlf sample,f ; bcdefghs C=a rlf sample,f ; cdefghsa C=b rrf sample,w ; cdefghsa C=a (W contains bcdefghs) rrf sample,f ; acdefghs C=a andlw FILTER_MASK ;examine the last N consecutive samples skpnz ;If they're all zero bcf sample,7 ; then the filtered state is low. xorlw FILTER_MASK ;But we're really interested in highs ;If we complement all of the bits ;and the result is zero, then that ;means the last N samples were high. skpnz ;If any of the last N were low or if btfsc sample,7 ; they're all high but the filter is return ; already high, then we're done ;If we get here then a positive pulse has been detected incf pulse_counter,f ;count it ; bsf sample,7 ;Set the "filtered" flag return Scott -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! use mailto:listserv@mitvma.mit.edu?body=SET%20PICList%20DIGEST