please dont rip this site

PIC Microcontoller IO Method

Pulse Width Measurement

by Scott Dattalo

;-----------------------------------------------
;pulse width measurements with 3 Tcyc resolution
;
;The purpose of this routine is to accurately measure
;the width of a pulse. The resolution is 3 instruction
;cycles and the dynamic range is 19 bits or 3*2^19 cycles.
;(That's 1,572,864 cycles which is approximately pi/10
;seconds on a 20Mhz pic.)
;
;

        btfsc   ioport,iobit
         goto   $-1

loop
   btfsc ioport,iobit   ;If the pulse is over
    goto high0          ;then adjust the count
                        ;
   movlw 1              ;Otherwise, intertwine the counter
                        ;incrementing with the pulse checking
   btfsc ioport,iobit   ;
    goto high1          ;
                        ;
   addwf lo,f           ;Increment the lo byte
                        ;
   btfsc ioport,iobit   ;
    goto high2          ;
                        ;
   rlf   upper,w        ;Pick up the carry (if lo byte
                        ;overflowed)
   btfsc ioport,iobit   ;
    goto high3          ;
                        ;
   addwf hi,f           ;Increment the high byte
                        ;
   btfsc ioport,iobit   ;
    goto high4          ;
                        ;
   skpc                 ;If the high byte rolls over
    btfsc ioport,iobit  ;or the pulse is over
     goto high5_or_done ;then get out of the loop
                        ;
   clrwdt               ;Could be a nop or some inst.
                        ;
   btfsc ioport,iobit   ;
    goto high6          ;
                        ;
   nop                  ;
                        ;
   btfss ioport,iobit   ;Note that we check the opp. level
    goto loop           ;

  ;fall through... Add 7 to the total count

   incf  upper,f

high6:
   incf  upper,f

high5_or_done:
   skpnc            ;If c=1 then we have an
    goto overflow   ;overflow

   incf  upper,f

high4:
   incf  upper,f

high3:
   incf  upper,f

high2:
   decf  lo,f       ;Get rid of the extra
                    ;increment of the lo byte
   incf  upper,f

high1:
   incf  upper,f

high0:


   rlf   upper,f

   rlf   lo,f
   rlf   hi,f
   rlf   upper,f

   rlf   lo,f
   rlf   hi,f
   rlf   upper,f

   rlf   lo,f
   rlf   hi,f
   rlf   upper,f

   swapf upper,w
   andlw 7
   iorwf lo,f

   movlw 7
   andwf upper,f

   retlw 0

overflow
   ;If we get here, then there was an overflow.
   ;it turns out that all three bytes of the
   ;counter are zero. Decrementing all three
   ;will set them to 0xff.

   decf  upper,f
   decf  hi,f
   decf  lo,f

   retlw 0xff

Dwayne Reid [dwayner at planet.eon.net] of Trinity Electronics Systems Ltd Edmonton, AB, CANADA says

BTW: the code works VERY well. Its being used to measure the pulse duty cycle ratio from Analog Devices TMP03 / 04 temperature sensors and can resolve down to fractins of a degree (far more accuracy than the sensor). I cleaned the normalize part of the routine up slightly as follows at the end of this message.

The entire measurement routine (wait for HI, wait for LO, measure LO, measure HI, test for noisy signal, normalize everything, multiply HI pulse by constant, divide by LO pulse, average 16 samples) takes about 400 code spaces - left me lots of room for the rest of the project in a 12c508.

;up to 19 bit pulse timer with 3 cycle resolution
;concept by Scott Dattalo, this version by Dwayne Reid

;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

;normalize high period
     movfw       MH_high         ;get LSB count into w
     clrf        MH_high         ;

     clrc                        ;make room for lower 3 LSBs
     rlf         MH_low,F        ;  (shift everything to the left 3 bits)
     rlf         MH_mid,F
     rlf         MH_high,F       ;

     rlf         MH_low,F
     rlf         MH_mid,F
     rlf         MH_high,F

     rlf         MH_low,F
     rlf         MH_mid,F
     rlf         MH_high,F       ;none of this affects LSB count in w

     iorwf       MH_low,F        ;put LSBs into low byte

;done!


Dwayne Reid   <dwayner@planet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, AB, CANADA
(780) 489-3199 voice          (780) 487-6397 fax

Celebrating 16 years of Engineering Innovation (1984 - 2000)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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.


file: /Techref/microchip/pulsewidth-sd.htm, 6KB, , updated: 2002/11/1 18:03, local time: 2024/12/26 08:56,
TOP NEW HELP FIND: 
18.188.218.140:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://ecomorder.com/Techref/microchip/pulsewidth-sd.htm"> PIC Microcontoller IO Method - Pulse Width Measurement</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .