Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

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
 

arturt134

Full Member level 2
Joined
Dec 31, 1999
Messages
125
Helped
8
Reputation
16
Reaction score
7
Trophy points
1,298
Location
Poland
Activity points
1,006
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.
 

Eumel

Member level 1
Joined
Dec 12, 2001
Messages
35
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
244
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
 

klug

Advanced Member level 1
Joined
Jul 4, 2002
Messages
476
Helped
12
Reputation
24
Reaction score
7
Trophy points
1,298
Activity points
4,382
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.
 

huntmine

Junior Member level 3
Joined
Mar 7, 2002
Messages
31
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
220
in hc11

for 68hc11 or hc12:

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

foxbrain

Full Member level 2
Joined
Feb 1, 2010
Messages
142
Helped
5
Reputation
10
Reaction score
4
Trophy points
1,298
Location
Damas
Activity points
2,322
@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.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top