PIC24 Support Libraries
pic24_delay.h
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 // Documentation for this file. If the \file tag isn't present,
31 // this file won't be documented.
32 /** \file
33  * Provides simple software delays. They are not cycle-accurate, however.
34  * These routines are integrated with a \ref doHeartbeat "heartbeat LED",
35  * so the heartbeat count is updated during a delay.
36  */
37 
38 #pragma once
39 
40 #include <stdint.h>
41 #include "pic24_util.h" // Need u32_heartbeatCount
42 #include <libpic30.h> // Has __delay32
43 
44 
45 
46 /** Delay the given number of processor clock cycles,
47  * then notify the heartbeat that time passed.
48  * Scale the time added to the heartbeat because
49  * we are not incurring the same
50  * overhead as if we repeatedly called
51  * <code>doHeartbeat()</code>.
52  * \param u32_cyc Number of processor clock cycles
53  * to delay.
54  */
55 inline static void delayAndUpdateHeartbeatCount(uint32_t u32_cyc) {
56  __delay32(u32_cyc);
57 #if USE_HEARTBEAT
58  u32_heartbeatCount += (u32_cyc >> 4);
59 #endif
60 }
61 
62 
63 /** A macro to delay the given number of milliseconds.
64  * The maximum delay is ~ 100+
65  * seconds when FCY = 40 MHz, because the underlying
66  * function \ref delayAndUpdateHeartbeatCount uses
67  * a uint32_t value for the number of processor clocks
68  * to delay.
69  * \param ms The number of milliseconds to delay.
70  */
71 #define DELAY_MS(ms) delayAndUpdateHeartbeatCount(CYCLES_PER_MS * ((uint32_t) (ms)));
72 
73 /** A macro to delay the given number of microseconds.
74  * \see \ref DELAY_MS for additional information.
75  * \param us The number of microseconds to delay.
76  */
77 #define DELAY_US(us) delayAndUpdateHeartbeatCount(CYCLES_PER_US * ((uint32_t) (us)));
_PERSISTENT uint32_t u32_heartbeatCount
Definition: pic24_util.c:71
static void delayAndUpdateHeartbeatCount(uint32_t u32_cyc)
Definition: pic24_delay.h:55