AVR Libc Home Page AVRs AVR Libc Development Pages
Main Page User Manual Reference FAQ Example Projects

delay_basic.8h

Go to the documentation of this file.
01	1 Copyright (c) 2002, Marek Michalkiewicz
	022Copyright (c) 2007 Joerg Wunsch
	033All rights reserved.
	044
	055Redistribution and use in source and binary forms, with or without
	066modification, are permitted provided that the following conditions are met:
	077
	088* Redistributions of source code must retain the above copyright
	099notice, this list of conditions and the following disclaimer.
	1010
	1111* Redistributions in binary form must reproduce the above copyright
	1212notice, this list of conditions and the following disclaimer in
	1313the documentation and/or other materials provided with the
	1414distribution.
	1515
	1616* Neither the name of the copyright holders nor the names of
	1717contributors may be used to endorse or promote products derived
	1818from this software without specific prior written permission.
	1919
	2020THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
	2121AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
	2222IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
	2323ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
	2424LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
	2525CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
	2626SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
	2727INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
	2828CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	2929ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
	3030POSSIBILITY OF SUCH DAMAGE. */
	3131
	3232/* $Id: delay_basic.h 2453 2014-10-19 08:18:11Z saaadhu $ */
	3333
	3434#ifndef _UTIL_DELAY_BASIC_H_
	3535#define _UTIL_DELAY_BASIC_H_ 1
	3636
	3737#include <inttypes.h>
	3838
	3939#if !defined(__DOXYGEN__)
	4040static __inline__ void _delay_loop_1(uint8_t __count) __attribute__((__always_inline__));
	4141static __inline__ void _delay_loop_2(uint16_t __count) __attribute__((__always_inline__));
	4242#endif
	4343
	4444/** \file */
	4545/** \defgroup util_delay_basic <util/delay_basic.h>: Basic busy-wait delay loops
	4646\code
	4747#include <util/delay_basic.h>
	4848\endcode
	4949
	5050The functions in this header file implement simple delay loops
	5151that perform a busy-waiting.  They are typically used to
	5252facilitate short delays in the program execution.  They are
	5353implemented as count-down loops with a well-known CPU cycle
	5454count per loop iteration.  As such, no other processing can
	5555occur simultaneously.  It should be kept in mind that the
	5656functions described here do not disable interrupts.
	5757
	5858In general, for long delays, the use of hardware timers is
	5959much preferrable, as they free the CPU, and allow for
	6060concurrent processing of other events while the timer is
	6161running.  However, in particular for very short delays, the
	6262overhead of setting up a hardware timer is too much compared
	6363to the overall delay time.
	6464
	6565Two inline functions are provided for the actual delay algorithms.
	6666
	6767*/
	6868
	6969/** \ingroup util_delay_basic
	7070
	7171Delay loop using an 8-bit counter \c __count, so up to 256
	7272iterations are possible.  (The value 256 would have to be passed
	7373as 0.)  The loop executes three CPU cycles per iteration, not
	7474including the overhead the compiler needs to setup the counter
	7575register.
	7676
	7777Thus, at a CPU speed of 1 MHz, delays of up to 768 microseconds
	7878can be achieved.
	7979*/
	8080void
	81   81
_delay_loop_1(uint8_t __count)
	8282{
	8383__asm__ volatile (
	8484"1: dec %0" "\n\t"
	8585"brne 1b"
	8686: "=r" (__count)
	8787: "0" (__count)
	8888);
	8989}
	9090
	9191/** \ingroup util_delay_basic
	9292
	9393Delay loop using a 16-bit counter \c __count, so up to 65536
	9494iterations are possible.  (The value 65536 would have to be
	9595passed as 0.)  The loop executes four CPU cycles per iteration,
	9696not including the overhead the compiler requires to setup the
	9797counter register pair.
	9898
	9999Thus, at a CPU speed of 1 MHz, delays of up to about 262.1
	100100milliseconds can be achieved.
	101101*/
	102102void
	103  103
_delay_loop_2(uint16_t __count)
	104104{
	105105__asm__ volatile (
	106106"1: sbiw %0,1" "\n\t"
	107107"brne 1b"
	108108: "=w" (__count)
	109109: "0" (__count)
	110110);
	111111}
	112112
	113113#endif /* _UTIL_DELAY_BASIC_H_ */
	

_delay_loop_2 void _delay_loop_2(uint16_t __count)

Definition: delay_basic.h:103

__attribute__ static __inline void __attribute__((__always_inline__)) __power_all_enable()

Definition: power.h:1148

uint8_t unsigned char uint8_t

Definition: stdint.h:83

inttypes.h

_delay_loop_1 void _delay_loop_1(uint8_t __count)

Definition: delay_basic.h:81

uint16_t unsigned int uint16_t

Definition: stdint.h:93