exactly 1us delay in AVR routine ?

Status
Not open for further replies.

elcielo

Full Member level 6
Joined
Jun 13, 2002
Messages
383
Helped
15
Reputation
30
Reaction score
8
Trophy points
1,298
Activity points
3,250
avr delay loop 1 us

1uS delay routine
 

avr routine

In IAR compiller use intrinstic function:
__dalay_cycles(number_of_cycles_to_wait)
In assembler, you have to make a loop, and measure delay time by hand.
Regards.
 

Hi,
in assembler you have to count the cycles used by the statements in the loop (I use a debugger to do this). Then you can calculate how often the loop has to be called as follows:

fosz * tdelay
loopcount = ----------------
cc

fosz e.g. 8Mhz, tdelay 1ms, cc cycle count of loop

no nead for measurement. The xtal should be exact enough.

greetings

Einhart
 

Hi,

if you need a lot of delays in assembler, than you can write a macro.

I have made this wait macro for hard realtime program, you can get any delay up to 660 takts:

Code:
;---------------> It is for AVRA assembler compiler.
.macro wait 
.if	@0 > 660
.error "Wait macro : too high wait value."
.endif
.if 	@0 == 1
	nop
.endif
.if	@0 == 2
	rjmp	loc0a
loc0a:
.endif
.if	@0 == 3
	rjmp	loc1a
loc1a:
	nop
.endif
.if	@0 == 4
	rjmp	loc2a
loc2a:	rjmp	loc2b
loc2b:
.endif
.if	@0 == 5
	rjmp	loc30
loc30:	rjmp	loc3a
loc3a:
	nop
.endif
.if	@0 == 6
	rjmp	loc4
loc4:	rjmp	loc4a
loc4a:  rjmp	loc4b
loc4b:
.endif
.if	@0 == 7
	rcall	QRX07
.endif
.if	@0 == 8
	rcall	QRX08
.endif
.if	@0 == 9
	rcall	QRX09
.endif
.if	@0 == 10
	rcall	QRX10
.endif
.if	@0 > 10
  	ldi	LAG,(@0-7)/3
  .if	(@0-7)%3==0
	rcall	make_lag0
  .endif
  .if	(@0-7)%3==1
	rcall	make_lag1
  .endif
  .if	(@0-7)%3==2
	rcall	make_lag2
  .endif
.endif
.endmacro
To make this macro working, you need additionally add somewhere in code this:
Code:
make_lag2:
	nop
make_lag:
	nop
make_lag0:
	dec	LAG
	brne	make_lag0
	ret
QRX10:	
	nop
QRX09:
	nop
QRX08:
	nop
QRX07:
	ret

---------------------------

So, if you have 10 MHz frequency and need 1 uS you will write simply in your code:
Code:
        wait 10     ;you get 10 takts, T=1 uS

        wait 541    ;you get 541 takts, T=54.1 uS

"wait 10" is some equivalent here of :
Code:
;....
        rcall    QRX10
;....

QRX10:
        nop
        nop
        nop
        ret

---------------------

But if you are writing some very simple program it will be enough to use stuped nops :

Code:
; 10 uS delay for Ft=10 MHz 
        nop
        nop
        nop
        nop
        nop    ;5
        nop
        nop
        nop
        nop
        nop    ;10

Good Luck! klug.
 

in hc11

for 68hc11 or hc12:

nop //each of cc take .5us, nop has 2cc.
 

@kdug
in your simple code for 1us delay i guess it's for 1 MHz not 10 MHz...
because nop is one clock cycle therefore 1/frequency=clock cycle time
is that right?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…