Simple delay utilities
Simple busy-wait loops assembly library. Should be used with AVR Assembler v. 2. This library can be used with any AVR device, because not use any specific peripheral.
Source code of wait.asm file
;------------------------------------------------------------------------------
; Busy-wait loops utilities module
; For F_CPU >= 4MHz
; http://avr-mcu.dxp.pl
; (c) Radoslaw Kwiecien, 2008
;------------------------------------------------------------------------------
#ifndef F_CPU
#error "F_CPU must be defined!"
#endif
#if F_CPU < 4000000
#warning "F_CPU too low, possible wrong delay"
#endif
#define CYCLES_PER_US (F_CPU/1000000)
#define C4PUS (CYCLES_PER_US/4)
#define DVUS(x) (C4PUS*x)
;------------------------------------------------------------------------------
; Input : XH:XL - number of CPU cycles to wait (divided by four)
;------------------------------------------------------------------------------
Wait4xCycles:
sbiw XH:XL, 1
brne Wait4xCycles
ret
;------------------------------------------------------------------------------
; Input : r16 - number of miliseconds to wait
;------------------------------------------------------------------------------
WaitMiliseconds:
push r16
WaitMsLoop:
ldi XH,HIGH(DVUS(500))
ldi XL,LOW(DVUS(500))
rcall Wait4xCycles
ldi XH,HIGH(DVUS(500))
ldi XL,LOW(DVUS(500))
rcall Wait4xCycles
dec r16
brne WaitMsLoop
pop r16
ret
;------------------------------------------------------------------------------
; End of file
;------------------------------------------------------------------------------ |
How use this file?
In file with entry code place include directive :
#include "wait.asm" ; AVR Assembler v.2
// or
.include "wait.asm" ; AVR Assembler v. 1 |
DVUS(x) macro
This macro is used to compute number of cycles for specified time in microseconds.
Wait4xCycles
This routine generate delay in CPU cycles multiply by 4. For delay equal 10000 CPU cycles load to XH:XL 2500.
Example of use :
ldi XH, HIGH(DVUS(500))
ldi XL, LOW(DVUS(500))
rcall Wait4xCycles ; wait 500 microseconds |
WaitMiliseconds
This routine generate delay in miliseconds. Number of miliseconds must be loaded into r16 register before call this routine.
Example of use :
ldi r16, 50
rcall WaitMiliseconds ; wait 50 miliseconds |
|