00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "pic24_all.h"
00031
00037 #define TMAX 16
00038 volatile uint8 au8_tbuff[TMAX];
00039 volatile uint8 u8_tcnt = 0;
00040 volatile uint8 u8_startTrace = 0;
00041
00042
00043 uint8 processRotaryData(volatile uint8 u8_curr, volatile uint8 u8_last,
00044 volatile uint8 *cntr, volatile uint8 max) {
00045 int8 delta = 0;
00046 if (u8_startTrace && (u8_tcnt != TMAX)) {
00047 au8_tbuff[u8_tcnt] = u8_curr;
00048 u8_tcnt++;
00049 }
00050 switch (u8_curr) {
00051 case 0:
00052 if (u8_last == 1) delta = 1;
00053 else if (u8_last == 2) delta = -1;
00054 break;
00055 case 1:
00056 if (u8_last == 3) delta = 1;
00057 else if (u8_last == 0) delta = -1;
00058 break;
00059 case 2:
00060 if (u8_last == 0) delta = 1;
00061 else if (u8_last == 3) delta = -1;
00062 break;
00063 case 3:
00064 if (u8_last == 2) delta = 1;
00065 else if (u8_last == 1) delta = -1;
00066 break;
00067 default:
00068 break;
00069 }
00070 if (delta == 0) return(1);
00071
00072 if (( *cntr == 0 && delta == -1)
00073 || (*cntr == max && delta == 1)) return(0);
00074 (*cntr) = (*cntr) + delta;
00075 return 0;
00076 }
00077
00078 #define ROT1_RAW _RB13
00079 #define ROT0_RAW _RB12
00080 #define GET_ROT_STATE() ((ROT1_RAW << 1) | ROT0_RAW)
00081
00083 inline void CONFIG_ROTENC() {
00084 CONFIG_RB13_AS_DIG_INPUT();
00085 ENABLE_RB13_PULLUP();
00086 CONFIG_RB12_AS_DIG_INPUT();
00087 ENABLE_RB12_PULLUP();
00088 DELAY_US(1);
00089 }
00090
00091 #define ROT_MAX 32 //arbitrary limit
00092
00093 volatile uint8 u8_valueROT = 0;
00094 volatile uint8 u8_lastvalueROT = 0;
00095 volatile uint8 u8_errROT = 0;
00096 volatile uint8 u8_cntrROT = 0;
00097
00098
00099 void _ISRFAST _T3Interrupt (void) {
00100 u8_valueROT = GET_ROT_STATE();
00101 if (u8_lastvalueROT != u8_valueROT) {
00102 u8_errROT = processRotaryData(u8_valueROT, u8_lastvalueROT, &u8_cntrROT, ROT_MAX);
00103 u8_lastvalueROT = u8_valueROT;
00104 }
00105 _T3IF = 0;
00106 }
00107
00108
00109 #define ISR_PERIOD 15 // in ms
00110 void configTimer3(void) {
00111
00112 T2CONbits.T32 = 0;
00113
00114
00115 T3CON = T3_OFF | T3_IDLE_CON | T3_GATE_OFF
00116 | T3_SOURCE_INT
00117 | T3_PS_1_64 ;
00118 PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
00119 TMR3 = 0;
00120 _T3IF = 0;
00121 _T3IP = 1;
00122 _T3IE = 1;
00123 T3CONbits.TON = 1;
00124 }
00125
00126 int main (void) {
00127 uint8 u8_i;
00128 configBasic(HELLO_MSG);
00130 CONFIG_ROTENC();
00131 DELAY_US(1);
00132 u8_valueROT = GET_ROT_STATE();
00133 u8_lastvalueROT = u8_valueROT;
00135 configTimer3();
00136 while (1) {
00137 u8_startTrace = 1;
00138 if (u8_tcnt == TMAX) {
00139 u8_startTrace = 0;
00140 for (u8_i = 0; u8_i < TMAX; u8_i++) {
00141 outUint8(au8_tbuff[u8_i]);
00142 outString("\n");
00143 }
00144 u8_tcnt = 0;
00145 }
00146 doHeartbeat();
00147 }
00148 }