Subversion Repositories svn.mios

Rev

Rev 52 | Rev 321 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
53 tk 1
; $Id: mios_mf.inc 53 2008-01-30 22:52:41Z tk $
1 tk 2
;
3
; MIOS Motorfader Handler
4
;
5
; ==========================================================================
6
;
7
;  Copyright 1998-2006 Thorsten Klose (tk@midibox.org)
8
;  Licensed for personal non-commercial use only.
9
;  All other rights reserved.
10
;
11
; ==========================================================================
12
 
13
;; for debugging ONLY: the ADC values during a move are logged at 0x080-0x37f
14
#define MIOS_MF_LOG_MOVES 0
15
 
16
 
17
;; Pins of MF Shift Registers
18
MIOS_MF_LAT_RCLK	EQU	LATC	; Pin C.0
19
MIOS_MF_PIN_RCLK	EQU	0
20
MIOS_MF_LAT_SCLK	EQU	LATC	; Pin C.1
21
MIOS_MF_PIN_SCLK	EQU	1
22
MIOS_MF_LAT_DOUT	EQU	LATC	; Pin C.2
23
MIOS_MF_PIN_DOUT	EQU	2
24
 
25
MIOS_MF_TIMEOUT_RELOAD_VALUE EQU 0xff
26
MIOS_MF_REPEAT_CTR_VALUE     EQU 0x1f
27
MIOS_MF_MANUAL_CTR_VALUE     EQU 0xff
28
 
29
;; ==========================================================================
30
;;  Motor Pins
31
;; ==========================================================================
32
MIOS_MF_PIN_Mx_UP       EQU     0
33
MIOS_MF_PIN_Mx_DOWN     EQU     1
34
 
35
MIOS_MF_REG_M1          EQU     MIOS_MF_SR0
36
MIOS_MF_PIN_M1_UP       EQU     6
37
MIOS_MF_PIN_M1_DOWN     EQU     7
38
MIOS_MF_REG_M2          EQU     MIOS_MF_SR0
39
MIOS_MF_PIN_M2_UP       EQU     4
40
MIOS_MF_PIN_M2_DOWN     EQU     5
41
MIOS_MF_REG_M3          EQU     MIOS_MF_SR0
42
MIOS_MF_PIN_M3_UP       EQU     2
43
MIOS_MF_PIN_M3_DOWN     EQU     3
44
MIOS_MF_REG_M4          EQU     MIOS_MF_SR0
45
MIOS_MF_PIN_M4_UP       EQU     0
46
MIOS_MF_PIN_M4_DOWN     EQU     1
47
MIOS_MF_REG_M5          EQU     MIOS_MF_SR1
48
MIOS_MF_PIN_M5_UP       EQU     6
49
MIOS_MF_PIN_M5_DOWN     EQU     7
50
MIOS_MF_REG_M6          EQU     MIOS_MF_SR1
51
MIOS_MF_PIN_M6_UP       EQU     4
52
MIOS_MF_PIN_M6_DOWN     EQU     5
53
MIOS_MF_REG_M7          EQU     MIOS_MF_SR1
54
MIOS_MF_PIN_M7_UP       EQU     2
55
MIOS_MF_PIN_M7_DOWN     EQU     3
56
MIOS_MF_REG_M8          EQU     MIOS_MF_SR1
57
MIOS_MF_PIN_M8_UP       EQU     0
58
MIOS_MF_PIN_M8_DOWN     EQU     1
59
 
60
#if MIOS_MF_LOG_MOVES
61
MIOS_MF_LOG_PTR_L	EQU	0x7c
62
MIOS_MF_LOG_PTR_H	EQU	0x7d
63
 
64
MIOS_MF_LOG_SAVED_FSR1L EQU	0x7e
65
MIOS_MF_LOG_SAVED_FSR1H	EQU	0x7f
66
 
67
;; clears log range from 0x080-0x37f
68
MIOS_MF_LOG_Init
69
	movff	FSR1L, MIOS_MF_LOG_SAVED_FSR1L
70
	movff	FSR1H, MIOS_MF_LOG_SAVED_FSR1H
71
 
72
	lfsr	FSR1, 0x080
73
MIOS_MF_LOG_Init_Loop
74
	clrf	POSTINC1
75
 
76
	movf	FSR1H, W
77
	xorlw	0x03
78
	bnz	MIOS_MF_LOG_Init_Loop
79
	movf	FSR1L, W
80
	xorlw	0x80
81
	bnz	MIOS_MF_LOG_Init_Loop
82
 
83
	lfsr	FSR1, 0x080
84
	movff	FSR1L, MIOS_MF_LOG_PTR_L
85
	movff	FSR1H, MIOS_MF_LOG_PTR_H
86
 
87
	movff	MIOS_MF_LOG_SAVED_FSR1L, FSR1L
88
	movff	MIOS_MF_LOG_SAVED_FSR1H, FSR1H
89
	return
90
 
91
 
92
MIOS_MF_LOG_Push
93
	movff	FSR1L, MIOS_MF_LOG_SAVED_FSR1L
94
	movff	FSR1H, MIOS_MF_LOG_SAVED_FSR1H
95
 
96
	movff	MIOS_MF_LOG_PTR_L, FSR1L
97
	movff	MIOS_MF_LOG_PTR_H, FSR1H
98
 
99
	movwf	POSTINC1
100
 
101
	movf	FSR1H, W
102
	xorlw	0x03
103
	bnz	MIOS_MF_LOG_Push_DontResetPtr
104
	movf	FSR1L, W
105
	xorlw	0x80
106
	bnz	MIOS_MF_LOG_Push_DontResetPtr
107
MIOS_MF_LOG_Push_ResetPtr
108
	lfsr	FSR1, 0x080
109
MIOS_MF_LOG_Push_DontResetPtr
110
 
111
	movff	FSR1L, MIOS_MF_LOG_PTR_L
112
	movff	FSR1H, MIOS_MF_LOG_PTR_H
113
 
114
	movff	MIOS_MF_LOG_SAVED_FSR1L, FSR1L
115
	movff	MIOS_MF_LOG_SAVED_FSR1H, FSR1H
