PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rot_enc.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 A 2-bit incremental Gray code rotary encoder example
34 */
35 
36 //clips cntr between 0 and max
37 uint8_t processRotaryData(volatile uint8_t u8_curr, volatile uint8_t u8_last,
38  volatile uint8_t *cntr, volatile uint8_t max) {
39  int8_t delta = 0;
40  //states listed in Gray code order for clarity
41  switch (u8_curr) {
42  case 0:
43  if (u8_last == 1) delta = 1;
44  else if (u8_last == 2) delta = -1;
45  break;
46  case 1:
47  if (u8_last == 3) delta = 1;
48  else if (u8_last == 0) delta = -1;
49  break;
50  case 3:
51  if (u8_last == 2) delta = 1;
52  else if (u8_last == 1) delta = -1;
53  break;
54  case 2:
55  if (u8_last == 0) delta = 1;
56  else if (u8_last == 3) delta = -1;
57  break;
58  default:
59  break;
60  }
61  if (delta == 0) return(1); //error, illegal state
62  //clip and update.
63  if (( *cntr == 0 && delta == -1)
64  || (*cntr == max && delta == 1)) return 0; //at limit
65  (*cntr) = (*cntr) + delta;
66  return 0;
67 }
68 
69 #define ROT1_RAW _RB13
70 #define ROT0_RAW _RB12
71 #define GET_ROT_STATE() ((ROT1_RAW << 1) | ROT0_RAW)
72 
73 /// ROT1 configuration
74 inline void configRotaryEncoder() {
75  CONFIG_RB13_AS_DIG_INPUT();
76  ENABLE_RB13_PULLUP(); //enable the pullup
77  CONFIG_RB12_AS_DIG_INPUT();
78  ENABLE_RB12_PULLUP(); //enable the pullup
79  DELAY_US(1); //wait for pullups to settle
80 }
81 
82 #define ROT_MAX 32 //arbitrary limit
83 
84 volatile uint8_t u8_valueROT = 0;
85 volatile uint8_t u8_lastvalueROT = 0;
86 volatile uint8_t u8_errROT = 0;
87 volatile uint8_t u8_cntrROT = 0;
88 
89 //Interrupt Service Routine for Timer3
90 void _ISRFAST _T3Interrupt (void) {
91  u8_valueROT = GET_ROT_STATE(); //a value between 0 & 3
92  if (u8_lastvalueROT != u8_valueROT) {
93  u8_errROT = processRotaryData(u8_valueROT, u8_lastvalueROT, &u8_cntrROT, ROT_MAX);
94  u8_lastvalueROT = u8_valueROT;
95  }
96  _T3IF = 0; //clear the timer interrupt bit
97 }
98 
99 
100 #define ISR_PERIOD 15 // in ms
101 void configTimer3(void) {
102  //ensure that Timer2,3 configured as separate timers.
103  T2CONbits.T32 = 0; // 32-bit mode off
104  //T3CON set like this for documentation purposes.
105  //could be replaced by T3CON = 0x0020
106  T3CON = T3_OFF | T3_IDLE_CON | T3_GATE_OFF
107  | T3_SOURCE_INT
108  | T3_PS_1_64 ; //results in T3CON= 0x0020
109  PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
110  TMR3 = 0; //clear timer3 value
111  _T3IF = 0; //clear interrupt flag
112  _T3IP = 1; //choose a priority
113  _T3IE = 1; //enable the interrupt
114  T3CONbits.TON = 1; //turn on the timer
115 }
116 
117 int main (void) {
118  uint8_t u8_lastCnt;
119  configBasic(HELLO_MSG);
120  /** PIO config ******/
122  u8_valueROT = GET_ROT_STATE();
123  u8_lastvalueROT = u8_valueROT;
124  u8_lastCnt = u8_cntrROT;
125  /** Configure the Timer */
126  configTimer3();
127  while (1) {
128  if (u8_lastCnt != u8_cntrROT) {
129  u8_lastCnt = u8_cntrROT;
130  outUint8(u8_lastCnt);
131  outString("\n");
132  if (u8_errROT) {
133  outString("Rotary state error\n");
134  u8_errROT = 0;
135  }
136  }
137  doHeartbeat(); //indicate that we are alive
138  } // end while (1)
139 }