delay_us not compiling


I am using delay_us in my program. When I compile i get an error 'undefined reference to delay_us.

I have installed the WinAVR, the AVR studio, and
I have the line in my program.
#include <util/delay.h>

please advise what Am I missing




That function is not part of WinAVR; you need to install the Pololu AVR library to access it. WinAVR does have the function _delay_us() in util/delay.h, but it can only delay for around 13 microseconds if your CPU frequency is 20 MHz, so you would need to call it multiple times to achieve longer delays.

- Ben


yes i got now but the program requires that i add this line
#include <pololu/orangutan.h>

I already have this line
#include <util/delay.h>

I am programming the BABY-o 168 i hope i have enough ram to accommodate both libraries


You probably no longer need this line since the the Pololu library gives you the same delay functions. Note that delay.h uses almost no RAM, but you can easily determine your heap and flash usage by compiling your program in AVR Studio and seeing how much data memory and program memory it is using.

- Ben

If all you need is a simple delay, you might consider adding a some assembly language delay routines such as the following:

	.global	delay16
	sbiw	r24, 1
	brne	delay16

	.global delay24
	subi	r22, 1
	sbci	r23, 0
	sbci	r24, 0
	brne	delay24

… together with C preprocessor macros such as:

#define	delay_ms(x)	(((x)<=13)?delay16((x)*5000U-1U):delay24((x)*4000UL-1UL))
#define	delay_us(x)	(((x)<=13107)?delay16((x)*5U-1U):delay24((x)*4UL-1UL))

The result is quite compact and flexible. The macros above are for a 20MHz CPU clock, but they can be constructed for the general case using F_CPU if desired.

Hello Ben

Yes I was able to install the package and now compiles successfully.

You mentioned 13ms?

I am really trying to create a delay to move a servo using the BabyO 168.
the code is below and the code was giving to me by Ryan, he said
"Position_delay greater than 1800 or less than 1200, because your servo might run into its stops."

Can you advise further as to a proper delay for this application or is the delay_us

And thank for the advise

void S1_MOVE(int delay_amt)
if (delay_amt == 0)
//	PORTC &= ~(1 << PORTC2); //  stop servo

// set pin high run servo
PORTC |= 1 << PORTC2;

// delay

// stop servo
// PORTC &= ~(1 << PORTC2);

// delay rest of the time
delay_us(2000 - delay_amt);



The delay_us() function in the Pololu AVR library can accept any two-byte unsigned integer as argument. The _delay_us() function from util/delay.h is the one that can only delay for a few microseconds when your MCU is running at 20 MHz.

- Ben


So if i am controlling a servo via the BabyO, and using Delay_us(), as long as I use 1200 Microsecounds -
1800 Microseconds I should be ok?

thank you


Probably, though I don’t want to make any strong claims since I don’t know anything about the servo you’re using. You should carefully test it to see what the limits are by writing a program that outputs a fixed-length pulse train. Start with 1500us and then change the value in steps to figure out what your servo can do.

- Ben

Hi Ben

I will be using this servo I have just Purchased it from Pololu