PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rot_enc_trace.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 
32 /** \file
33 Demonstrates use of an ISR trace buffer for capturing the states of
34 a rotary encoder.
35 */
36 
37 #define TMAX 16
38 volatile uint8_t au8_tbuff[TMAX];
39 volatile uint8_t u8_tcnt = 0;
40 volatile uint8_t u8_startTrace = 0;
41 
42 //clips cntr between 0 and max
43 uint8_t processRotaryData(volatile uint8_t u8_curr, volatile uint8_t u8_last,
44  volatile uint8_t *cntr, volatile uint8_t max) {
45  int8_t delta = 0;
46  if (u8_startTrace && (u8_tcnt != TMAX)) {
47  au8_tbuff[u8_tcnt] = u8_curr;
48  u8_tcnt++;
49  }
50  switch (u8_curr) {
51  case 0:
52  if (u8_last == 1) delta = 1;
53  else if (u8_last == 2) delta = -1;
54  break;
55  case 1:
56  if (u8_last == 3) delta = 1;
57  else if (u8_last == 0) delta = -1;
58  break;
59  case 2:
60  if (u8_last == 0) delta = 1;
61  else if (u8_last == 3) delta = -1;
62  break;
63  case 3:
64  if (u8_last == 2) delta = 1;
65  else if (u8_last == 1) delta = -1;
66  break;
67  default:
68  break;
69  }
70  if (delta == 0) return(1); //error, illegal state
71  //clip and update.
72  if (( *cntr == 0 && delta == -1)
73  || (*cntr == max && delta == 1)) return(0); //at limit
74  (*cntr) = (*cntr) + delta;
75  return 0;
76 }
77 
78 #define ROT1_RAW _RB13
79 #define ROT0_RAW _RB12
80 #define GET_ROT_STATE() ((ROT1_RAW << 1) | ROT0_RAW)
81 
82 /// ROT1 configuration
83 inline void CONFIG_ROTENC() {
84  CONFIG_RB13_AS_DIG_INPUT();
85  ENABLE_RB13_PULLUP(); //enable the pullup
86  CONFIG_RB12_AS_DIG_INPUT();
87  ENABLE_RB12_PULLUP(); //enable the pullup
88  DELAY_US(1); //wait for pullups to settle
89 }
90 
91 #define ROT_MAX 32 //arbitrary limit
92 
93 volatile uint8_t u8_valueROT = 0;
94 volatile uint8_t u8_lastvalueROT = 0;
95 volatile uint8_t u8_errROT = 0;
96 volatile uint8_t u8_cntrROT = 0;
97 
98 //Interrupt Service Routine for Timer3
99 void _ISRFAST _T3Interrupt (void) {
100  u8_valueROT = GET_ROT_STATE(); //a value between 0 & 3
101  if (u8_lastvalueROT != u8_valueROT) {
102  u8_errROT = processRotaryData(u8_valueROT, u8_lastvalueROT, &u8_cntrROT, ROT_MAX);
103  u8_lastvalueROT = u8_valueROT;
104  }
105  _T3IF = 0; //clear the timer interrupt bit
106 }
107 
108 
109 #define ISR_PERIOD 15 // in ms
110 void configTimer3(void) {
111  //ensure that Timer2,3 configured as separate timers.
112  T2CONbits.T32 = 0; // 32-bit mode off
113  //T3CON set like this for documentation purposes.
114  //could be replaced by T3CON = 0x0020
115  T3CON = T3_OFF | T3_IDLE_CON | T3_GATE_OFF
116  | T3_SOURCE_INT
117  | T3_PS_1_64 ; //results in T3CON= 0x0020
118  PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
119  TMR3 = 0; //clear timer3 value
120  _T3IF = 0; //clear interrupt flag
121  _T3IP = 1; //choose a priority
122  _T3IE = 1; //enable the interrupt
123  T3CONbits.TON = 1; //turn on the timer
124 }
125 
126 int main (void) {
127  uint8_t u8_i;
128  configBasic(HELLO_MSG);
129  /** PIO config ******/
130  CONFIG_ROTENC();
131  DELAY_US(1); //wait for pullups to settle
132  u8_valueROT = GET_ROT_STATE();
133  u8_lastvalueROT = u8_valueROT;
134  /** Configure the Timer */
135  configTimer3();
136  while (1) {
137  u8_startTrace = 1;
138  if (u8_tcnt == TMAX) {
139  u8_startTrace = 0;
140  for (u8_i = 0; u8_i < TMAX; u8_i++) {
141  outUint8(au8_tbuff[u8_i]);
142  outString("\n");
143  }
144  u8_tcnt = 0;
145  }
146  doHeartbeat(); //ensure that we are alive
147  } // end while (1)
148 }