116
	return
117
#endif
118
 
119
;; --------------------------------------------------------------------------
120
;;  FUNCTION: MIOS_MF_FaderMove
121
;;  C_DECLARATION: void MIOS_MF_FaderMove(unsigned char fader, unsigned int pos)
122
;;  DESCRIPTION: set target position and move fader
123
;;  NOTE: function can only be used when motordriver has been enabled.
124
;;  IN:	  fader number in WREG, fader position in MIOS_PARAMETER[12]
125
;;  C_IN: fader number in <fader>, fader position in <pos>
126
;;  OUT:  -
127
;;  USES: FSR1, BSR
128
;;  EXAMPLE:
129
;;	;; move fader #7 to highest position (0x3ff)
130
;;	movlw	0x03
131
;;	movwf	MIOS_PARAMETER2
132
;;	movlw	0xff
133
;;	movwf	MIOS_PARAMETER1
134
;;	movlw	0x07
135
;;	call	MIOS_MF_FaderMove
136
;;  C_EXAMPLE:
137
;;	// move fader #7 to highest position (0x3ff)
138
;;	MIOS_MF_FaderMove(0x07, 0x3ff);
139
;; --------------------------------------------------------------------------
140
MIOS_MF_FaderMove
141
	SET_BSR	MIOS_AIN_CTRL
142
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
143
	return
144
 
145
	IRQ_DISABLE
146
 
147
	;; ignore new value if fader is currently manually moved (feedback killer)
148
	lfsr	FSR1, MIOS_MF_MANUAL_CTR0
149
	andlw	0x07
150
	addwf	FSR1L, F
151
	movf	INDF1, W
152
	bnz	MIOS_MF_FaderMove_Skip
153
 
154
	movlw	(MIOS_MF_TARGET_POS_L0-MIOS_MF_MANUAL_CTR0) & 0xff
155
	addwf	FSR1L, F
156
	movff	MIOS_PARAMETER1, INDF1
157
	bsf	FSR1L, 3
158
	movff	MIOS_PARAMETER2, INDF1
159
 
160
	movlw	(MIOS_MF_REPEAT_CTR0-MIOS_MF_TARGET_POS_H0) & 0xff
161
	addwf	FSR1L, F
162
	movf	INDF1, W
163
	movlw	MIOS_MF_REPEAT_CTR_VALUE
164
	movwf	INDF1
165
 
166
	movlw	(MIOS_MF_TIMEOUT_CTR0-MIOS_MF_REPEAT_CTR0) & 0xff
167
	addwf	FSR1L, F
168
	movlw	MIOS_MF_TIMEOUT_RELOAD_VALUE
169
	movwf	INDF1
170
 
171
#if MIOS_MF_LOG_MOVES
172
	;; clear log buffer if fader number == 0x00
173
	movf	FSR1L, W
174
	andlw	0x07
175
	skpnz
176
	rcall	MIOS_MF_LOG_Init
177
#endif
178
 
179
MIOS_MF_FaderMove_Skip
180
	IRQ_ENABLE
181
	return
182
 
183
;; --------------------------------------------------------------------------
184
;;  FUNCTION: MIOS_MF_Enable
185
;;  C_DECLARATION: void MIOS_MF_Enable(void)
186
;;  DESCRIPTION: enables the MF module - in this mode, multiplexers
187
;;  are disabled. Up to 8 motorfaders can be controlled over the MUX port.
188
;;  This function can only be used when the motordriver has been enabled.
189
;;  IN:	  -
190
;;  C_IN:  -
191
;;  OUT:  -
192
;;  C_OUT:  -
193
;;  USES: BSR
194
;;  EXAMPLE:
195
;;
196
;;	;; initialize the MF driver for Alps RSAON11M9 faders
197
;;	call	MIOS_MF_Enable
198
;;	movlw	3
199
;;	call	MIOS_MF_DeadbandSet
200
;;
201
;;	movlw	1
202
;;	call	MIOS_MF_PWM_DutyCycleUpSet
203
;;	movlw	1
204
;;	call	MIOS_MF_PWM_DutyCycleDownSet
205
;;	movlw	3
206
;;	call	MIOS_MF_PWM_PeriodSet
207
;;
208
;;  C_EXAMPLE:
209
;;
210
;;	;; initialize the MF driver for Alps RSAON11M9 faders
211
;;	MIOS_MF_Enable();
212
;;	MIOS_MF_DeadbandSet(3);
213
;;	MIOS_MF_PWM_DutyCycleUpSet(1);
214
;;	MIOS_MF_PWM_DutyCycleDownSet(1);
215
;;	MIOS_MF_PWM_PeriodSet(3);
216
;;
217
;; --------------------------------------------------------------------------
218
MIOS_MF_Enable
219
	;; ensure that max. 8 faders are active
220
	movlw	0x08
221
	cpfsgt	MIOS_AIN_NUMBER, BANKED
222
	rgoto MIOS_MF_Enable_NumberOk
223
	movwf	MIOS_AIN_NUMBER, BANKED
224
MIOS_MF_Enable_NumberOk
225
 
226
	SET_BSR	MIOS_AIN_CTRL
227
	bsf	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
228
 
229
	SET_BSR	MIOS_MF_SUSPEND
230
	clrf	MIOS_MF_SUSPEND, BANKED
231
 
232
	;; set initial PWM values
233
	movlw	0x03
234
	rcall	MIOS_MF_PWM_PeriodSet
235
	movlw	0x01
236
	rcall	MIOS_MF_PWM_DutyCycleDownSet
237
	movlw	0x01
238
	rcall	MIOS_MF_PWM_DutyCycleUpSet
239
 
240
	;; set initial deadband
241
	movlw	0x01
242
	rcall	MIOS_MF_DeadbandSet
243
 
244
	return
245
 
246
;; --------------------------------------------------------------------------
247
;;  FUNCTION: MIOS_MF_Disable
248
;;  C_DECLARATION: void MIOS_MF_Disable(void)
249
;;  DESCRIPTION: disables the MF module
250
;;  IN:	  -
251
;;  C_IN:  -
252
;;  OUT:  -
253
;;  C_OUT:  -
254
;;  USES: BSR
255
;; --------------------------------------------------------------------------
256
MIOS_MF_Disable
257
	SET_BSR	MIOS_AIN_CTRL
