forked from Grapsus/cc254x_sdcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delay.s
64 lines (63 loc) · 2.06 KB
/
delay.s
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
.module delay
.optsdcc -mmcs51 --model-medium
.area CSEG (CODE)
; void delayMicroseconds(unsigned char microseconds)
; microseconds: number of microseconds delay; any value between 0 and 255
;
; This function delays for the specified number of microseconds using
; a simple loop. If an interrupt occurs during this function, the delay
; will be longer than desired.
;
; Prerequisites: The chip must be running at 24 MHz, and flash prefetching
; and caching must be enabled (MEMCTR = 0;).
;
; Tests:
; This function was tested using the following code:
; P1_0 = 1;
; delayMicroseconds(100);
; P1_0 = 0;
; delayMicroseconds(10);
; P1_0 = 1;
; delayMicroseconds(1);
; P1_0 = 0;
; The code above produced a high pulse of 100.20/100.16 microseconds,
; followed by a low pulse of 10.20/10.16 microseconds,
; followed by a high pulse of 1.20/1.16 microseconds.
; (The exact length of the pulses depended on the parity of the address
; of the lcall instruction used to call delayMicroseconds.)
;
; Implementation Details:
; By experimenting, David determined that jumping (e.g. lcall, ret, or
; ajmp) to an odd address takes one more instruction cycle than jumping
; to an even address.
;
; To get around this, the delayMicroseconds loop contains two jumps:
; one of these jumps will be to an odd address, and one will be to an
; even address, so the effects of the object's parity will always
; cancel out.
;
; The ".even" directive doesn't guarantee that the absolute address of
; a label will be even, it seems to only effect relative positioning
; within an object file, so we could not use the .even directive.
;
.globl _delay_us
loopStart:
nop
nop
nop
nop
ljmp loopJump
nop ; unreachable instruction
nop ; unreachable instruction
nop ; unreachable instruction
nop ; unreachable instruction
loopJump: ; Guaranteed to have a different parity from loopStart.
nop
nop
nop
nop
_delay_us:
mov a,dpl
jz loopEnd
djnz dpl, loopStart
loopEnd: ret