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