258
	bcf	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
259
	return
260
 
261
;; --------------------------------------------------------------------------
262
;;  FUNCTION: MIOS_MF_SuspendEnable
263
;;  C_DECLARATION: void MIOS_MF_SuspendEnable(unsigned char fader)
264
;;  DESCRIPTION: suspends the motor<BR>
265
;;  (function used by touchsensor detection)<BR>
266
;;  function can only be used when motordriver has been enabled.
267
;;  IN:	  number of motor in WREG (0-7)
268
;;  C_IN:  number of motor in <fader> (0-7)
269
;;  OUT:  -
270
;;  C_OUT:  -
271
;;  USES: BSR
272
;; --------------------------------------------------------------------------
273
MIOS_MF_SuspendEnable
274
	SET_BSR	MIOS_AIN_CTRL
275
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
276
	return
277
	call	MIOS_HLP_GetBitORMask
278
	SET_BSR	MIOS_MF_SUSPEND
279
	iorwf	MIOS_MF_SUSPEND, F, BANKED
280
	return
281
 
282
;; --------------------------------------------------------------------------
283
;;  FUNCTION: MIOS_MF_SuspendDisable
284
;;  C_DECLARATION: void MIOS_MF_SuspendDisable(unsigned char fader)
285
;;  DESCRIPTION: deactivate suspend mode of motor<BR>
286
;;  (function used by touchsensor detection)<BR>
287
;;  function can only be used when motordriver has been enabled.
288
;;  IN:	  number of motor in WREG (0-7)
289
;;  C_IN:  number of motor in <fader> (0-7)
290
;;  OUT:  -
291
;;  C_OUT:  -
292
;;  USES: BSR, FSR1
293
;; --------------------------------------------------------------------------
294
MIOS_MF_SuspendDisable
295
	SET_BSR	MIOS_AIN_CTRL
296
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
297
	return
298
	SET_BSR	MIOS_TMP1
299
	movwf	MIOS_TMP1, BANKED
300
	lfsr	FSR1, MIOS_MF_MANUAL_CTR0
301
	andlw	0x07
302
	addwf	FSR1L, F
303
	movlw	MIOS_MF_MANUAL_CTR_VALUE
304
	movwf	INDF1
305
 
306
	movf	MIOS_TMP1, W, BANKED
307
	call	MIOS_HLP_GetBitANDMask
308
	SET_BSR	MIOS_MF_SUSPEND
309
	andwf	MIOS_MF_SUSPEND, F, BANKED
310
	return
311
 
312
;; --------------------------------------------------------------------------
313
;;  FUNCTION: MIOS_MF_SuspendGet
314
;;  C_DECLARATION: unsigned char MIOS_MF_SuspendGet(unsigned char fader)
315
;;  DESCRIPTION: return suspend state of motor
316
;;  IN:	  number of motor in WREG (0-7)
317
;;  C_IN:  number of motor in <fader> (0-7)
318
;;  OUT:  WREG = 1 if motor is suspended, else 0
319
;;  C_OUT:  1 if motor is suspended, else 0
320
;;  USES: BSR
321
;; --------------------------------------------------------------------------
322
MIOS_MF_SuspendGet
323
	call	MIOS_HLP_GetBitORMask
324
	SET_BSR	MIOS_MF_SUSPEND
325
	andwf	MIOS_MF_SUSPEND, W, BANKED
326
	movlw	0x00
327
	skpz
328
	movlw	0x01
329
	andlw	0xff		; to update STATUS register
330
	return
331
 
332
;; --------------------------------------------------------------------------
333
;;  FUNCTION: MIOS_MF_TouchDetectionReset
334
;;  C_DECLARATION: void MIOS_MF_TouchDetectionReset(unsigned char fader)
335
;;  DESCRIPTION: this function resets the software implemented touch detection
336
;;  so that the fader is repositioned regardless if it is currently
337
;;  manually moved or not
338
;;  IN:	  number of motor in WREG (0-7)
339
;;  C_IN:  number of motor in <fader> (0-7)
340
;;  OUT:  -
341
;;  C_OUT:  -
342
;;  USES: BSR
343
;; --------------------------------------------------------------------------
344
MIOS_MF_TouchDetectionReset
345
	SET_BSR	MIOS_AIN_CTRL
346
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
347
	return
348
 
349
	lfsr	FSR1, MIOS_MF_MANUAL_CTR0
350
	andlw	0x07
351
	clrf	PLUSW1
352
	return
353
 
354
;; --------------------------------------------------------------------------
355
;;  FUNCTION: MIOS_MF_DeadbandSet
356
;;  C_DECLARATION: void MIOS_MF_DeadbandSet(unsigned char deadband)
357
;;  DESCRIPTION: sets the deadband value for MF driver
358
;;  NOTE: function can only be used when motordriver has been enabled.
359
;;  IN:	  deadband value in WREG
360
;;  C_IN:  deadband value in <deadband>
361
;;  OUT:  -
362
;;  C_OUT:  -
363
;;  USES: BSR
364
;; --------------------------------------------------------------------------
365
MIOS_MF_DeadbandSet
366
	SET_BSR	MIOS_AIN_CTRL
367
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
368
	return
369
	SET_BSR	MIOS_MF_DEADBAND
370
	movwf	MIOS_MF_DEADBAND, BANKED
371
	return
372
 
373
;; --------------------------------------------------------------------------
374
;;  FUNCTION: MIOS_MF_DeadbandGet
375
;;  C_DECLARATION: unsigned char MIOS_MF_DeadbandGet(void)
376
;;  DESCRIPTION: returns the deadband value for MF driver
377
;;  IN:	  -
378
;;  C_IN:  -
379
;;  OUT:  deadband value in MIOS_PARAMETER1 and WREG
380
;;  C_OUT:  deadband value
381
;;  USES: BSR
382
;; --------------------------------------------------------------------------
383
MIOS_MF_DeadbandGet
384
	SET_BSR	MIOS_MF_DEADBAND
385
	movf	MIOS_MF_DEADBAND, W, BANKED
386
	movwf	MIOS_PARAMETER1
387
	return
388
 
389
;; --------------------------------------------------------------------------
390
;;  FUNCTION: MIOS_MF_PWM_PeriodSet
391
;;  C_DECLARATION: void MIOS_MF_PWM_PeriodSet(unsigned char speed)
392
;;  DESCRIPTION: Sets the PWM period - see http://www.ucapps.de/mbhp_mf.html
393
;;  for detailed informations about this value
394
;;  IN:	  PWM period in WREG
395
;;  C_IN:  PWM period in <speed>
396
;;  OUT:  -
397
;;  C_OUT:  -
398
;;  USES: BSR
399
;; --------------------------------------------------------------------------
400
MIOS_MF_PWM_PeriodSet
401
	SET_BSR	MIOS_AIN_CTRL
402
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
403
	return
404
	SET_BSR	MIOS_MF_PWM_PERIOD
405
	movwf	MIOS_MF_PWM_PERIOD, BANKED
406
	return
407
 
408
;; --------------------------------------------------------------------------
409
;;  FUNCTION: MIOS_MF_PWM_PeriodGet
410
;;  C_DECLARATION: unsigned char MIOS_MF_PWM_PeriodGet(void)
411
;;  DESCRIPTION: Returns the PWM period - see http://www.ucapps.de/mbhp_mf.html
412
;;  for detailed informations about this value
413
;;  IN:	  -
414
;;  C_IN:	  -
415
;;  OUT:  PWM period in WREG
416
;;  C_OUT:  PWM period
417
;;  USES: BSR
418
;; --------------------------------------------------------------------------
419
MIOS_MF_PWM_PeriodGet
420
	SET_BSR	MIOS_MF_PWM_PERIOD
421
	movf	MIOS_MF_PWM_PERIOD, W, BANKED
422
	movwf	MIOS_PARAMETER1
423
	return
424
 
425
;; --------------------------------------------------------------------------
426
;;  FUNCTION: MIOS_MF_PWM_DutyCycleDownSet
427
;;  C_DECLARATION: void MIOS_MF_PWM_DutyCycleDownSet(unsigned char cali_down)
428
;;  DESCRIPTION: Sets the Duty Cycle for downward moves -
429
;;  see http://www.ucapps.de/mbhp_mf.html for detailed informations about this value
430
;;  IN:	  Duty Cycle in WREG
431
;;  C_IN:  Duty Cycle in <cali_down>
432
;;  OUT:  -
433
;;  C_OUT:  -
434
;;  USES: BSR
435
;; --------------------------------------------------------------------------
436
MIOS_MF_PWM_DutyCycleDownSet
437
	SET_BSR	MIOS_AIN_CTRL
438
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
439
	return
440
	SET_BSR	MIOS_MF_PWM_DUTY_CYCLE_DOWN
441
	movwf	MIOS_MF_PWM_DUTY_CYCLE_DOWN, BANKED
442
	return
443
 
444
;; --------------------------------------------------------------------------
445
;;  FUNCTION: MIOS_MF_PWM_DutyCycleDownGet
446
;;  C_DECLARATION: unsigned char MIOS_MF_PWM_DutyCycleDownGet(void)
447
;;  DESCRIPTION: Returns the Duty Cycle for downward moves -
448
;;  see http://www.ucapps.de/mbhp_mf.html for detailed informations about this value
449
;;  IN:	  -
450
;;  C_IN:  -
451
;;  OUT:  Duty Cycle in WREG
452
;;  C_OUT:  Duty Cycle
453
;;  USES: BSR
454
;; --------------------------------------------------------------------------
455
MIOS_MF_PWM_DutyCycleDownGet
456
	SET_BSR	MIOS_MF_PWM_DUTY_CYCLE_DOWN
457
	movf	MIOS_MF_PWM_DUTY_CYCLE_DOWN, W, BANKED
458
	movwf	MIOS_PARAMETER1
459
	return
460
 
461
;; --------------------------------------------------------------------------
462
;;  FUNCTION: MIOS_MF_PWM_DutyCycleUpSet
463
;;  C_DECLARATION: void MIOS_MF_PWM_DutyCycleUpSet(unsigned char cali_up)
464
;;  DESCRIPTION: Sets the Duty Cycle for upward moves -
465
;;  see http://www.ucapps.de/mbhp_mf.html for detailed informations about this value
466
;;  IN:	  Duty Cycle in WREG
467
;;  C_IN:  Duty Cycle in <cali_up>
468
;;  OUT:  -
469
;;  C_OUT:  -
470
;;  USES: BSR
471
;; --------------------------------------------------------------------------
472
MIOS_MF_PWM_DutyCycleUpSet
473
	SET_BSR	MIOS_AIN_CTRL
474
	btfss	MIOS_AIN_CTRL, MIOS_AIN_CTRL_MF, BANKED
475
	return
476
	SET_BSR	MIOS_MF_PWM_DUTY_CYCLE_UP
477
	movwf	MIOS_MF_PWM_DUTY_CYCLE_UP, BANKED
478
	return
479
 
480
;; --------------------------------------------------------------------------
481
;;  FUNCTION: MIOS_MF_PWM_DutyCycleUpGet
482
;;  C_DECLARATION: unsigned char MIOS_MF_PWM_DutyCycleUpGet(void)
483
;;  DESCRIPTION: Returns the Duty Cycle for upward moves -
484
;;  see http://www.ucapps.de/mbhp_mf.html for detailed informations about this value
485
;;  IN:	  -
486
;;  C_IN:  -
487
;;  OUT:  Duty Cycle in WREG
488
;;  C_OUT:  Duty Cycle
489
;;  USES: BSR
490
;; --------------------------------------------------------------------------
491
MIOS_MF_PWM_DutyCycleUpGet
492
	SET_BSR	MIOS_MF_PWM_DUTY_CYCLE_UP
493
	movf	MIOS_MF_PWM_DUTY_CYCLE_UP, W, BANKED
494
	movwf	MIOS_PARAMETER1
495
	return
496
 
497
 
498
;; --------------------------------------------------------------------------
499
;;  MIOS SRM Tick (called every 100 uS by the AIN handler)
500
;; --------------------------------------------------------------------------
501
MIOS_MF_SRM_Tick
502
	SET_BSR	MIOS_MF_NUMBER
503
 
504
	;; load MF shift register
505
	movff	MIOS_MF_SR1, IRQ_TMP2
506
	movff	MIOS_MF_SR0, IRQ_TMP1
507
	movlw	0x10
508
	movwf	MIOS_MF_NUMBER, BANKED
509
MIOS_MF_SRM_Tick_Loop
510
	btfsc	IRQ_TMP2, 7
511
	bsf	MIOS_MF_LAT_DOUT, MIOS_MF_PIN_DOUT
512
	btfss	IRQ_TMP2, 7
513
	bcf	MIOS_MF_LAT_DOUT, MIOS_MF_PIN_DOUT
514
 
515
	rlf	IRQ_TMP1, F
516
        bsf     MIOS_MF_LAT_SCLK, MIOS_MF_PIN_SCLK	; toggle clock
517
	rlf	IRQ_TMP2, F
518
        bcf     MIOS_MF_LAT_SCLK, MIOS_MF_PIN_SCLK
519
	decfsz	MIOS_MF_NUMBER, F, BANKED		; loop 16 times
520
	rgoto	MIOS_MF_SRM_Tick_Loop
521
 
522
        bsf     MIOS_MF_LAT_RCLK, MIOS_MF_PIN_RCLK	; trigger latch
523
        bcf     MIOS_MF_LAT_RCLK, MIOS_MF_PIN_RCLK
524
 
525
 
526
	;; increment/decrement the MF counters each 800 uS
527
	incf	MIOS_MF_CTR_UPDATE_CTR, F, BANKED	; the "counter update counter"
528
	BRA_IFCLR MIOS_MF_CTR_UPDATE_CTR, 3, BANKED, MIOS_MF_SRM_Tick_NoCtrUpdate
529
MIOS_MF_SRM_Tick_CtrUpdate
530
	clrf	MIOS_MF_CTR_UPDATE_CTR, BANKED
531
	clrf	MIOS_MF_NUMBER, BANKED
532
MIOS_MF_SRM_Tick_CtrUpdate_Loop
533
 
534
	;; --- decrement MANUAL CTR if != 0
535
	lfsr	FSR2, MIOS_MF_MANUAL_CTR0
536
	movf	MIOS_MF_NUMBER, W, BANKED
537
	movf	PLUSW2, F
538
	skpz
539
	decf	PLUSW2, F
540
 
541
	;; --- decrement TIMEOUT if != 0
542
	lfsr	FSR2, MIOS_MF_TIMEOUT_CTR0
543
	movf	MIOS_MF_NUMBER, W, BANKED
544
	movf	PLUSW2, F
545
	skpz
546
	decf	PLUSW2, F
547
 
548
	incf	MIOS_MF_NUMBER, F, BANKED
549
	BRA_IFCLR MIOS_MF_NUMBER, 3, BANKED, MIOS_MF_SRM_Tick_CtrUpdate_Loop
550
 
551
MIOS_MF_SRM_Tick_NoCtrUpdate
552
 
553
	return
554
 
555
 
556
;; ------------------------------------------------------------------------------------------------
557
;;  MIOS MF Motor Tick: motor control routine
558
;; ------------------------------------------------------------------------------------------------
559
MIOS_MF_Tick
560
	;; ----------------------------------------------------------------------------------------
561
	;; prepare variables
562
	;; ----------------------------------------------------------------------------------------
563
 
564
	;; --- store FSR1 in temp. register
565
	movff	FSR1L, MIOS_IRQ_FSR1L
566
	movff	FSR1H, MIOS_IRQ_FSR1H
567
 
568
	;; --- now we are working in the MIOS_MF page
569
	SET_BSR	MIOS_MF_NUMBER
570
 
571
	;; --- copy the AIN input counter to MIOS_MF_NUMBER
572
	movff	MIOS_AIN_INPUT_CTR, MIOS_MF_NUMBER
573
 
574
	;; --- ensure that NUMBER is never greather than 7
575
	movf	MIOS_MF_NUMBER, W, BANKED
576
	andlw	0xf8
577
	skpz
578
MIOS_MF_Tick_EndlessLoop	; force a watchdog timer reset in this case
579
	rgoto	MIOS_MF_Tick_EndlessLoop
580
 
581
	;; --- calc initial pointer to MIOS_MF* page and add MF number
582
	lfsr	FSR2, MIOS_MF_NUMBER & 0xf80
583
	movf	MIOS_MF_NUMBER, W, BANKED
584
	addwf	FSR2L, F
585
 
586
	;; --- store current fader value into MIOS_MF_VALUE_[LH]
587
	movff	ADRESL, MIOS_MF_VALUE_L		; take original value from ADC so that AIN deadband doesn't matter!
588
	movff	ADRESH, MIOS_MF_VALUE_H
589
 
590
	;; -- clear "changed" flag
591
	movlw	MIOS_AIN_RESULT_H_00 & 0x7f
592
	movff	PLUSW2, MIOS_MF_AIN_CHANGED_FLAG; save "changed" flag
593
	bcf	PLUSW2, 7	; clear "changed" flag - MF driver takes control over this semaphore
594
 
595
	;; --- store target position into MIOS_MF_TARGET_POS_[LH]
596
	movlw	MIOS_MF_TARGET_POS_L0 & 0x7f
597
	movff	PLUSW2, MIOS_MF_TARGET_POS_L
598
	movlw	MIOS_MF_TARGET_POS_H0 & 0x7f
599
	movff	PLUSW2, MIOS_MF_TARGET_POS_H
600
 
601
	;; --- store current timeout counter MIOS_MF_TIMEOUT_CTR
602
	movlw	MIOS_MF_TIMEOUT_CTR0 & 0x7f
603
	movff	PLUSW2, MIOS_MF_TIMEOUT_CTR
604
 
605
 
606
	;; ----------------------------------------------------------------------------------------
607
	;; case: motor on target position (REPEAT_CTRx <= 1)
608
	;; ----------------------------------------------------------------------------------------
609
	movlw	MIOS_MF_REPEAT_CTR0 & 0x7f
610
	movf	PLUSW2, W
611
	sublw	1
612
	bnc	MIOS_MF_Tick_NotSnapped
613
MIOS_MF_Tick_Snapped
614
	;; --- motor should go into standby mode
615
	rcall	MIOS_MF_StandBy
616
 
617
	;; if TIMEOUT_CTR == 0, go directly into idle mode
618
	movf	MIOS_MF_TIMEOUT_CTR, W, BANKED
619
	bz	MIOS_MF_SnappedIdle
620
 
621
	;; --- get REPEAT_CTRx again
622
	movlw	MIOS_MF_REPEAT_CTR0 & 0x7f
623
	movf	PLUSW2, W
624
	;; --- special case: REPEAT_CTRx == 1: in this case the motor is just stopping
625
	bz	MIOS_MF_SnappedIdle
626
 
627
	;; --- motor is just stopping, clear CTR
628
	movlw	MIOS_MF_REPEAT_CTR0 & 0x7f
629
	clrf	PLUSW2
630
 
631
#if MIOS_MF_LOG_MOVES
632
	;; log that the target has been reached
633
	movf	MIOS_MF_NUMBER, W, BANKED
634
	bnz	MIOS_MF_Tick_DontLogTarget
635
	movlw	0xff
636
	rcall	MIOS_MF_LOG_Push
637
	movlw	0xff
638
	rcall	MIOS_MF_LOG_Push
639
MIOS_MF_Tick_DontLogTarget
640
#endif
641
 
642
	;; ---> END
643
	rgoto	MIOS_MF_Tick_End
644
 
645
	;; ----------------------------------------------------------------------------------------
646
 
647
MIOS_MF_SnappedIdle
648
	;; --- copy current value into target position, TIMEOUT counter > 0 (reassurance phase)
649
	movf	MIOS_MF_TIMEOUT_CTR, W, BANKED
650
	bz	MIOS_MF_SnappedIdle2
651
	movlw	MIOS_MF_TARGET_POS_L0 & 0x7f
652
	movff	MIOS_MF_VALUE_L, PLUSW2
653
	movlw	MIOS_MF_TARGET_POS_H0 & 0x7f
654
	movff	MIOS_MF_VALUE_H, PLUSW2
655
 
656
	rgoto	MIOS_MF_Tick_End
657
 
658
	;; ----------------------------------------------------------------------------------------
659
 
660
MIOS_MF_SnappedIdle2
661
	;; do nothing if "changed" flag was not set by AIN driver
662
	BRA_IFCLR MIOS_MF_AIN_CHANGED_FLAG, 7, BANKED, MIOS_MF_Tick_End
663
 
664
	;; --- else: save new value
665
	movlw	MIOS_MF_TARGET_POS_L0 & 0x7f
666
	movff	MIOS_MF_VALUE_L, PLUSW2
667
	movlw	MIOS_MF_TARGET_POS_H0 & 0x7f
668
	movff	MIOS_MF_VALUE_H, PLUSW2
669
 
670
	;; --- set manual move counter, so that the motor is not moved during this time
671
	movlw	MIOS_MF_MANUAL_CTR_VALUE
672
	movwf	IRQ_TMP1
673
	movlw	MIOS_MF_MANUAL_CTR0 & 0x7f
674
	movff	IRQ_TMP1, PLUSW2
675
 
676
	;; --- notify change in MIOS_AIN_RESULT_H[7], so that the AIN handler can regognize it
677
	movlw	MIOS_AIN_RESULT_H_00 & 0x7f
678
	bsf	PLUSW2, 7
679
 
680
	;; ---> END
681
	rgoto	MIOS_MF_Tick_End
682
 
683
 
684
	;; ----------------------------------------------------------------------------------------
685
MIOS_MF_Tick_NotSnapped
686
 
687
	;; ----------------------------------------------------------------------------------------
688
	;; case: motor very slow or not moving
689
	;; ----------------------------------------------------------------------------------------
690
	movff	MIOS_AIN_POTSPEED, IRQ_TMP4
691
	movlw	0x01
692
	cpfslt	IRQ_TMP4, ACCESS
693
	rgoto MIOS_MF_Tick_Moving
694
MIOS_MF_Tick_NotMoving
695
	;; --- continue if TIMEOUT > 0
696
	movf	MIOS_MF_TIMEOUT_CTR, W, BANKED
697
	bnz	MIOS_MF_Tick_MovingCont
698
 
699
	;; --- if TIMEOUT == 0, write 0x01 into REPEAT_CTRx for a proper shutdown
700
	movlw	MIOS_MF_REPEAT_CTR0 & 0x7f
701
	clrf	PLUSW2		; (PLUSW2 = 0x01)
702
	incf	PLUSW2, F
703
	rgoto	MIOS_MF_Tick_End
704
 
705
	;; ----------------------------------------------------------------------------------------
706
 
707
MIOS_MF_Tick_Moving
708
	;; --- reload TIMEOUT
709
	movlw	MIOS_MF_TIMEOUT_RELOAD_VALUE
710
	movwf	IRQ_TMP1
711
	movlw	MIOS_MF_TIMEOUT_CTR0 & 0x7f
712
	movff	IRQ_TMP1, PLUSW2
713
MIOS_MF_Tick_MovingCont
714
 
715
	;; ----------------------------------------------------------------------------------------
716
	;; Check Touch Detection. ShutDown Motor if active
717
	;; ----------------------------------------------------------------------------------------
718
	movf	MIOS_MF_NUMBER, W, BANKED
719
	call	MIOS_HLP_GetBitORMask
720
	andwf	MIOS_MF_SUSPEND, W, BANKED
721
	bz	MIOS_MF_Tick_NotSuspended
722
MIOS_MF_Tick_Suspended
723
	;; --- write 0x01 into REPEAT_CTRx for a clean shutdown
