AVR Libc Home Page | AVR Libc Development Pages | ||||
Main Page | User Manual | Library Reference | FAQ | Alphabetical Index | Example Projects |
00001 /* Copyright (c) 2002, Marek Michalkiewicz 00002 Copyright (c) 2007 Joerg Wunsch 00003 All rights reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are met: 00007 00008 * Redistributions of source code must retain the above copyright 00009 notice, this list of conditions and the following disclaimer. 00010 00011 * Redistributions in binary form must reproduce the above copyright 00012 notice, this list of conditions and the following disclaimer in 00013 the documentation and/or other materials provided with the 00014 distribution. 00015 00016 * Neither the name of the copyright holders nor the names of 00017 contributors may be used to endorse or promote products derived 00018 from this software without specific prior written permission. 00019 00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00024 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00030 POSSIBILITY OF SUCH DAMAGE. */ 00031 00032 /* $Id: delay_basic.h 2143 2010-06-08 21:19:51Z joerg_wunsch $ */ 00033 00034 #ifndef _UTIL_DELAY_BASIC_H_ 00035 #define _UTIL_DELAY_BASIC_H_ 1 00036 00037 #include <inttypes.h> 00038 00039 #if !defined(__DOXYGEN__) 00040 static inline void _delay_loop_1(uint8_t __count) __attribute__((always_inline)); 00041 static inline void _delay_loop_2(uint16_t __count) __attribute__((always_inline)); 00042 #endif 00043 00044 /** \file */ 00045 /** \defgroup util_delay_basic <util/delay_basic.h>: Basic busy-wait delay loops 00046 \code 00047 #include <util/delay_basic.h> 00048 \endcode 00049 00050 The functions in this header file implement simple delay loops 00051 that perform a busy-waiting. They are typically used to 00052 facilitate short delays in the program execution. They are 00053 implemented as count-down loops with a well-known CPU cycle 00054 count per loop iteration. As such, no other processing can 00055 occur simultaneously. It should be kept in mind that the 00056 functions described here do not disable interrupts. 00057 00058 In general, for long delays, the use of hardware timers is 00059 much preferrable, as they free the CPU, and allow for 00060 concurrent processing of other events while the timer is 00061 running. However, in particular for very short delays, the 00062 overhead of setting up a hardware timer is too much compared 00063 to the overall delay time. 00064 00065 Two inline functions are provided for the actual delay algorithms. 00066 00067 */ 00068 00069 /** \ingroup util_delay_basic 00070 00071 Delay loop using an 8-bit counter \c __count, so up to 256 00072 iterations are possible. (The value 256 would have to be passed 00073 as 0.) The loop executes three CPU cycles per iteration, not 00074 including the overhead the compiler needs to setup the counter 00075 register. 00076 00077 Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds 00078 can be achieved. 00079 */ 00080 void 00081 _delay_loop_1(uint8_t __count) 00082 { 00083 __asm__ volatile ( 00084 "1: dec %0" "\n\t" 00085 "brne 1b" 00086 : "=r" (__count) 00087 : "0" (__count) 00088 ); 00089 } 00090 00091 /** \ingroup util_delay_basic 00092 00093 Delay loop using a 16-bit counter \c __count, so up to 65536 00094 iterations are possible. (The value 65536 would have to be 00095 passed as 0.) The loop executes four CPU cycles per iteration, 00096 not including the overhead the compiler requires to setup the 00097 counter register pair. 00098 00099 Thus, at a CPU speed of 1 MHz, delays of up to about 262.1 00100 milliseconds can be achieved. 00101 */ 00102 void 00103 _delay_loop_2(uint16_t __count) 00104 { 00105 __asm__ volatile ( 00106 "1: sbiw %0,1" "\n\t" 00107 "brne 1b" 00108 : "=w" (__count) 00109 : "0" (__count) 00110 ); 00111 } 00112 00113 #endif /* _UTIL_DELAY_BASIC_H_ */