1 |
/* Copyright (c) 2002, Marek Michalkiewicz |
/* Copyright (c) 2002, Marek Michalkiewicz |
2 |
Copyright (c) 2004, Joerg Wunsch |
Copyright (c) 2004,2005 Joerg Wunsch |
3 |
All rights reserved. |
All rights reserved. |
4 |
|
|
5 |
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
47 |
#include <avr/delay.h> |
#include <avr/delay.h> |
48 |
\endcode |
\endcode |
49 |
|
|
50 |
|
\note As an alternative method, it is possible to pass the |
51 |
|
F_CPU macro down to the compiler from the Makefile. |
52 |
|
Obviously, in that case, no \c #define statement should be |
53 |
|
used. |
54 |
|
|
55 |
The functions in this header file implement simple delay loops |
The functions in this header file implement simple delay loops |
56 |
that perform a busy-waiting. They are typically used to |
that perform a busy-waiting. They are typically used to |
57 |
facilitate short delays in the program execution. They are |
facilitate short delays in the program execution. They are |
75 |
operate on double typed arguments, however when optimization is |
operate on double typed arguments, however when optimization is |
76 |
turned on, the entire floating-point calculation will be done at |
turned on, the entire floating-point calculation will be done at |
77 |
compile-time. |
compile-time. |
78 |
|
|
79 |
|
\note When using _delay_us() and _delay_ms(), the expressions |
80 |
|
passed as arguments to these functions shall be compile-time |
81 |
|
constants, otherwise the floating-point calculations to setup the |
82 |
|
loops will be done at run-time, thereby drastically increasing |
83 |
|
both the resulting code size, as well as the time required to |
84 |
|
setup the loops. |
85 |
*/ |
*/ |
86 |
|
|
87 |
|
#if !defined(DOXYGEN) |
88 |
|
static void _delay_loop_1(uint8_t __count) __attribute__((always_inline)); |
89 |
|
static void _delay_loop_2(uint16_t __count) __attribute__((always_inline)); |
90 |
|
static void _delay_us(double __us) __attribute__((always_inline)); |
91 |
|
static void _delay_ms(double __ms) __attribute__((always_inline)); |
92 |
|
#endif |
93 |
|
|
94 |
/** \ingroup avr_delay |
/** \ingroup avr_delay |
95 |
|
|
96 |
Delay loop using an 8-bit counter \c __count, so up to 256 |
Delay loop using an 8-bit counter \c __count, so up to 256 |
102 |
Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds |
Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds |
103 |
can be achieved. |
can be achieved. |
104 |
*/ |
*/ |
105 |
static __inline__ void |
void |
106 |
_delay_loop_1(uint8_t __count) |
_delay_loop_1(uint8_t __count) |
107 |
{ |
{ |
108 |
__asm__ volatile ( |
__asm__ volatile ( |
124 |
Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 |
Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 |
125 |
milliseconds can be achieved. |
milliseconds can be achieved. |
126 |
*/ |
*/ |
127 |
static __inline__ void |
void |
128 |
_delay_loop_2(uint16_t __count) |
_delay_loop_2(uint16_t __count) |
129 |
{ |
{ |
130 |
__asm__ volatile ( |
__asm__ volatile ( |
151 |
|
|
152 |
The maximal possible delay is 768 us / F_CPU in MHz. |
The maximal possible delay is 768 us / F_CPU in MHz. |
153 |
*/ |
*/ |
154 |
static __inline__ void |
void |
155 |
_delay_us(double __us) |
_delay_us(double __us) |
156 |
{ |
{ |
157 |
uint8_t __ticks; |
uint8_t __ticks; |
176 |
|
|
177 |
The maximal possible delay is 262.14 ms / F_CPU in MHz. |
The maximal possible delay is 262.14 ms / F_CPU in MHz. |
178 |
*/ |
*/ |
179 |
static __inline__ void |
void |
180 |
_delay_ms(double __ms) |
_delay_ms(double __ms) |
181 |
{ |
{ |
182 |
uint16_t __ticks; |
uint16_t __ticks; |