-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathosccal.asm
178 lines (133 loc) · 2.2 KB
/
osccal.asm
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
;.include "tn2313def.inc"
; DS3231SN 32kHz connected to pin T1
; main loop counts continuously
; interrupt at overflow (2^16) - takes 2 seconds.
; main is 5 cycles, target 8MHz
#define TARGET 2*8000000/5
; search up and down, take the value with the lowest error
.def dir = r31
#define UP 1
#define DOWN 2
rjmp init
.org 0x0005 ;TIMER1 OVF
sub r18, XL
sbc r19, XH
sbc r20, YL
brcs fast
slow:
cpi dir, DOWN
brne nochange1
mov r0, r21
mov r1, r20
mov r2, r19
mov r3, r18
cpse r4,r17
rjmp writeEEP
nochange1:
ldi dir, UP
inc r21
rjmp endi
fast:
cpi dir, UP
brne nochange2
mov r4, r21
mov r5, r20
mov r6, r19
mov r7, r18
cpse r0,r17
rjmp writeEEP
nochange2:
ldi dir, DOWN
dec r21
endi:
out OSCCAL, r21
clr XL
clr XH
clr YL
clr YH
ldi r18, BYTE1(TARGET)
ldi r19, BYTE2(TARGET)
ldi r20, BYTE3(TARGET)
out PORTB, r21
reti
writeEEP:
; DEBUG: log osccal and error amounts
; ldi r17,32
; mov r16,r0
; rcall writeByte
; inc r17
; mov r16,r1
; rcall writeByte
; inc r17
; mov r16,r2
; rcall writeByte
; inc r17
; mov r16,r3
; rcall writeByte
; inc r17
; mov r16,r4
; rcall writeByte
; inc r17
; mov r16,r5
; rcall writeByte
; inc r17
; mov r16,r6
; rcall writeByte
; inc r17
; mov r16,r7
; rcall writeByte
add r7, r3
adc r6, r2
adc r5, r1
brmi use_r0
mov r21,r4
rjmp use_r4
use_r0:
mov r21,r0
use_r4:
mov r16, r21
clr r17
rcall writeByte
end:
com r17
out PORTB, r17
delay: sbiw X,1 nop nop nop brne delay
rjmp end
writeByte:
sbic EECR,EEPE
rjmp PC-1
out EEAR, r17 ;address
out EEDR, r16 ;data
sbi EECR,EEMPE
sbi EECR,EEPE
ret
init:
ldi r16, 1<<CS10 | 1<<CS11 | 1<<CS12 ;T1 rising edge
out TCCR1B, r16
ldi r16, 1<<TOIE1
out TIMSK, r16
clr r16
out EEAR, r16
sbi EECR,EERE
in r16,EEDR
sbrs r16,7
out OSCCAL, r16
in r21, OSCCAL
ldi r16, 0xff
out DDRB, r16
ldi r18, BYTE1(TARGET)
ldi r19, BYTE2(TARGET)
ldi r20, BYTE3(TARGET)
ldi r16,1
ldi r17,0
clr XL
clr XH
clr YL
clr dir
sei
main:
add XL, r16 ;1
adc XH, r17 ;1
adc YL, r17 ;1
rjmp main ;2
; 5 cycles