Author Topic: Delay in C  (Read 11435 times)

0 Members and 1 Guest are viewing this topic.

Offline TrickyNekroTopic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Delay in C
« on: October 24, 2008, 10:42:41 AM »
Hello guys,

  I featured it was time for me starting learning C, but a need a a lot of help in the way....
I know nothing, but this is ok since I got u guys helping....

So can you tell to a total newbie how to create delays in C, I only want to flash a led!!!
But how about accurate delays... I know I need a timer but how???

Thanks in advance, Lefteris


Note that I program AVR using Programmers' Notepad...
« Last Edit: October 24, 2008, 10:43:17 AM by TrickyNekro »
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: Delay in C
« Reply #1 on: October 25, 2008, 11:26:29 AM »
I presume you are using AVR Studio - if not then you should!  :P
AVR Studio installls a whole load of stuff at something like 'C:\WinAVR-20071221'. Most of these files are used to define the makeup of all the different AVR microprocessors so that your code can be more generic.
If you look in the sub-directory: 'avr\include\util' you will find 'delay.h' which defines two routines:
1.  _delay_us(double __us) which delays for a given number of microseconds, and
2. _delay_ms(double __ms) which delays for a given number of milliseconds

These don't require a timer as it uses the clock speed to work out how many operations are needed for the required time. It then just spins round 'wasting' that amount of time.

Suggest you also download AVRlib (google it). It provides all sorts of supports classes for servos, uarts, etc etc.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
Re: Delay in C
« Reply #2 on: October 25, 2008, 02:58:08 PM »
there's a good article here on AVR timers and examples on how to use them:
http://members.shaw.ca/climber/avrtimers.html

dunk.

Offline TrickyNekroTopic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,207
  • Helpful? 15
  • 1.6L Peugeot 307 tuner
Re: Delay in C
« Reply #3 on: October 26, 2008, 04:09:53 AM »
First of all a big thanks to you guys...

 By reading the book "C Programming for Microcontrollers", the writer suggested that I
include <avr/delay.h> which turned out to have other directory to <util/delay.h>

That was all right... I got it...

But when I say _delay_loop_2( any number here ) what does this number means and how accurate is it...

And how accurate is the delay_us and delay_ms ???

Not that I got better accuracy with the BASCOM I used but I need to know cause
devices which require precise timing like devices that have 1wire protocol need
time sensitive libraries to operate...

Also are delays, you said to me, crystal depended or am I clear with this...

I use Programmers notepad for linking and compiling and programmnig but still can produce the .coff file so that I can
simulate with AVRstudio...

That is basically... Thanks for all the help guy cause I'm a totally new in C...

Cheers,
Lefteris, Greece
For whom the interrupts toll...


P.S. I've been inactive for almost a year... Don't give promises but I'll try to complete my tutorials. I'll let you know when..

Cheers!

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 109
Re: Delay in C
« Reply #4 on: October 27, 2008, 07:53:26 AM »
First of all you shouldn't really call _delay_loop_1 or _delay_loop_2 directly. You should be calling _delay_ms or _delay_us instead because these calculate the parameter that should be passed to _delay_loop_1 or _delay_loop_2 in order to generate the correct delay depending on the clock speed of your processor. This speed should be defined: either in your makefile or in 'global.h' by using:

#define F_CPU 8000000
where 8000000 indicates your clock speed - in this case 8MHz.

The actual clock speed depends on how you have set the fuses and whether or not you are using an external crystal.

Check-out the comments in util/delay.h to see the largest possible delay you can achieve with each of the methods.

I'm not sure what the accuracy of the delay routines is - so cannot comment. However: it is worth noting that the actual delay may well be increased by the total amount of time spent within interrupt service routines. So either keep your interrupt routines short and fast.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline gamefreak

  • Supreme Robot
  • *****
  • Posts: 543
  • Helpful? 2
  • Robo-Enthusiast
Re: Delay in C
« Reply #5 on: November 05, 2008, 09:14:15 AM »
Out of curiosity is there a way to make the controller work while it waits? Driving servos requires a delay so the pulses are right, but wouldnt it be way more efficient to make the robot do what it needs to do next instead of twiddling its thumbs? Would it not be possible to store the clock to a variable and then call the clock to check against that variable, if the difference is smaller then your desired delay, do something, otherwise, move on.

Any thoughts as to how to implement that?
All hail Rodney, the holy 555 timer
And Steve said: "Let there be lead!"

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
Re: Delay in C
« Reply #6 on: November 05, 2008, 09:39:16 AM »
Quote
Out of curiosity is there a way to make the controller work while it waits?
have a read up on timer interrupts.
you can have the built in hardware timer call an ISR after a specified time delay.
this way your program can continue doing it's thing until it is paused by the ISR to execute the code you have there.


dunk.

Offline should be workin

  • Beginner
  • *
  • Posts: 1
  • Helpful? 0
Re: Delay in C
« Reply #7 on: July 08, 2009, 10:31:16 AM »
you can check the accuracy by setting a large delay for example 1000seconds then compare it to a watch, or clock on computer.

Offline sonictj

  • Supreme Robot
  • *****
  • Posts: 416
  • Helpful? 11
Re: Delay in C
« Reply #8 on: July 08, 2009, 12:15:25 PM »
I would not recommend using #include <util/delay.h> if you are doing anything in the background.  This command uses whats called a nop (no operation) meaning the micro controller won't do anything during this time.  If you are just blinking an led than use it.  Also check avrlibc's manual both _delay_ms and _delay_us have maximum allowable delays before they do undesirable things.

 


Get Your Ad Here