Main Page User Manual Library Reference FAQ Alphabetical Index Example Projects

delay_basic.h
Go to the documentation of this file.
1 /* Copyright (c) 2002, Marek Michalkiewicz
2  Copyright (c) 2007 Joerg Wunsch
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright
9  notice, this list of conditions and the following disclaimer.
10 
11  * Redistributions in binary form must reproduce the above copyright
12  notice, this list of conditions and the following disclaimer in
13  the documentation and/or other materials provided with the
14  distribution.
15 
16  * Neither the name of the copyright holders nor the names of
17  contributors may be used to endorse or promote products derived
18  from this software without specific prior written permission.
19 
20  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  POSSIBILITY OF SUCH DAMAGE. */
31 
32 /* $Id: delay_basic.h 2143 2010-06-08 21:19:51Z joerg_wunsch $ */
33 
34 #ifndef _UTIL_DELAY_BASIC_H_
35 #define _UTIL_DELAY_BASIC_H_ 1
36 
37 #include <inttypes.h>
38 
39 #if !defined(__DOXYGEN__)
40 static inline void _delay_loop_1(uint8_t __count) __attribute__((always_inline));
41 static inline void _delay_loop_2(uint16_t __count) __attribute__((always_inline));
42 #endif
43 
44 /** \file */
45 /** \defgroup util_delay_basic <util/delay_basic.h>: Basic busy-wait delay loops
46  \code
47  #include <util/delay_basic.h>
48  \endcode
49 
50  The functions in this header file implement simple delay loops
51  that perform a busy-waiting. They are typically used to
52  facilitate short delays in the program execution. They are
53  implemented as count-down loops with a well-known CPU cycle
54  count per loop iteration. As such, no other processing can
55  occur simultaneously. It should be kept in mind that the
56  functions described here do not disable interrupts.
57 
58  In general, for long delays, the use of hardware timers is
59  much preferrable, as they free the CPU, and allow for
60  concurrent processing of other events while the timer is
61  running. However, in particular for very short delays, the
62  overhead of setting up a hardware timer is too much compared
63  to the overall delay time.
64 
65  Two inline functions are provided for the actual delay algorithms.
66 
67 */
68 
69 /** \ingroup util_delay_basic
70 
71  Delay loop using an 8-bit counter \c __count, so up to 256
72  iterations are possible. (The value 256 would have to be passed
73  as 0.) The loop executes three CPU cycles per iteration, not
74  including the overhead the compiler needs to setup the counter
75  register.
76 
77  Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
78  can be achieved.
79 */
80 void
82 {
83  __asm__ volatile (
84  "1: dec %0" "\n\t"
85  "brne 1b"
86  : "=r" (__count)
87  : "0" (__count)
88  );
89 }
90 
91 /** \ingroup util_delay_basic
92 
93  Delay loop using a 16-bit counter \c __count, so up to 65536
94  iterations are possible. (The value 65536 would have to be
95  passed as 0.) The loop executes four CPU cycles per iteration,
96  not including the overhead the compiler requires to setup the
97  counter register pair.
98 
99  Thus, at a CPU speed of 1 MHz, delays of up to about 262.1
100  milliseconds can be achieved.
101  */
102 void
104 {
105  __asm__ volatile (
106  "1: sbiw %0,1" "\n\t"
107  "brne 1b"
108  : "=w" (__count)
109  : "0" (__count)
110  );
111 }
112 
113 #endif /* _UTIL_DELAY_BASIC_H_ */
void _delay_loop_2(uint16_t __count)
Definition: delay_basic.h:103
unsigned char uint8_t
Definition: stdint.h:79
void _delay_loop_1(uint8_t __count)
Definition: delay_basic.h:81
unsigned int uint16_t
Definition: stdint.h:89

Automatically generated by Doxygen 1.8.7 on Tue Jul 8 2014. Dash Docset conversion by Matt Kane