Forum Menu




 


Log in Problems?
New User? Sign Up!
AVR Freaks Forum Index

Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Author Message
hazem_x_h
PostPosted: Oct 20, 2006 - 06:20 PM
Newbie


Joined: Nov 15, 2005
Posts: 1


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
 
 View user's profile Send private message  
Reply with quote Back to top
alireza
PostPosted: Oct 21, 2006 - 05:50 AM
Rookie


Joined: May 06, 2006
Posts: 31
Location: Tehran

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)
 
 View user's profile Send private message  
Reply with quote Back to top
josefbs
PostPosted: Oct 22, 2006 - 10:27 AM
Newbie


Joined: Mar 12, 2005
Posts: 1


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!
 
 View user's profile Send private message  
Reply with quote Back to top
Jepael
PostPosted: Oct 22, 2006 - 10:36 AM
Raving lunatic


Joined: May 24, 2004
Posts: 6275
Location: Tampere, Finland

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
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Oct 22, 2006 - 03:32 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 23958
Location: Orlando Florida

Code:

//-------------------
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
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
brunomusw
PostPosted: Oct 22, 2006 - 07:07 PM
Posting Freak


Joined: Dec 15, 2005
Posts: 1165
Location: Brazil

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.
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Oct 22, 2006 - 07:10 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 23958
Location: Orlando Florida

It'll just be longer, but usually thats why you want a delay... to give something a few ms to happen....
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
brunomusw
PostPosted: Oct 22, 2006 - 07:19 PM
Posting Freak


Joined: Dec 15, 2005
Posts: 1165
Location: Brazil

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

_________________
Regards,
Brunomusw
 
 View user's profile Send private message  
Reply with quote Back to top
Jepael
PostPosted: Oct 22, 2006 - 07:32 PM
Raving lunatic


Joined: May 24, 2004
Posts: 6275
Location: Tampere, Finland

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
 
 View user's profile Send private message  
Reply with quote Back to top
peter.sager
PostPosted: Oct 23, 2006 - 10:04 AM
Hangaround


Joined: Nov 03, 2002
Posts: 296
Location: Switzerland

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
 
 View user's profile Send private message  
Reply with quote Back to top
clawson
PostPosted: Oct 23, 2006 - 10:11 AM
10k+ Postman


Joined: Jul 18, 2005
Posts: 71163
Location: (using avr-gcc in) Finchingfield, Essex, England

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

_________________
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Oct 23, 2006 - 02:58 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 28940
Location: Wisconsin USA

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!

Huh? CodeVision's delay library?

delay_us() is done in-line; maybe 10 words per invocation. The delay_ms() routine is small; call overhead is maybe half a dozen words.

You must have a whole bunch of invocations to make a noticeable impact on flash usage.

Lee
 
 View user's profile Send private message  
Reply with quote Back to top
bobgardner
PostPosted: Oct 23, 2006 - 05:06 PM
10k+ Postman


Joined: Sep 04, 2002
Posts: 23958
Location: Orlando Florida

For a small flash processor, I vote for not using inlines if the flash starts filling up. There should be 2 versions of the delays.. one that takes the call and return overhead into the calc, and one for inline/macro use. That 'count an int down from 2400' trick is quick and easy and scales well... I tested it by calling it 10 times in a loop that repeated 1000 times, so the loop overhead is small.
 
 View user's profile Send private message Send e-mail Visit poster's website 
Reply with quote Back to top
heinrichs.hj
PostPosted: Oct 23, 2006 - 05:51 PM
Wannabe


Joined: Jul 25, 2002
Posts: 68
Location: Paderborn, Germany

Allthough it is for WinAVR, take a look at this project:
http://www.avrfreaks.net/index.php?module=Freaks%20Academy&func=viewItem&item_type=project&item_id=665
It creates delays as accurate as a single clock cycle.
It requires compile time constants and at least -O1 optimization.

Heinrichs.hj
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Oct 23, 2006 - 06:27 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 28940
Location: Wisconsin USA

Quote:

There should be 2 versions of the delays.. one that takes the call and return overhead into the calc, and one for inline/macro use.

