AVR-LibC  2.3.0git
Standard C library for AVR-GCC
 

AVR-LibC Documen­tation

AVR-LibC Development Pages

Main Page

User Manual

Library Refe­rence

FAQ

Example Projects

File List

Index

Loading...
Searching...
No Matches
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#ifndef _UTIL_DELAY_BASIC_H_
33#define _UTIL_DELAY_BASIC_H_ 1
34
35#include <inttypes.h>
36
37#if !defined(__DOXYGEN__)
38static __inline__ void _delay_loop_1(uint8_t __count) __attribute__((__always_inline__));
39static __inline__ void _delay_loop_2(uint16_t __count) __attribute__((__always_inline__));
40#endif
41
42/** \file */
43/** \defgroup util_delay_basic <util/delay_basic.h>: Basic busy-wait delay loops
44 \code
45 #include <util/delay_basic.h>
46 \endcode
47
48 The functions in this header file implement simple delay loops
49 that perform a busy-waiting. They are typically used to
50 facilitate short delays in the program execution. They are
51 implemented as count-down loops with a well-known CPU cycle
52 count per loop iteration. As such, no other processing can
53 occur simultaneously. It should be kept in mind that the
54 functions described here do not disable interrupts.
55
56 In general, for long delays, the use of hardware timers is
57 much preferable, as they free the CPU, and allow for
58 concurrent processing of other events while the timer is
59 running. However, in particular for very short delays, the
60 overhead of setting up a hardware timer is too much compared
61 to the overall delay time.
62
63 Two inline functions are provided for the actual delay algorithms.
64
65*/
66
67/** \ingroup util_delay_basic
68
69 Delay loop using an 8-bit counter \c __count, so up to 256
70 iterations are possible. (The value 256 would have to be passed
71 as 0.) The loop executes three CPU cycles per iteration, not
72 including the overhead the compiler needs to setup the counter
73 register.
74
75 Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
76 can be achieved.
77*/
78void
80{
81 __asm__ volatile (
82 "1: dec %0" "\n\t"
83 "brne 1b"
84 : "=r" (__count)
85 : "0" (__count)
86 );
87}
88
89/** \ingroup util_delay_basic
90
91 Delay loop using a 16-bit counter \c __count, so up to 65536
92 iterations are possible. (The value 65536 would have to be
93 passed as 0.) The loop executes four CPU cycles per iteration,
94 not including the overhead the compiler requires to setup the
95 counter register pair.
96
97 Thus, at a CPU speed of 1 MHz, delays of up to about 262.1
98 milliseconds can be achieved.
99 */
100void
102{
103#if defined (__AVR_TINY__)
104 __asm__ volatile (
105 "1: subi %A0,1" "\n\t"
106 " sbci %B0,0" "\n\t"
107 "brne 1b"
108 : "+d" (__count)
109 );
110#else
111 __asm__ volatile (
112 "1: sbiw %0,1" "\n\t"
113 "brne 1b"
114 : "+w" (__count)
115 );
116#endif /* AVR_TINY */
117}
118
119#endif /* _UTIL_DELAY_BASIC_H_ */
unsigned int uint16_t
Definition: stdint.h:91
unsigned char uint8_t
Definition: stdint.h:81
void _delay_loop_1(uint8_t __count)
Definition: delay_basic.h:79
void _delay_loop_2(uint16_t __count)
Definition: delay_basic.h:101