00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <avr/io.h>
00023 #include <avr/interrupt.h>
00024
00025 #include "global.h"
00026 #include "encoder.h"
00027
00028
00029
00030
00031 volatile EncoderStateType EncoderState[NUM_ENCODERS];
00032
00033
00034
00035
00036
00037 void encoderInit(void)
00038 {
00039 u08 i;
00040
00041
00042 for(i=0; i<NUM_ENCODERS; i++)
00043 {
00044 EncoderState[i].position = 0;
00045
00046 }
00047
00048
00049
00050
00051
00052
00053
00054 #ifdef ENC0_SIGNAL
00055
00056 cbi(ENC0_PHASEA_DDR, ENC0_PHASEA_PIN);
00057 sbi(ENC0_PHASEA_PORT, ENC0_PHASEA_PIN);
00058
00059 cbi(ENC0_PHASEB_DDR, ENC0_PHASEB_PIN);
00060 sbi(ENC0_PHASEB_PORT, ENC0_PHASEB_PIN);
00061
00062 sbi(ENC0_ICR, ENC0_ISCX0);
00063 cbi(ENC0_ICR, ENC0_ISCX1);
00064
00065 sbi(IMSK, ENC0_INT);
00066 #endif
00067 #ifdef ENC1_SIGNAL
00068
00069 cbi(ENC1_PHASEA_DDR, ENC1_PHASEA_PIN);
00070 sbi(ENC1_PHASEA_PORT, ENC1_PHASEA_PIN);
00071
00072 cbi(ENC1_PHASEB_DDR, ENC1_PHASEB_PIN);
00073 sbi(ENC1_PHASEB_PORT, ENC1_PHASEB_PIN);
00074
00075 sbi(ENC1_ICR, ENC1_ISCX0);
00076 cbi(ENC1_ICR, ENC1_ISCX1);
00077
00078 sbi(IMSK, ENC1_INT);
00079 #endif
00080 #ifdef ENC2_SIGNAL
00081
00082 cbi(ENC2_PHASEA_DDR, ENC2_PHASEA_PIN);
00083 sbi(ENC2_PHASEA_PORT, ENC2_PHASEA_PIN);
00084
00085 cbi(ENC2_PHASEB_DDR, ENC2_PHASEB_PIN);
00086 sbi(ENC2_PHASEB_PORT, ENC2_PHASEB_PIN);
00087
00088 sbi(ENC2_ICR, ENC2_ISCX0);
00089 cbi(ENC2_ICR, ENC2_ISCX1);
00090
00091 sbi(IMSK, ENC2_INT);
00092 #endif
00093 #ifdef ENC3_SIGNAL
00094
00095 cbi(ENC3_PHASEA_DDR, ENC3_PHASEA_PIN);
00096 sbi(ENC3_PHASEA_PORT, ENC3_PHASEA_PIN);
00097
00098 cbi(ENC3_PHASEB_DDR, ENC3_PHASEB_PIN);
00099 sbi(ENC3_PHASEB_PORT, ENC3_PHASEB_PIN);
00100
00101 sbi(ENC3_ICR, ENC3_ISCX0);
00102 cbi(ENC3_ICR, ENC3_ISCX1);
00103
00104 sbi(IMSK, ENC3_INT);
00105 #endif
00106
00107
00108 sei();
00109 }
00110
00111
00112 void encoderOff(void)
00113 {
00114
00115 #ifdef ENC0_SIGNAL
00116
00117 sbi(IMSK, INT0);
00118 #endif
00119 #ifdef ENC1_SIGNAL
00120
00121 sbi(IMSK, INT1);
00122 #endif
00123 #ifdef ENC2_SIGNAL
00124
00125 sbi(IMSK, INT2);
00126 #endif
00127 #ifdef ENC3_SIGNAL
00128
00129 sbi(IMSK, INT3);
00130 #endif
00131 }
00132
00133
00134 s32 encoderGetPosition(u08 encoderNum)
00135 {
00136
00137 if(encoderNum < NUM_ENCODERS)
00138 return EncoderState[encoderNum].position;
00139 else
00140 return 0;
00141 }
00142
00143
00144 void encoderSetPosition(u08 encoderNum, s32 position)
00145 {
00146
00147 if(encoderNum < NUM_ENCODERS)
00148 EncoderState[encoderNum].position = position;
00149
00150 }
00151
00152 #ifdef ENC0_SIGNAL
00153
00154 SIGNAL(ENC0_SIGNAL)
00155 {
00156
00157
00158
00159 if( ((inb(ENC0_PHASEA_PORTIN) & (1<<ENC0_PHASEA_PIN)) == 0) ^
00160 ((inb(ENC0_PHASEB_PORTIN) & (1<<ENC0_PHASEB_PIN)) == 0) )
00161 {
00162 EncoderState[0].position++;
00163 }
00164 else
00165 {
00166 EncoderState[0].position--;
00167 }
00168 }
00169 #endif
00170
00171 #ifdef ENC1_SIGNAL
00172
00173 SIGNAL(ENC1_SIGNAL)
00174 {
00175
00176
00177
00178 if( ((inb(ENC1_PHASEA_PORTIN) & (1<<ENC1_PHASEA_PIN)) == 0) ^
00179 ((inb(ENC1_PHASEB_PORTIN) & (1<<ENC1_PHASEB_PIN)) == 0) )
00180 {
00181 EncoderState[1].position++;
00182 }
00183 else
00184 {
00185 EncoderState[1].position--;
00186 }
00187 }
00188 #endif
00189
00190 #ifdef ENC2_SIGNAL
00191
00192 SIGNAL(ENC2_SIGNAL)
00193 {
00194
00195
00196
00197 if( ((inb(ENC2_PHASEA_PORTIN) & (1<<ENC2_PHASEA_PIN)) == 0) ^
00198 ((inb(ENC2_PHASEB_PORTIN) & (1<<ENC2_PHASEB_PIN)) == 0) )
00199 {
00200 EncoderState[2].position++;
00201 }
00202 else
00203 {
00204 EncoderState[2].position--;
00205 }
00206 }
00207 #endif
00208
00209 #ifdef ENC3_SIGNAL
00210
00211 SIGNAL(ENC3_SIGNAL)
00212 {
00213
00214
00215
00216 if( ((inb(ENC3_PHASEA_PORTIN) & (1<<ENC3_PHASEA_PIN)) == 0) ^
00217 ((inb(ENC3_PHASEB_PORTIN) & (1<<ENC3_PHASEB_PIN)) == 0) )
00218 {
00219 EncoderState[3].position++;
00220 }
00221 else
00222 {
00223 EncoderState[3].position--;
00224 }
00225 }
00226 #endif