CodeVision already has that, Bob-- delay_us() is inline for accurate timing. delay_ms() is a function call. You can, however, do whatever you want. Smile With the library routines in CV, and if you tell the truth about your AVR clock speed, and if you don't let ISRs get in the way, the CV times are going to be quite accurate down to a us or so for delay_ms(), and nearly that for delay_ms() regardless of your AVR clock speed.

Lee
 
 View user's profile Send private message  
Reply with quote Back to top
teddrao
PostPosted: Oct 15, 2010 - 05:35 AM
Newbie


Joined: Oct 06, 2010
Posts: 7


Hi,
I am using AVR studio. I want to generate us delays.
I included the delay.h and called the function as _delay_us(1) to get a 1us delay.
The compiler optimization is set to -00.
when I compile, I see severe code bloating and the FLash consumption increased from 33% to 50%.
Can anyone suggest why this is happening.?
What is the workaround.?
 
 View user's profile Send private message  
Reply with quote Back to top
teddrao
PostPosted: Oct 15, 2010 - 05:46 AM
Newbie


Joined: Oct 06, 2010
Posts: 7


I understood from the header description that this is because the floating point libraries are linked.
Please suggest a solution.
 
 View user's profile Send private message  
Reply with quote Back to top
mckeemj
PostPosted: Oct 15, 2010 - 06:40 AM
Resident


Joined: Jun 26, 2006
Posts: 847
Location: San Luis Valley, Colorado ( 2,318m )

Perhaps a solution could also be found in the documentation... In case actually checking is a bit much to ask... ALWAYS compile with optimization >= -O1, and with a constant parameter, so that the floating-point libraries are not pulled in.

Martin Jay McKee
 
 View user's profile Send private message  
Reply with quote Back to top
Koshchi
PostPosted: Oct 15, 2010 - 04:14 PM
10k+ Postman


Joined: Nov 17, 2004
Posts: 15005
Location: Vancouver, BC

Quote:
obviously you need to figure out how to prevent optimization from taking your code out
And why would the optimizer take that code out?

_________________
Regards,
Steve A.

The Board helps those that help themselves.
 
 View user's profile Send private message  
Reply with quote Back to top
theusch
PostPosted: Oct 15, 2010 - 04:33 PM
10k+ Postman


Joined: Feb 19, 2001
Posts: 28940
Location: Wisconsin USA

Quote:


Quote:
delay_us() is inline for accurate timing


it depends on what you mean by "accurate".


OK, we are mixing apples and oranges here. But you did get a rise out of me.

The quote is from the old posts, referring to CodeVision. The new posts are talking about GCC.

[and as always, the question of accurate long delays, and even short delays in C, on a microcontroller is a tempest in a teapot. Any real app has a lot more to do than sit in one place and twiddle, And those apps that need cycle-counting (e.g., video generation)--need cycle-counting. Again the implementation of delay routines as C library functions is immaterial.]

So let's get back to your quote and question. What do >>you<< mean by accurate? AFAIK the CV routines will indeed give the correct microseconds. I don't know if at 3.6864MHz clock and a requested delay of (say) 4us whether you get 15 clocks or 16. I'd speculate it is consistent.

AFAIK CodeVision doesn't have a delay_cycles(). IIRC someone developed/posted a nice one for GCC.

So, are you implying that the CV delay_us() is >>not<< accurate?

[edit] You got me curious. So I poked a few delay_us() into a test program and ran it through Simulator2 using a 3.6864MHz AVR clock setting.

Code:
AVR Clock 3.6864MHz
_us     cycles      time
6       22          5.968
5       18          4.833
4       15          4.069
3       12          3.255
2       7           1.899
1       3           .814


Last edited by theusch on Oct 15, 2010 - 04:54 PM; edited 1 time in total
 
 View user's profile Send private message  
Reply with quote Back to top
Display posts from previous:     
Jump to:  
All times are GMT + 1 Hour
Post new topic   Reply to topic
View previous topic Printable version Log in to check your private messages View next topic
Powered by PNphpBB2 © 2003-2006 The PNphpBB Group
Credits