AVR Delay Functions

34 posts / 0 new
Last post
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Reader,
am not new to asm programming but new in using the CodeVision, any way my problem is, i wana write a function its input is the delay i want in sec or msec and it generate to me this delay. using c or asm its ok, but asm is prefered. thnx for ur help

please send to me on: hazem_x_h_work@hotmail.com

urs hazem hegazy

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is DELAY.h header that you should include at your C program before using belove functions.
Pay attention to dissable all interrupts before calling the functions to working the dalay properly.
every where in your program you can use these Functions:
void delay_us(unsigned int n)
void delay_ms(unsigned int n)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,
I've got the same problem, and would like to have precise delays without timers, only that delay.h seems too big to be used on an ATtiny2313 with its 2kB Flash!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

josefbs wrote:
Hello,
I've got the same problem, and would like to have precise delays without timers, only that delay.h seems too big to be used on an ATtiny2313 with its 2kB Flash!

In AVR-LIBC the main delay function accepts float type of variables, using even a single float variable can take 2-4kB of memory. I always custom wrap the inline assembler loops to my own macro. Maybe this is possible in your compiler too.

- Jani

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
//-------------------
void delnms(unsigned int n){
//delay n ms
unsigned int x;

  while(n--){
    x=2600;       //empirically determined fudge factor 16mhz
    while(x--);
  }
}

//-------------------
void del100us(unsigned int n){
//delay n 100us
unsigned int x;

  while(n--){
    x=260; 
    while(x--);
  }
}

//-------------------
void del10us(unsigned int n){
//delay n 10us
unsigned int x;

  while(n--){
    x=26; 
    while(x--);
  }
}

MHz N
14 2500
16 2600
18 2800

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi..

Using the delay.h library from Codevision you need to disable the interrupts when you call for delays functions if you don´t disable the interrups the time delay could not work right.

You could check this in Help from Codevision.

Regards,
Brunomusw

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It'll just be longer, but usually thats why you want a delay... to give something a few ms to happen....

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I mean if you want a precise delay you need to disable the interrupts using the delay.h libraty.

Regards,
Brunomusw

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

brunomusw wrote:
I mean if you want a precise delay you need to disable the interrupts using the delay.h libraty.

Offtopic yes for using delay loops, but if precise delays are required, I would use a timer interrupt to generate that, so that other interrupts would have little effect on delay length.

- Jani

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Hello,
josefbs wrote:
Quote:

Hello,
I've got the same problem, and would like to have precise delays without timers, only that delay.h seems too big to be used on an ATtiny2313 with its 2kB Flash!

In AVR-LIBC the main delay function accepts float type of variables, using even a single float variable can take 2-4kB of memory. I always custom wrap the inline assembler loops to my own macro. Maybe this is possible in your compiler too.


There will be no floating point operation on runtime, if your delay parameter is a constant and if you have enabled the optmizer. Anyway, I recommend to use at least -O1. Then the floting-point calculation will be evaluated at compile-time by the compiler!

Take attention about the suitable delay range, which does depend on the F_CPU. Refer to the avr_libc documentation for details.

Peter

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

josefbs wrote:
Hello,
I've got the same problem, and would like to have precise delays without timers, only that delay.h seems too big to be used on an ATtiny2313 with its 2kB Flash!

Presumably you are using AVR Studio as an IDE for avr-gcc? For some completely odd reason the guys at Atmel decided to default projects to build -O0 rather than the more usual -Os default. The delay.h functions in avr-libc's delay.h will only resolve to optimised delays at compile time IF the optimisation for the compile is something other then -O0 - yet another reason why AVR Studio should NOT default to -O0 but I guess it's done so there's a one to one corresondence when debugging/simulating between the source and the object

Cliff

 

Pages