please dont rip this site

PIC Microcontoller Delay Method

0/6681

DelayUS() macro front-end for DelayTcy() by Mike McLaren, K8LH

Here's a simple DelayUS(microseconds) macro "front-end" for my DelayTcy routines. Remember to define or equate the "clock" variable as shown in the listing below.

Areas for improvement? How about parameter limit checking within the macro with gentle 'error' messages telling us where we went wrong?

;******************************************************************
;                                                                 *
;  DelayUS() Macro                  Mike McLaren, K8LH, June '07  *
;                                                                 *
;  the DelayUS() macro simply scales the "delay" parameter value  *
;  passed to your favorite DelayTcy routine and doesn't generate  *
;  any assembler instructions on its own.                         *
;                                                                 *
;  the 'usec' delay range for a 16-bit target DelayTcy() routine  *
;  is based on clock frequency;                                   *
;                                                                 *
;     4 MHz,  1   cycle /usec, 17..65551 usecs                    *
;     6 MHz,  1.5 cycles/usec, 12..43700 usecs                    *
;     8 MHz,  2   cycles/usec,  9..32775 usecs                    *
;    10 MHz,  2.5 cycles/usec,  7..26220 usecs                    *
;    12 MHz,  3   cycles/usec,  6..21850 usecs                    *
;    16 MHz,  4   cycles/usec,  5..16387 usecs                    *
;    20 MHz,  5   cycles/usec,  4..13110 usecs                    *
;    24 MHz,  6   cycles/usec,  3..10925 usecs                    *
;    28 MHz,  7   cycles/usec,  3.. 9364 usecs                    *
;    32 MHz,  8   cycles/usec,  3.. 8193 usecs                    *
;    36 MHz,  9   cycles/usec,  2.. 7283 usecs                    *
;    40 MHz, 10   cycles/usec,  2.. 6555 usecs                    *
;    48 MHz, 12   cycles/usec,  2.. 5462 usecs                    *
;                                                                 *
;  the DelayUS() macro requires the following two equates;        *
;                                                                 *
clock           equ   32        ; see list above
cycles_per_usec equ  (10000/(4000/clock))
;                                                                 *
;                                                                 *
;                                                                 *
;  DelayUS() macro                                                *
;                                                                 *
DelayUS macro   usecs           ; delay range: see list above
        DelayTcy((usecs*cycles_per_usec)/10)
        endm
;                                                                 *
;******************************************************************
;******************************************************************
;                                                                 *
;  DelayTcy(), 16..65551 Tcy         Mike McLaren, K8LH, June'07  *
;                                                                 *
;  requires the use of constant operands known at assembly time!  *
;                                                                 *
;  12 words, 1 RAM variable, 14-bit core                          *
;                            ^^^^^^^^^^^                          *
;  macro produces 4 instructions;                                 *
;                                                                 *
DelayTcy macro  delay           ; parameter range 16..65535
        movlw   ~(high(delay-16))
        movwf   TMRH
        movlw   low(delay-16)
        call    DelayLo-(delay%4)
        endm
;                                                                 *
;                                                                 *
;  example code for simulation testing;                           *
;                                                                 *
SimTest DelayUS(2048)           ; delay 2048 usecs
        nop                     ; put simulator break point here
;                                                                 *
;                                                                 *
;******************************************************************
        nop                     ; entry point for delay%4 == 3
        nop                     ; entry point for delay%4 == 2
        nop                     ; entry point for delay%4 == 1
DelayLo addlw   -4              ; subtract 4 cycle loop time
        skpnc                   ; borrow?  yes, skip, else
        goto    DelayLo         ; do another loop
        nop                     ;
DelayHi addlw   -4              ; subtract 4 cycle loop time
        incfsz  TMRH,F          ; done?  yes, skip, else
        goto    DelayLo         ; do another loop
        goto    $+1             ;
        return                  ;
;******************************************************************


file: /techref/microchip/delay/xus-mm.htm, 5KB, , updated: 2007/6/22 14:39, local time: 2008/7/4 13:45,
TOP NEW HELP FIND: 
38.103.63.16:LOG IN
©2008 PLEASE DON'T RIP! DO: LINK / DIGG! / MAKE!

 ©2008 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!
<A HREF="http://www.piclist.com/techref/microchip/delay/xus-mm.htm"> DelayUS() macro </A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be reviewed) Just type in the box and press the Post button. (HTML welcomed!): 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: 
Did you find what you needed?

  PICList 2008 contributors:
o List host: MIT, Site host massmind.org, Top posters @20080704 Apptech, Jinx, Xiaofan Chen, David VanHorn, Alan B. Pearce, Bob Axtell, William \Chops\ Westfield, Cedric Chang, Gerhard Fiedler, bean,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Shultz Electronics, Larry Williams, David VanHorn, Bryan Whitehouse, Timothy Weber, David Challis. Peter Todd. on-going support is MOST appreciated!
* Contributors: Neil Narwani, David Cary, Elemer AM Nyiry, Philip J Taylor, Gus Calabrese of Omegadogs.com, Gautama Venegas, Patrick B. Murphy, William Chops Westfield, Peter Todd, Leslie Ellis
  'What can I do?' - SiCKO

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .