-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.s43
294 lines (257 loc) · 11.4 KB
/
api.s43
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include "bsp.h"
;-------------------------------------------------------------------------------
DIV16bit MACRO Divided,Divisor ; left/right
LOCAL L1,L2,L3 ; for multiple use
mov #17,R9
clr R8 ; Quotient register
mov.w Divided,R13 ; Devided registers is R5,R4
clr R5
mov.w Divisor,R6 ; Divisor registers is R6
L3 rla R8
L1 dec R9
jz L2
rla R13
rlc R5
cmp R6,R5
jlo L3
sub R6,R5
setc
rlc R8
jmp L1
L2 ;mov.w R8,Quotient
;mov.w R5,Remainder
ENDM
;-------------------------------------------------------------------------------
;-------------MULTI MACRO--------------------------------------------
MULT MACRO Number,Times
LOCAL L4
CLR R14
mov Times, R9
L4 add Number, R14
dec R9
JNZ L4
ENDM
;-------------MULTI MACRO--------------------------------------------
TranASCII MACRO Origin_Num
MOV Origin_Num, R7
ADD #0x30, R7
MOV R7, Origin_Num
ENDM
MODULE API
PUBLIC FindExtremas,CalcAvg,FreqOut
EXTERN LCDConfig,LCD_CMD,LCDClear,LCDCursorTo1,margin
EXTERN LCDCursorOff,LCDCursorOn,LCDCursorR,LCDCursorL
EXTERN LCDCursorTo17,LCDPrintChar,Task3state
EXTERN state,poll_delay,counter,VoltDigitCounter,Stop_Timer
EXTERN originFlag,ADC_Start,State2Counter,TIMERconfig,ADCconfig
RSEG CODE
;==============================================================================
; State 1 (PB0) - Global Min and Max Voltage
;==============================================================================
FindExtremas CALL #LCDClear
CALL #LCDCursorTo1 ; Move cursor to 1st square
CALL #PrintVmin
CALL #LCDCursorTo17 ; Move cursor to second row
CALL #PrintVmax
VoltReset CLR R10 ; R10 = Max V. (start with Min)
mov #MaxADC, R11 ; R11 = Min V. (start with Max)
VoltMeasure call #ADC_Start ; start conversion and go to sleep
cmp R10, ADC_MEM ; ADC_MEM - (Our Max Value)
jn IsMin ; if ADC_MEM < (Our Max Value) jump.
mov ADC_MEM, R10 ; if not, set ADC_MEM = Max Value.
IsMin cmp ADC_MEM, R11 ; is MEM lower than known min?
jn DoneMeasure ; if not - we're done sampling
mov ADC_MEM, R11 ; if yes, set ADC_MEM = New Minimun
DoneMeasure cmp #1, state ; is this my current state?
jne ExitState1
dec counter ; reduce noise and error values
jnz VoltMeasure
mov #1000, counter ; reset value to default
PUSH R10 ; Push max value to be printed
CALL #HexToDec
CALL #LCDCursorTo17
CALL #PrintVoltValue
PUSH R11 ; Push min value to be printed
CALL #HexToDec
CALL #LCDCursorTo1
CALL #PrintVoltValue
JMP VoltReset
ExitState1 CALL #LCDClear
RET
;==============================================================================
; State 2 (PB1) - Find Average Voltage
;==============================================================================
CalcAvg CALL #LCDClear
CALL #LCDCursorTo1 ; Move cursor to 1st square
CALL #PrintVavg
NOP
ResetAvg CMP #2, state
JNZ ExitFindAverage
CLR R10 ; R10 = Sampling Sum.
CLR R11 ; R11 = Carry Counter.
State2Loop CALL #ADC_Start
ADD ADC_MEM, R10
jc Carry
DecForLoop DEC State2Counter ; we want 2000 samples
JNZ State2Loop
MOV #NumberOfSamples, State2Counter ; reset to default
call #avagcal
PUSH R9 ; prepare avg to be printeds
CALL #HexToDec
CALL #LCDCursorTo1
CALL #PrintVoltValue
JMP ResetAvg
ExitFindAverage CALL #LCDClear
RET
Carry inc R11
jmp DecForLoop
;==============================================================================
; State 3 (PB2) - Changing PWM
;==============================================================================
FreqOut CALL #ADCconfig
State3Loop CMP #3,state
JNE ExitFreqOut
CLR R10
CLR R11
CALL #ADC_Start
ADD ADC_MEM, R10 ; T0
CALL #ADC_Start
ADD ADC_MEM, R11 ; T1
SUB R10,R11 ; T1-T0->T1 (R11)
MULT R11,#280 ; (T1-T0)(FACTOR=280)
MULT R11,#2048 ; (T'1-T'0)(2048)
cmp #3,R11 ;(T1'-T0')>4 ?
jge Freq1K ; if yes we want 1KHZ freq.
add #3,R11 ;(T1'-T0')<-4?
jn Freq3K ; if yes we want 3KHZ freq.
jmp State3Loop
Freq1K push #524
jmp PWM
Freq3K push #58
jmp PWM
PWM pop R4 ;return address
pop R5 ;value
bis.b #0x08,PWMPort ; PWM ~ 1 , 5cycles
mov R5,R12 ; 2cycles
loop_one dec R12 ; 1cycle
jnz loop_one ; 2cycle
bic.b #0x08,PWMPort ; PWM ~ 0 , 5cycles
mov R5,R12 ; 2cycles
loop_zero dec R12 ; 1cycle
jnz loop_zero ; 2cycle
jmp State3Loop ; Let's calculate again.
ExitFreqOut ret
;===============================================================================
; sub-service routines
;===============================================================================
PrintVmin PUSH #'V'
CALL #LCDPrintChar
PUSH #'m'
CALL #LCDPrintChar
PUSH #'i'
CALL #LCDPrintChar
PUSH #'n'
CALL #LCDPrintChar
PUSH #'='
CALL #LCDPrintChar
CALL #LCDCursorR
PUSH #'.'
CALL #LCDPrintChar
CALL #LCDCursorR
CALL #LCDCursorR
PUSH #'['
CALL #LCDPrintChar
PUSH #'v'
CALL #LCDPrintChar
PUSH #']'
CALL #LCDPrintChar
RET
PrintVmax PUSH #'V'
CALL #LCDPrintChar
PUSH #'m'
CALL #LCDPrintChar
PUSH #'a'
CALL #LCDPrintChar
PUSH #'x'
CALL #LCDPrintChar
PUSH #'='
CALL #LCDPrintChar
CALL #LCDCursorR
PUSH #'.'
CALL #LCDPrintChar
CALL #LCDCursorR
CALL #LCDCursorR
PUSH #'['
CALL #LCDPrintChar
PUSH #'v'
CALL #LCDPrintChar
PUSH #']'
CALL #LCDPrintChar
RET
PrintVavg PUSH #'V'
CALL #LCDPrintChar
PUSH #'a'
CALL #LCDPrintChar
PUSH #'v'
CALL #LCDPrintChar
PUSH #'g'
CALL #LCDPrintChar
PUSH #'='
CALL #LCDPrintChar
CALL #LCDCursorR
PUSH #'.'
CALL #LCDPrintChar
CALL #LCDCursorR
CALL #LCDCursorR
PUSH #'['
CALL #LCDPrintChar
PUSH #'v'
CALL #LCDPrintChar
PUSH #']'
CALL #LCDPrintChar
RET
HexToDec DINT
POP R4 ; Return Address
POP R5 ; R5 = Print Value
ConvertLoop DIV16bit R5, #VoltRatio ; 1/286 = Delta V
PUSH R8 ; X.YZ, push X first than Y and Z.
MULT R5,#10 ; MULTIPLY remainder by 10
MOV R14,R5 ; R14=10*R5
DEC VoltDigitCounter ; 3
JNZ ConvertLoop
MOV #3, VoltDigitCounter
PUSH R4
EINT
RET
PrintVoltValue DINT
POP R12 ; Return Address
POP R15 ; 1/100
POP R13 ; 1/10
POP R14 ; 1
TranASCII R15
TranASCII R13
TranASCII R14
CALL #LCDCursorR
CALL #LCDCursorR
CALL #LCDCursorR
CALL #LCDCursorR
CALL #LCDCursorR
PUSH R14
CALL #LCDPrintChar
CALL #LCDCursorR
PUSH R13
CALL #LCDPrintChar
PUSH R15
CALL #LCDPrintChar
PUSH R12
EINT
RET
avagcal mov.w #11,R6 ; why 11
loop rrc R11 ; Carry
rrc R10 ; Sum
dec R6
jnz loop
mov R10,R9
ret
ENDMOD
END