AVR-LibC
2.3.0
Standard C library for AVR-GCC
AVR-LibC Manual
AVR-LibC Sources
Main Page
User Manual
Library Reference
FAQ
Example Projects
Index
Loading...
Searching...
No Matches
source
avr-libc-onlinedocs
include
util
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
#include <bits/attribs.h>
37
38
#if !defined(__DOXYGEN__)
39
static
__ATTR_ALWAYS_INLINE__
void
_delay_loop_1
(
uint8_t
__count);
40
static
__ATTR_ALWAYS_INLINE__
void
_delay_loop_2
(
uint16_t
__count);
41
#endif
42
43
/** \file */
44
/** \defgroup util_delay_basic <util/delay_basic.h>: Basic busy-wait delay loops
45
\code
46
#include <util/delay_basic.h>
47
\endcode
48
49
The functions in this header file implement simple delay loops
50
that perform a busy-waiting. They are typically used to
51
facilitate short delays in the program execution. They are
52
implemented as count-down loops with a well-known CPU cycle
53
count per loop iteration. As such, no other processing can
54
occur simultaneously. It should be kept in mind that the
55
functions described here do not disable interrupts.
56
57
In general, for long delays, the use of hardware timers is
58
much preferable, as they free the CPU, and allow for
59
concurrent processing of other events while the timer is
60
running. However, in particular for very short delays, the
61
overhead of setting up a hardware timer is too much compared
62
to the overall delay time.
63
64
Two inline functions are provided for the actual delay algorithms.
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, delays of up to 768 / f<sub>CPU</sub> microseconds can be achieved,
76
where f<sub>CPU</sub> denotes the CPU speed in units of 1 MHz.
77
78
As an alternative, consider <a
79
href="https://gcc.gnu.org/onlinedocs/gcc/AVR-Built-in-Functions.html#index-_005f_005fbuiltin_005favr_005fdelay_005fcycles"
80
><tt>__builtin_avr_delay_cycles</tt></a> which allows to specify the cycle
81
count and can implement delays of up to 71.5 / f<sub>CPU</sub> seconds.
82
*/
83
void
84
_delay_loop_1
(
uint8_t
__count)
85
{
86
__asm__
volatile
(
87
"1: dec %0"
"\n\t"
88
"brne 1b"
89
:
"=r"
(__count)
90
:
"0"
(__count)
91
);
92
}
93
94
/** \ingroup util_delay_basic
95
96
Delay loop using a 16-bit counter \c __count, so up to 65536
97
iterations are possible. (The value 65536 would have to be
98
passed as 0.) The loop executes four CPU cycles per iteration,
99
not including the overhead the compiler requires to setup the
100
counter register pair.
101
102
Thus, delays of up to 262.1 / f<sub>CPU</sub> milliseconds can be achieved,
103
where f<sub>CPU</sub> denotes the CPU speed in units of 1 MHz.
104
105
As an alternative, consider <a
106
href="https://gcc.gnu.org/onlinedocs/gcc/AVR-Built-in-Functions.html#index-_005f_005fbuiltin_005favr_005fdelay_005fcycles"
107
><tt>__builtin_avr_delay_cycles</tt></a> which allows to specify the cycle
108
count and can implement delays of up to 71.5 / f<sub>CPU</sub> seconds.
109
*/
110
void
111
_delay_loop_2
(
uint16_t
__count)
112
{
113
#if defined (__AVR_TINY__)
114
__asm__
volatile
(
115
"1: subi %A0,1"
"\n\t"
116
" sbci %B0,0"
"\n\t"
117
"brne 1b"
118
:
"+d"
(__count)
119
);
120
#else
121
__asm__
volatile
(
122
"1: sbiw %0,1"
"\n\t"
123
"brne 1b"
124
:
"+w"
(__count)
125
);
126
#endif
/* AVR_TINY */
127
}
128
129
#endif
/* _UTIL_DELAY_BASIC_H_ */
uint16_t
unsigned int uint16_t
Definition:
stdint.h:96
uint8_t
unsigned char uint8_t
Definition:
stdint.h:88
_delay_loop_1
void _delay_loop_1(uint8_t __count)
Definition:
delay_basic.h:84
_delay_loop_2
void _delay_loop_2(uint16_t __count)
Definition:
delay_basic.h:111
inttypes.h
Generated on Sun Dec 28 2025 13:38:37 for AVR-LibC by
1.9.6