## Improved delay loop calculator

16 posts / 0 new
Author
Message

AvrDelayLoop.exe is a handy tool but it generates some really complicated code. Example:

```; =============================
;    delay loop generator
;     50000000 cycles:
; -----------------------------
; delaying 49939965 cycles:
ldi  R18, \$FF
WGLOOP0:  ldi  R19, \$FF
WGLOOP1:  ldi  R20, \$FF
WGLOOP2:  dec  R20
brne WGLOOP2
dec  R19
brne WGLOOP1
dec  R18
brne WGLOOP0
; -----------------------------
; delaying 60030 cycles:
ldi  R18, \$57
WGLOOP3:  ldi  R19, \$E5
WGLOOP4:  dec  R19
brne WGLOOP4
dec  R18
brne WGLOOP3
; -----------------------------
; delaying 3 cycles:
ldi  R18, \$01
WGLOOP5:  dec  R18
brne WGLOOP5
; -----------------------------
; delaying 2 cycles:
nop
nop
; =============================
```

I put together a web page using Javascript that generates much simpler code, such as:

```; Delay 50 000 000 cycles

ldi  r18, 254
ldi  r19, 167
ldi  r20, 102
L1: dec  r20
brne L1
dec  r19
brne L1
dec  r18
brne L1
nop
nop
```

I think I got the math right, and I tested it on Firefox and IE on Windows, but if you have any problems let me know. If I've reinvented the wheel and there's already something that calculates this type of loop, let me know that too. I looked but didn't find.

The math works out like this:

Zero, one, or two "nop" to get to a multiple of 3.

For cycles up to 768, a single loop at 3 cycles each, 2 cycles for the last loop, 1 cycle for the LDI, so just 3 cycles per loop.

For cycles up to 197121, an outer loop is added. Each time an outer loop runs for the first time, the inner loop runs for the specific short amount of time, and then it runs for 256 loops each time after that because the counter has been reset to zero. The inner loop takes 770 cycles after the first time (256*3 -1 for failed branch, +3 for new DEC/BRNE).

After a third loop is added, the two inner loops together with their counters reset to zero take 197122 cycles each time. Then three loops take 50463234 cycles, four take about 13 billion, etc. (256 * previous + 2).

This example for 50 million cycles takes 253 * 197122 + 166 * 770 + 101 * 3 + 9 + 2.

Here's a one-day loop for a 20MHz clock:

```; Delay 1 728 000 000 000 cycles

ldi  r18, 134
ldi  r19, 195
ldi  r20, 193
ldi  r21, 122
ldi  r22, 166
L1: dec  r22
brne L1
dec  r21
brne L1
dec  r20
brne L1
dec  r19
brne L1
dec  r18
brne L1
nop
nop
```

Hi
Hi Bret

Nice java applet

one thing to make it a bit more sequential is your time line
[s] [ms] [us] [ns] [mins] [hrs] [days] change to [ns] [us] [ms] [sec] [mins] [hrs] [days]

when outputting can it display also the xxx[ms] time as well like you did with Delay xxx {cycles]
we have no idea when we select a time box as this will be a nice feedback

Ken

Done. Thanks for the feedback. It now outputs the duration info, which may include fractional nanoseconds in many cases for non-simple clock frequencies:

```; Delay 369 cycles
; 25us 24 53/128 ns
; at 14.7456 MHz
```

Hi Bret

Fantastic, with the modifaction & update & your effort.
Now applet is more linear in the time selections.

Cheers
Ken

Quote:
If a man has good corn or wood, or boards, or pigs, to sell, or can make better chairs or knives, crucibles or church organs, than anybody else, you will find a broad hard-beaten road to his house, though it be in the woods.
-- Ralph Waldo Emerson.

The more familiar version: "Build a better mousetrap, and the world will beat a path to your door."

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Bret's delay loop java applet is no longer available anywhere on the net. Tried contacting Bret too. No luck.

Anyone have a copy ? Cheers

Last Edited: Fri. Jan 16, 2015 - 10:38 PM

You can probably use a calculator, as the formula examples are given above.

eg from

253 * 197122 + 166 * 770 + 101 * 3 + 9 + 2.

and  (256 * previous + 2). & The inner loop takes 770 cycles after the first time

We have these param values for KL (Loop Constant) and Register Load Values, V0 is innermost

KL=770;V0=102;V1=167;V2=254;

LD=(V2-1)*(256*KL+2)+(V1-1)*KL+(V0-1)*3+9+2  LD = 50000000

and you can work backwards, like this (expect 253 * 197122 + 166 * 770 + 101 * 3 + 9 + 2.)

50M/197122  = 253.650023843102241   so have 253 MSB counts

50M-197122*floor(50M/197122)  = 128134

(50M-197122*floor(50M/197122))/770  = 166.407792207792208  == 166 mid-counts

(128134-floor((50M-197122*floor(50M/197122))/770)*770)  = 314

(314-9-2)/3  = 101 LSB counts

when you have register values [V2..V0], check to confirm a 3 loop Delay value with the formula above

Last Edited: Sat. Jan 17, 2015 - 02:02 AM

JimK wrote:

Bret's delay loop java applet is no longer available anywhere on the net. Tried contacting Bret too. No luck.

Anyone have a copy ? Cheers

I also tried to contact Bret, no luck. I think it was the best one available.

I would love to be able to use it again.

I did find an app on google play but it does have some limitations.

I contacted Bret. The calculator is back online.

That's just great! The best one I have ever used! I just created a 90 minute delay, the routine is very compact. the avrdelayloop.exe program created about 2 pages of code for the same delay!

Wouldn't it be nice if it was a portable windows application??

newbie:

Save the page as "web page complete" in its own folder and run it offline in a web browser. It is completely self contained.

I just created a 90 minute delay, the routine is very compact.

???  To what possible useful end is there to having a microcontroller "sit in one place" and do nothing for 90 minutes?  And if you do, what difference does it make how compact the code is?  Even a Tiny4 has a few hundred words available.

Compare your answer with an AVR app with a simple timer tick e.g. every 10ms.  Handling that takes e.g. 10us every 10ms, or about 0.1% of CPU time.  The other 99+% of the time can be used for useful things, such as watching the button that aborts the countdown; servicing the USART for a new countdown interval; and myriad other things.

100 of these timer ticks make a second, and so forth: 60 seconds in a minute; 90 minutes to your sample interval.  Using the example 10ms tick, a 32-bit counter can time intervals to over a year.  And any combination of "soft timers" in between.

Does "accurate microcontroller delay loop" fit the definition of oxymoron?  At least half of that anyway.  Much ado about nothing.  GCC people in particular seem fascinated by the topic.  The old GCC forum is now "Compilers"; well over 1000 hits over the years.  In other words, the same thing has ben brought up at least once a week since the site started.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Thu. Apr 2, 2015 - 07:25 PM

RickB wrote:

newbie:

Save the page as "web page complete" in its own folder and run it offline in a web browser. It is completely self contained.

Yes I figured that out last night, thank you for the tip.

theusch wrote:

???  To what possible useful end is there to having a microcontroller "sit in one place" and do nothing for 90 minutes?  And if you do, what difference does it make how compact the code is?  Even a Tiny4 has a few hundred words available.

the timer is in use in the device I am using. I know, I know, I know... i could just use another avr.

If you must know, I use an at90S1200 as an Infrared transmitter.

I work nights. Trying to sleep during the day is sometimes difficult. It is easier for me to watch some boring sitcom which will put me out like a light! Trouble is my media player does not have a sleep timer. TV does, but not the media player.

After 90 minutes, the avr transmits the Infrared stop command to stop the video and then turn the unit off. It's powered by the usb port in the back of the media player so when the unit is off, so is the avr circuit.

NOW some will ask "WHY NOT USE AN AVR WITH A PWM?"

my answer: I have done this successfully using an attiny2313. It is working, however I just wanted to try it on a device without the PWM hardware just for the hell of it.

Staying up all night when I am not working on the weekend  (because my system is used to it) gets boring.

Just looking for a challenge now and then.

Last Edited: Thu. Apr 2, 2015 - 08:53 PM

RickB wrote:

I contacted Bret. The calculator is back online.

Nice work Rick ! cheers

the timer is in use in the device I am using. I know, I know, I know... i could just use another avr.

??? again...even with the [where did you find one?] '1200, you are either using the timer during the 90 minutes...or you aren't.

If you have the timer running during that period, then you simply count the overflows at whatever rate.  If the timer isn't in use during that period, then use it for the 90 minutes.