Starting Electronics Needs Your Help!
It is that time of the year when we need to pay for web hosting and buy new components and equipment for new tutorials. You can help by making a donation. Contribute to this website by clicking the Donate button. The total will be updated once daily. (You may need to clear your browser cache to see the updates.)

Target Amount: $2000
Amount Raised: $1369

Donations Received

Top Donor: C.C. $100

X

AVR _delay_ms() Delay Library Function

Created on: 3 May 2013

Part 10 of the ATtiny2313 Tutorial

The _delay_ms() library function is used to create a timing delay in software for the ATtiny2313 AVR microcontroller. The function is part of the GNU library that is installed with Atmel Studio.

Although it is better in most cases to use an AVR timer to generate delays, the software delay function is convenient in small programs and for quick prototyping and experimentation.

In this part of the tutorial, an LED is switched on and off using the _delay_ms() function to create on and off timing delays for the LED.

This video shows the circuit operating and running the C program that calls the _delay_ms() function.

Can't see the video? View on YouTube →

Software

The software is shown below for an ATtiny2313 with an LED interfaced to pin 14 (PB2). For circuit diagrams, see previous parts of this tutorial and modify to connect the LED to pin 14.

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>

int main(void)
{
    DDRB   |= (1 << PB2);           // LED on pin PB2
    
    while(1)
    {
        _delay_ms(50);              // 50ms delay
        PORTB &= ~(1 << PB2);       // LED off
        _delay_ms(50);              // 50ms delay
        PORTB |=  (1 << PB2);       // LED on
    }
}

Clock Frequency

Before using the _delay_ms() function, the clock frequency of the ATtiny2313 must first be defined at the top of the file before the header files. In this example, the default 1MHz factory setting for the ATtiny2313 is used:

#define F_CPU 1000000UL

Header File

The delay.h header file must be included in order to use the delay library function:

#include <util/delay.h>

Calling the Function

The function is called and passed the desired time delay in milliseconds. Here it is set to make a delay of 50ms:

_delay_ms(50);

The same library has a microsecond delay function as well: _delay_us().

Function Requirements

In order to use the _delay_ms() function and get it to delay for the specified time, firstly the frequency that the ATtiny2313 is running at must be specified by defining it at the top of the C source file as shown above.

Compiler optimization must be on for the function to generate the desired timing period. In Atmel Studio optimization level 1 is on by default.

Function Documentation

The header file delay.h contains documentation in the form of comments in the file. To open the delay.h file in Atmel Studio, right-click the header file name in the C source file and then click Goto Implementation on the menu that pops up.

Opening the Header File from the C Source File in Atmel Studio

Opening the Header File from the C Source File in Atmel Studio

Documentation can also be found online at www.nongnu.org/avr-libc/user-manual/group__util__delay.html

The documentation contains more information and limitations of the two delay library functions.

Amazon.com

Amazon.co.uk

Atmel Studio Source Code

The C source code for this part of the tutorial can be copied and pasted from the above listing, or the entire Atmel Studio 6 project can be downloaded here:

built_in_delay.zip (14.9kB)

← Go back to Part 9Go to Part 11 →