At 09:17 AM 3/31/2006, alan smith wrote: >So not to reinvent the wheel OR see a better way of doing it....is >this code out in the public domain somewhere? > >Dwayne Reid wrote: >There exists code that will measure an incoming pulse width with 3 >cycle resolution. That's 3 us resolution based on your mention of 4 >MHz clock frequency. The timer counter is max 19 bits but can be >shortened easily. > >In other words, you can measure a pulse of up to about 1.5 seconds >with 3 us resolution. ;the following code measures both the high and low portions of an incoming pulse stream. ;The counters are up to 19 bits wide (compile-time selectable) and measures in 3 cycle increments. ;This allows you to tailor the time-out period to something appropriate for the application. ;This code is the result of a request that I posted to the piclist way back when - Andrew Warren, Reggie Bergin, Scott ;Dattalo all came up with great suggestions. As seems happen frequently, Scott's solution turned out to provide the ;best "bang for the buck" in terms of performance. Many thanks, Scott! Dwayne Reid ;Also note that the 5-cycle-resolution routine posted by Andrew Warren is much, much tinier than this and might also ;be suitable. Ask and ye shall receive. ;insert code here that looks for HI to LO transition. MeasLo ;measure LO period ;up to 19 bit counter with 3 cycle resolution ;concept by Scott Dattalo, this version by Dwayne Reid clrf ML_low clrf ML_mid clrf ML_high ;used as known zero for main loop ML_loop btfsc PULSE goto ML_1st movlw 1 btfsc PULSE goto ML_2nd addwf ML_low,F btfsc PULSE goto ML_3rd rlf ML_high,W ;get C into W lsb (add 0 or 1 to next byte) btfsc PULSE goto ML_4th addwf ML_mid,F ;add previous carry btfsc PULSE goto ML_5th ;use either line below (not both) btfss ML_mid,5 ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpc = 19 ; skpc ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpc = 19 btfsc PULSE goto ML_6th nop ;spare cycle! btfsc PULSE goto ML_7th clrwdt btfss PULSE goto ML_loop ML_8th incf ML_high,F ;ML_high now used to accumulate LSBs ML_7th incf ML_high,F ML_6th incf ML_high,F ML_5th ;use either line below (not both) btfsc ML_mid,5 ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpnc =19 ; skpnc ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpnc =19 goto overflow ; incf ML_high,F ML_4th incf ML_high,F ML_3rd decf ML_low,F ;undo increment incf ML_high,F ML_2nd incf ML_high,F ML_1st ;now measure HI period clrf MH_low clrf MH_mid clrf MH_high ;used as known zero for main loop MH_loop btfss PULSE goto MH_1st movlw 1 btfss PULSE goto MH_2nd addwf MH_low,F btfss PULSE goto MH_3rd rlf MH_high,W ;get C into W lsb (add 0 or 1 to next byte) btfss PULSE goto MH_4th addwf MH_mid,F ;add previous carry btfss PULSE goto MH_5th ;use either line below (not both) btfss MH_mid,5 ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpc = 19 ; skpc ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpc = 19 btfss PULSE goto MH_6th nop ;spare cycle! btfss PULSE goto MH_7th clrwdt btfsc PULSE goto MH_loop MH_8th incf MH_high,F ;MH_high now used to accumulate LSBs MH_7th incf MH_high,F MH_6th incf MH_high,F MH_5th ;use either line below (not both) btfsc MH_mid,5 ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpnc =19 ; skpnc ;5 = 16 bits; 6 = 17 bits; 7 = 18; skpnc =19 goto overflow ; subwf MH_mid,F ;undo increment, if any incf MH_high,F MH_4th incf MH_high,F MH_3rd decf MH_low,F ;undo increment incf MH_high,F MH_2nd incf MH_high,F MH_1st clrc rlf MH_low,F ;make room for lower 3 LSBs rlf MH_mid,F rlf MH_high,F ;LSB count gets shifted safely out of the way rlf MH_low,F rlf MH_mid,F rlf MH_high,F rlf MH_low,F rlf MH_mid,F rlf MH_high,F rlf MH_high,F ;get LSB count into w swapf MH_high,w andlw b'00000111' rrf MH_high,F iorwf MH_low,F ;put LSBs into low byte movlw b'00000111' ;done with LSB count bits andwf MH_high,F ;done! ;now mormalize low period clrc rlf ML_low,F ;make room for lower 3 LSBs rlf ML_mid,F rlf ML_high,F ;LSB count gets shifted safely out of the way rlf ML_low,F rlf ML_mid,F rlf ML_high,F rlf ML_low,F rlf ML_mid,F rlf ML_high,F rlf ML_high,F ;get LSB count into w swapf ML_high,w andlw b'00000111' rrf ML_high,F iorwf ML_low,F ;put LSBs into low byte movlw b'00000111' ;done with LSB count bits andwf ML_high,F ;done -- Dwayne Reid Trinity Electronics Systems Ltd Edmonton, AB, CANADA (780) 489-3199 voice (780) 487-6397 fax Celebrating 22 years of Engineering Innovation (1984 - 2006) .-. .-. .-. .-. .-. .-. .-. .-. .-. .- `-' `-' `-' `-' `-' `-' `-' `-' `-' Do NOT send unsolicited commercial email to this email address. This message neither grants consent to receive unsolicited commercial email nor is intended to solicit commercial email. -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist