-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder.c
executable file
·920 lines (844 loc) · 24 KB
/
encoder.c
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/*******************************************************************************
*
* TITLE: encoder.c
*
* VERSION: 0.6 (Beta)
*
* DATE: 15-Jan-2008
*
* AUTHOR: R. Kevin Watson
*
* COMMENTS: This version is compatible with Microchip C18 3.0+ only.
*
* This file best viewed with tabs set to four.
*
* You are free to use this source code for any non-commercial
* use. Please do not make copies of this source code, modified
* or un-modified, publicly available on the internet or
* elsewhere without permission. Thanks.
*
* Copyright ©2003-2008 R. Kevin Watson. All rights are reserved.
*
********************************************************************************
*
* CHANGE LOG:
*
* DATE REV DESCRIPTION
* ----------- --- ----------------------------------------------------------
* 20-Dec-2003 0.1 RKW - Original code.
* 18-Feb-2004 0.2 RKW - Reassigned the encoder digital inputs to run
* on the FRC robot controller too.
* 01-Jan-2005 0.3 RKW - Get_Left_Encoder_Count(), Get_Right_Encoder_Count(),
* Reset_Left_Encoder_Count() and Reset_Right_Encoder_Count()
* functions added.
* 01-Jan-2005 0.3 RKW - Renamed Int_1_Handler() and Int_2_Handler() to
* Left_Encoder_Int_Handler() and Right_Encoder_Int_Handler
* respectively.
* 01-Jan-2005 0.3 RKW - Altered the interrupt service routines to easily
* flip the direction the encoders count by altering the
* RIGHT_ENCODER_TICK_DELTA and LEFT_ENCODER_TICK_DELTA
* #defines found in encoder.h
* 06-Jan-2005 0.4 RKW - Rebuilt with C18 version 2.40.
* 17-Dec-2005 0.5 RKW - Added code to accomodate four more encoder on
* interrupts 3 through 6. These additional encoder inputs
* are optimized for position control.
* 13-Jan-2006 0.5 RKW - Verified code works properly on new PIC18F8722-
* based robot controllers.
* 15-Jan-2008 0.6 RKW - renamed ISRs to be compatible with new robot
* controller code; restructured the ENABLE_ENCODER_n
* scheme to be compatible with new robot controller code.
*
*******************************************************************************/
#include "ifi_frc.h"
#include "encoder.h"
// These variables are used to keep track of the encoder position over time.
// Though these are global variables, they shouldn't be modified directly.
// Functions to modify these variables are included below.
#ifdef ENABLE_ENCODER_1
volatile long Encoder_1_Count = 0;
#endif
#ifdef ENABLE_ENCODER_2
volatile long Encoder_2_Count = 0;
#endif
#ifdef ENABLE_ENCODER_3
unsigned char Encoder_3_State;
volatile long Encoder_3_Count = 0;
#endif
#ifdef ENABLE_ENCODER_4
unsigned char Encoder_4_State;
volatile long Encoder_4_Count = 0;
#endif
#ifdef ENABLE_ENCODER_5
unsigned char Encoder_5_State;
volatile long Encoder_5_Count = 0;
#endif
#ifdef ENABLE_ENCODER_6
unsigned char Encoder_6_State;
volatile long Encoder_6_Count = 0;
#endif
/*******************************************************************************
*
* FUNCTION: Initialize_Encoder_1()
*
* PURPOSE: Initializes encoder #1.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_1
void Initialize_Encoder_1(void)
{
// make sure interrupt 1 is configured as an input
TRISBbits.TRISB2 = 1;
// interrupt 1 is low priority
INTCON3bits.INT2IP = 0;
// trigger on rising edge
INTCON2bits.INTEDG2 = 1;
// make sure the interrupt flag is reset before enabling
INTCON3bits.INT2IF = 0;
// enable interrupt 1
INTCON3bits.INT2IE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Initialize_Encoder_2()
*
* PURPOSE: Initializes encoder #2.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_2
void Initialize_Encoder_2(void)
{
// make sure interrupt 2 is configured as an input
TRISBbits.TRISB3 = 1;
// interrupt 2 is low priority
INTCON2bits.INT3IP = 0;
// trigger on rising edge
INTCON2bits.INTEDG3 = 1;
// make sure the interrupt flag is reset before enabling
INTCON3bits.INT3IF = 0;
// enable interrupt 2
INTCON3bits.INT3IE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Initialize_Encoder_3_6()
*
* PURPOSE: Initializes encoders 3 through 6.
*
* CALLED FROM: main.c/Initialization()
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_3_6
void Initialize_Encoder_3_6(void)
{
// make sure interrupts 3 through 6 are configured as inputs
TRISBbits.TRISB4 = 1;
TRISBbits.TRISB5 = 1;
TRISBbits.TRISB6 = 1;
TRISBbits.TRISB7 = 1;
// interrupts 3 through 6 are low priority
INTCON2bits.RBIP = 0;
// make sure the interrupt flag is reset before enabling
INTCONbits.RBIF = 0;
// enable interrupts 3 through 6
INTCONbits.RBIE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Get_Encoder_1_Count()
*
* PURPOSE: Gets the current number of encoder 1 counts.
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Number of encoder 1 counts since the last reset.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_1
long Get_Encoder_1_Count(void)
{
long count;
// Since we're about to access the Encoder_1_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_1_Count variable doesn't get altered while
// we're using it.
INTCON3bits.INT2IE = 0;
// Now we can get a local copy of the encoder count without fear
// that we'll get corrupted data.
count = Encoder_1_Count;
// Okay, we have a local copy of the encoder count, so turn the
// encoder's interrupt back on.
INTCON3bits.INT2IE = 1;
// Return the encoder count to the caller.
return(count);
}
#endif
/*******************************************************************************
*
* FUNCTION: Reset_Encoder_1_Count()
*
* PURPOSE: Resets the encoder 1 count to zero
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_1
void Reset_Encoder_1_Count(void)
{
// Since we're about to access the Encoder_1_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_1_Count variable doesn't get altered while
// we're using it.
INTCON3bits.INT2IE = 0;
// Now we can set the value of the encoder count without fear
// that we'll write corrupted data.
Encoder_1_Count = 0;
// Okay, we're done updating the encoder count, so turn the
// left encoder's interrupt back on.
INTCON3bits.INT2IE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Int_1_ISR()
*
* PURPOSE: If enabled, the encoder 1 interrupt handler is called when
* the interrupt 1 pin changes from a logic 0 to a logic 1.
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_1
#pragma tmpdata low_isr_tmpdata
void Int_1_ISR(void)
{
// Encoder 1's phase a signal just transitioned from zero to one, causing
// this interrupt service routine to be called. We know that the encoder
// just rotated one count or "tick" so now check the logical state of the
// phase b signal to determine which way the the encoder shaft rotated.
if(ENCODER_1_PHASE_B_PIN == 0)
{
Encoder_1_Count -= ENCODER_1_TICK_DELTA;
}
else
{
Encoder_1_Count += ENCODER_1_TICK_DELTA;
}
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Get_Encoder_2_Count()
*
* PURPOSE: Gets the current number of encoder 2 counts.
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Number of encoder 2 counts since the last reset.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_2
long Get_Encoder_2_Count(void)
{
long count;
// Since we're about to access the Encoder_2_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_2_Count variable doesn't get altered while
// we're using it.
INTCON3bits.INT3IE = 0;
// Now we can get a local copy of the encoder count without fear
// that we'll get corrupted data.
count = Encoder_2_Count;
// Okay, we have a local copy of the encoder count, so turn the
// encoder's interrupt back on.
INTCON3bits.INT3IE = 1;
// Return the encoder count to the caller.
return(count);
}
#endif
/*******************************************************************************
*
* FUNCTION: Reset_Encoder_2_Count()
*
* PURPOSE: Resets the encoder 2 count to zero
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_2
void Reset_Encoder_2_Count(void)
{
// Since we're about to access the Encoder_2_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_2_Count variable doesn't get altered while
// we're using it.
INTCON3bits.INT3IE = 0;
// Now we can set the value of the encoder count without fear
// that we'll write corrupted data.
Encoder_2_Count = 0;
// Okay, we're done updating the encoder count, so turn the
// left encoder's interrupt back on.
INTCON3bits.INT3IE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Int_2_ISR()
*
* PURPOSE: If enabled, the encoder 2 interrupt handler is called when
* the interrupt 2 pin changes from a logic 0 to a logic 1.
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_2
#pragma tmpdata low_isr_tmpdata
void Int_2_ISR(void)
{
// Encoder 2's phase a signal just transitioned from zero to one, causing
// this interrupt service routine to be called. We know that the encoder
// just rotated one count or "tick" so now check the logical state of the
// phase b signal to determine which way the the encoder shaft rotated.
if(ENCODER_2_PHASE_B_PIN == 0)
{
Encoder_2_Count -= ENCODER_2_TICK_DELTA;
}
else
{
Encoder_2_Count += ENCODER_2_TICK_DELTA;
}
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Get_Encoder_3_Count()
*
* PURPOSE: Gets the current number of encoder 3 counts.
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Number of encoder 3 counts since the last reset.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_3
long Get_Encoder_3_Count(void)
{
long count;
// Since we're about to access the Encoder_3_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_3_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can get a local copy of the encoder count without fear
// that we'll get corrupted data.
count = Encoder_3_Count;
// Okay, we have a local copy of the encoder count, so turn the
// encoder's interrupt back on.
INTCONbits.RBIE = 1;
// Return the encoder count to the caller.
return(count);
}
#endif
/*******************************************************************************
*
* FUNCTION: Reset_Encoder_3_Count()
*
* PURPOSE: Resets the encoder 3 count to zero
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_3
void Reset_Encoder_3_Count(void)
{
// Since we're about to access the Encoder_3_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_3_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can set the value of the encoder count without fear
// that we'll write corrupted data.
Encoder_3_Count = 0;
// Okay, we're done updating the encoder count, so turn the
// left encoder's interrupt back on.
INTCONbits.RBIE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Int_3_ISR()
*
* PURPOSE: If enabled, the encoder 3 interrupt handler is called when
* the interrupt 3 pin changes logic state
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_3
#pragma tmpdata low_isr_tmpdata
void Int_3_ISR(unsigned char state)
{
// Encoder 3's phase a signal just changed logic level, causing this
// interrupt service routine to be called.
if(state == 1)
{
// rising-edge interrupt
if(ENCODER_3_PHASE_B_PIN == 0)
{
// backward
if(Encoder_3_State == 1)
{
Encoder_3_Count -= ENCODER_3_TICK_DELTA;
}
}
else
{
// forward
if(Encoder_3_State == 0)
{
Encoder_3_Count += ENCODER_3_TICK_DELTA;
}
}
}
else
{
// falling-edge interrupt
// phase b is zero if moving forward
// phase b is one if moving backward
Encoder_3_State = ENCODER_3_PHASE_B_PIN;
}
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Get_Encoder_4_Count()
*
* PURPOSE: Gets the current number of encoder 4 counts.
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Number of encoder 4 counts since the last reset.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_4
long Get_Encoder_4_Count(void)
{
long count;
// Since we're about to access the Encoder_4_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_4_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can get a local copy of the encoder count without fear
// that we'll get corrupted data.
count = Encoder_4_Count;
// Okay, we have a local copy of the encoder count, so turn the
// encoder's interrupt back on.
INTCONbits.RBIE = 1;
// Return the encoder count to the caller.
return(count);
}
#endif
/*******************************************************************************
*
* FUNCTION: Reset_Encoder_4_Count()
*
* PURPOSE: Resets the encoder 4 count to zero
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_4
void Reset_Encoder_4_Count(void)
{
// Since we're about to access the Encoder_4_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_4_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can set the value of the encoder count without fear
// that we'll write corrupted data.
Encoder_4_Count = 0;
// Okay, we're done updating the encoder count, so turn the
// left encoder's interrupt back on.
INTCONbits.RBIE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Int_4_ISR()
*
* PURPOSE: If enabled, the encoder 4 interrupt handler is called when
* the interrupt 4 pin changes logic state
*
* CALLED FROM: ifi_frc.c/Interrupt_Handler_Low()
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_4
#pragma tmpdata low_isr_tmpdata
void Int_4_ISR(unsigned char state)
{
// Encoder 4's phase a signal just changed logic level, causing this
// interrupt service routine to be called.
if(state == 1)
{
// rising-edge interrupt
if(ENCODER_4_PHASE_B_PIN == 0)
{
// backward
if(Encoder_4_State == 1)
{
Encoder_4_Count -= ENCODER_4_TICK_DELTA;
}
}
else
{
// forward
if(Encoder_4_State == 0)
{
Encoder_4_Count += ENCODER_4_TICK_DELTA;
}
}
}
else
{
// falling-edge interrupt
// phase b is zero if moving forward
// phase b is one if moving backward
Encoder_4_State = ENCODER_4_PHASE_B_PIN;
}
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Get_Encoder_5_Count()
*
* PURPOSE: Gets the current number of encoder 5 counts.
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Number of encoder 5 counts since the last reset.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_5
long Get_Encoder_5_Count(void)
{
long count;
// Since we're about to access the Encoder_5_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_5_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can get a local copy of the encoder count without fear
// that we'll get corrupted data.
count = Encoder_5_Count;
// Okay, we have a local copy of the encoder count, so turn the
// encoder's interrupt back on.
INTCONbits.RBIE = 1;
// Return the encoder count to the caller.
return(count);
}
#endif
/*******************************************************************************
*
* FUNCTION: Reset_Encoder_5_Count()
*
* PURPOSE: Resets the encoder 5 count to zero
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_5
void Reset_Encoder_5_Count(void)
{
// Since we're about to access the Encoder_5_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_5_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can set the value of the encoder count without fear
// that we'll write corrupted data.
Encoder_5_Count = 0;
// Okay, we're done updating the encoder count, so turn the
// left encoder's interrupt back on.
INTCONbits.RBIE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Int_5_ISR()
*
* PURPOSE: If enabled, the encoder 5 interrupt handler is called when
* the interrupt 5 pin changes logic state
*
* CALLED FROM: user_routines_fast.c/InterruptHandlerLow()
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_5
#pragma tmpdata low_isr_tmpdata
void Int_5_ISR(unsigned char state)
{
// Encoder 5's phase a signal just changed logic level, causing this
// interrupt service routine to be called.
if(state == 1)
{
// rising-edge interrupt
if(ENCODER_5_PHASE_B_PIN == 0)
{
Encoder_5_Count -= ENCODER_5_TICK_DELTA;
}
else
{
Encoder_5_Count += ENCODER_5_TICK_DELTA;
}
}
else
{
// falling-edge interrupt
if(ENCODER_5_PHASE_B_PIN == 0)
{
Encoder_5_Count += ENCODER_5_TICK_DELTA;
}
else
{
Encoder_5_Count -= ENCODER_5_TICK_DELTA;
}
}
}
#pragma tmpdata
#endif
/*******************************************************************************
*
* FUNCTION: Get_Encoder_6_Count()
*
* PURPOSE: Gets the current number of encoder 6 counts.
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Number of encoder 6 counts since the last reset.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_6
long Get_Encoder_6_Count(void)
{
long count;
// Since we're about to access the Encoder_6_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_6_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can get a local copy of the encoder count without fear
// that we'll get corrupted data.
count = Encoder_6_Count;
// Okay, we have a local copy of the encoder count, so turn the
// encoder's interrupt back on.
INTCONbits.RBIE = 1;
// Return the encoder count to the caller.
return(count);
}
#endif
/*******************************************************************************
*
* FUNCTION: Reset_Encoder_6_Count()
*
* PURPOSE: Resets the encoder 6 count to zero
*
* CALLED FROM:
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_6
void Reset_Encoder_6_Count(void)
{
// Since we're about to access the Encoder_6_Count variable,
// which can also be modified in the interrupt service routine,
// we'll briefly disable the encoder's interrupt to make sure
// that the Encoder_6_Count variable doesn't get altered while
// we're using it.
INTCONbits.RBIE = 0;
// Now we can set the value of the encoder count without fear
// that we'll write corrupted data.
Encoder_6_Count = 0;
// Okay, we're done updating the encoder count, so turn the
// left encoder's interrupt back on.
INTCONbits.RBIE = 1;
}
#endif
/*******************************************************************************
*
* FUNCTION: Int_6_ISR()
*
* PURPOSE: If enabled, the encoder 6 interrupt handler is called when
* the interrupt 6 pin changes logic state
*
* CALLED FROM: user_routines_fast.c/InterruptHandlerLow()
*
* PARAMETERS: None.
*
* RETURNS: Nothing.
*
* COMMENTS:
*
*******************************************************************************/
#ifdef ENABLE_ENCODER_6
#pragma tmpdata low_isr_tmpdata
void Int_6_ISR(unsigned char state)
{
// Encoder 6's phase a signal just changed logic level, causing this
// interrupt service routine to be called.
if(state == 1)
{
// rising-edge interrupt
if(ENCODER_6_PHASE_B_PIN == 0)
{
Encoder_6_Count -= ENCODER_6_TICK_DELTA;
}
else
{
Encoder_6_Count += ENCODER_6_TICK_DELTA;
}
}
else
{
// falling-edge interrupt
if(ENCODER_6_PHASE_B_PIN == 0)
{
Encoder_6_Count += ENCODER_6_TICK_DELTA;
}
else
{
Encoder_6_Count -= ENCODER_6_TICK_DELTA;
}
}
}
#pragma tmpdata
#endif