724
	movlw	MIOS_MF_REPEAT_CTR0 & 0x7f
725
	clrf	PLUSW2		; (PLUSW2 = 0x01)
726
	incf	PLUSW2, F
727
	;; --- write 0x00 into timeout register
728
	movlw	MIOS_MF_TIMEOUT_CTR0 & 0x7f
729
	clrf	PLUSW2
730
	;; ---> End
731
	rgoto	MIOS_MF_Tick_End
732
 
733
MIOS_MF_Tick_NotSuspended
734
 
735
	;; ----------------------------------------------------------------------------------------
736
	;; Don't move motor if speed > 5/1024
737
	;; ----------------------------------------------------------------------------------------
738
	movff	MIOS_AIN_POTSPEED, IRQ_TMP4
739
	movlw	10
740
	cpfsgt	IRQ_TMP4, ACCESS
741
	rgoto MIOS_MF_Tick_DontReduceSpeed
742
MIOS_MF_Tick_ReduceSpeed
743
	;; --- Dont Move Fader -> Standby
744
	rcall	MIOS_MF_StandBy
745
	;; ---> END
746
	rgoto	MIOS_MF_Tick_End
747
MIOS_MF_Tick_DontReduceSpeed
748
 
749
	;; ----------------------------------------------------------------------------------------
750
	;; case: determine in which direction the motor should be moved
751
	;; ----------------------------------------------------------------------------------------
752
 
753
#if MIOS_MF_LOG_MOVES
754
	;; log move of fader 0
755
	movf	MIOS_MF_NUMBER, W, BANKED
756
	bnz	MIOS_MF_Tick_DontLogValue
757
	movf	MIOS_MF_VALUE_L, W, BANKED
758
	rcall	MIOS_MF_LOG_Push
759
	movf	MIOS_MF_VALUE_H, W, BANKED
760
	rcall	MIOS_MF_LOG_Push
761
MIOS_MF_Tick_DontLogValue
762
#endif
763
 
764
	;; --- special case: if target and current position <= 0x07, stop motor
765
	;; (workaround for ALPS faders which never reach the 0x00 value)
766
	movf	MIOS_MF_TARGET_POS_H, W, BANKED
767
	bnz	MIOS_MF_Tick_NotTargetZero
768
	movf	MIOS_MF_VALUE_H, W, BANKED
769
	bnz	MIOS_MF_Tick_NotTargetZero
770
	movf	MIOS_MF_TARGET_POS_L, W, BANKED
771
	iorwf	MIOS_MF_VALUE_L, W, BANKED
772
	andlw	0xf8
773
	bnz	MIOS_MF_Tick_NotTargetZero
774
	rgoto	MIOS_MF_DontMove
775
MIOS_MF_Tick_NotTargetZero
776
 
777
	;; --- check distance between current and target position
778
	;; --- if fader is in between "deadband", don't move it
779
	;; calc IRQ_TMP_[12] = MIOS_MF_VALUE_[LH] - MIOS_MF_TARGET_POS_[LH]
780
	movf	MIOS_MF_TARGET_POS_L, W, BANKED
781
	subwf	MIOS_MF_VALUE_L, W, BANKED
782
	movwf	IRQ_TMP1
783
	movf	MIOS_MF_TARGET_POS_H, W, BANKED
784
	subwfb	MIOS_MF_VALUE_H, W, BANKED
785
	movwf	IRQ_TMP2
786
	;; if result negative, invert values
787
	bcf	IRQ_TMP3, 0
788
	BRA_IFCLR IRQ_TMP2, 7, ACCESS, MIOS_MF_SVNotNeg
789
	comf	IRQ_TMP1, F
790
	comf	IRQ_TMP2, F
791
	incf	IRQ_TMP1, F
792
	skpnz
793
	incf	IRQ_TMP2, F
794
	bsf	IRQ_TMP3, 0		; set marker for "motor moved up"
795
MIOS_MF_SVNotNeg
796
 
797
	movf	IRQ_TMP1, W
798
	movf	IRQ_TMP2, F
799
	skpz
800
	movlw	0xff
801
	movwf	IRQ_TMP1
802
 
803
	;; dynamic deadband - depends on repeat counter
804
	movlw	MIOS_MF_REPEAT_CTR0 & 0x7f
805
	movff	PLUSW2, IRQ_TMP4
806
	movf	MIOS_MF_DEADBAND, W, BANKED
807
	btfsc	IRQ_TMP4, 2
808
	movlw 0x04
809
	btfsc	IRQ_TMP4, 3
810
	movlw 0x08
811
	btfsc	IRQ_TMP4, 4
812
	movlw 0x20
813
	cpfsgt	IRQ_TMP1, ACCESS
814
	rgoto MIOS_MF_DontMove
815
	rgoto	MIOS_MF_Move
816
 
817
MIOS_MF_DontMove
818
	;; --- Dont Move Fader -> Standby
819
	rcall	MIOS_MF_StandBy
820
 
821
	;; --- Decrement Repeat CTR
822
	movlw	MIOS_MF_REPEAT_CTR0 & 0x7f
823
	decf	PLUSW2, F
824
 
825
	;; ---> END
826
	rgoto	MIOS_MF_Tick_End
827
 
828
MIOS_MF_Move
829
 
830
	;; ----------------------------------------------------------------------------------------
831
	;; slow down motor if distance between current and target position < 0x60
832
	;; ----------------------------------------------------------------------------------------
833
	movlw	0x60
834
	cpfslt	IRQ_TMP1, ACCESS
835
	rgoto MIOS_MF_Tick_NoPWM
836
 
837
	movlw	MIOS_MF_PWM_CTR0 & 0x7f
838
	incf	PLUSW2, F
839
	movff	PLUSW2, IRQ_TMP4
840
	movf	MIOS_MF_PWM_PERIOD, W, BANKED
841
	bz	MIOS_MF_Tick_NoPWM
842
	cpfsgt	IRQ_TMP4, ACCESS
843
	rgoto MIOS_MF_Tick_NoPWMReset
844
MIOS_MF_Tick_PWMReset
845
	clrf	IRQ_TMP4
846
	movlw	MIOS_MF_PWM_CTR0 & 0x7f
847
	movff	IRQ_TMP4, PLUSW2
848
MIOS_MF_Tick_NoPWMReset
849
	movf	MIOS_MF_PWM_DUTY_CYCLE_DOWN, W, BANKED
850
	btfsc	IRQ_TMP3, 0
851
	movf	MIOS_MF_PWM_DUTY_CYCLE_UP, W, BANKED
852
	cpfsgt	IRQ_TMP4, ACCESS
853
	rgoto MIOS_MF_Tick_NoPWM
854
	;; --- Dont Move Fader -> Standby
855
	rcall	MIOS_MF_StandBy
856
	;; ---> END
857
	rgoto	MIOS_MF_Tick_End
858
MIOS_MF_Tick_NoPWM
859
 
860
	;; --- check if fader should be moved up or down
861
	BRA_IFSET IRQ_TMP3, 0, ACCESS, MIOS_MF_MoveUp	; check marker for "motor moved up"
862
 
863
MIOS_MF_MoveDown
864
	;; ---> save direction
865
	movlw	MIOS_MF_STATE0 & 0x7f
866
	bcf	PLUSW2, 0
867
	;; ---> Move Fader Down
868
	rcall	MIOS_MF_Down
869
	;; ---> END
870
	rgoto	MIOS_MF_Tick_End
871
 
872
MIOS_MF_MoveUp
873
	;; ---> save direction
874
	movlw	MIOS_MF_STATE0 & 0x7f
875
	bsf	PLUSW2, 0
876
	;; ---> Move Fader Up
877
	rcall	MIOS_MF_Up
878
	;; ---> END
879
	;; 	goto	MIOS_MF_Tick_End
880
 
881
MIOS_MF_Tick_End
882
	;; restore FSR1 from temp. register
883
	movff	MIOS_IRQ_FSR1L, FSR1L
884
	movff	MIOS_IRQ_FSR1H, FSR1H
885
 
886
	rgoto	MIOS_MF_Tick_Return
887
 
888
 
889
 
890
;; --------------------------------------------------------------------------
891
;; this is a result of a code optimisation
892
;; requirements:
893
;;    - can be used by MIOS_MF_StandBy/MIOS_MF_Down and MIOS_MF_Up
894
;;    - only FSR1 pointer access allowed
895
;;    - beside of WREG no other register used
896
 
897
;; expecting UP/DOWN bit in WREG[1:0]
898
MIOS_MF_UpDownStandby_Mod
899
	lfsr	FSR1, MIOS_MF_SR0
900
	btfsc	MIOS_MF_NUMBER, 2, BANKED
901
	lfsr	FSR1, MIOS_MF_SR1
902
 
903
	BRA_IFSET MIOS_MF_NUMBER, 1, BANKED, MIOS_MF_UpDownStandby_Mod_3478
904
MIOS_MF_UpDownStandby_Mod_1256
905
	BRA_IFSET MIOS_MF_NUMBER, 0, BANKED, MIOS_MF_UpDownStandby_Mod_26
906
MIOS_MF_UpDownStandby_Mod_15
907
	bcf	INDF1, MIOS_MF_PIN_M1_UP
908
	bcf	INDF1, MIOS_MF_PIN_M1_DOWN
909
	btfsc	WREG, MIOS_MF_PIN_Mx_UP
910
	bsf	INDF1, MIOS_MF_PIN_M1_UP
911
	btfsc	WREG, MIOS_MF_PIN_Mx_DOWN
912
	bsf	INDF1, MIOS_MF_PIN_M1_DOWN
913
	return
914
 
915
MIOS_MF_UpDownStandby_Mod_26
916
	bcf	INDF1, MIOS_MF_PIN_M2_UP
917
	bcf	INDF1, MIOS_MF_PIN_M2_DOWN
918
	btfsc	WREG, MIOS_MF_PIN_Mx_UP
919
	bsf	INDF1, MIOS_MF_PIN_M2_UP
920
	btfsc	WREG, MIOS_MF_PIN_Mx_DOWN
921
	bsf	INDF1, MIOS_MF_PIN_M2_DOWN
922
	return
923
 
924
MIOS_MF_UpDownStandby_Mod_3478
925
	BRA_IFSET MIOS_MF_NUMBER, 0, BANKED, MIOS_MF_UpDownStandby_Mod_48
926
MIOS_MF_UpDownStandby_Mod_37
927
	bcf	INDF1, MIOS_MF_PIN_M3_UP
928
	bcf	INDF1, MIOS_MF_PIN_M3_DOWN
929
	btfsc	WREG, MIOS_MF_PIN_Mx_UP
930
	bsf	INDF1, MIOS_MF_PIN_M3_UP
931
	btfsc	WREG, MIOS_MF_PIN_Mx_DOWN
932
	bsf	INDF1, MIOS_MF_PIN_M3_DOWN
933
	return
934
 
935
MIOS_MF_UpDownStandby_Mod_48
936
	bcf	INDF1, MIOS_MF_PIN_M4_UP
937
	bcf	INDF1, MIOS_MF_PIN_M4_DOWN
938
	btfsc	WREG, MIOS_MF_PIN_Mx_UP
939
	bsf	INDF1, MIOS_MF_PIN_M4_UP
940
	btfsc	WREG, MIOS_MF_PIN_Mx_DOWN
941
	bsf	INDF1, MIOS_MF_PIN_M4_DOWN
942
	return
943
 
944
 
945
;; --------------------------------------------------------------------------
946
MIOS_MF_Up
947
	movlw	(1 << MIOS_MF_PIN_Mx_UP)
948
	rgoto	MIOS_MF_UpDownStandby_Mod
949
 
950
;; --------------------------------------------------------------------------
951
MIOS_MF_Down
952
	movlw	(1 << MIOS_MF_PIN_Mx_DOWN)
953
	rgoto	MIOS_MF_UpDownStandby_Mod
954
 
955
;; --------------------------------------------------------------------------
956
MIOS_MF_StandBy
957
	movlw	0
958
	rgoto	MIOS_MF_UpDownStandby_Mod