00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00056 #include <stdlib.h>
00057
00058 #include <aversive.h>
00059 #include <aversive/wait.h>
00060
00061 #include <encoders_microb.h>
00062 #include <encoders_microb_config.h>
00063
00064
00065
00066 int32_t g_encoders_microb_values[ENCODERS_MICROB_NUMBER];
00067 int8_t g_encoders_microb_previous[ENCODERS_MICROB_NUMBER];
00068
00069
00070
00071 #define SEL_A() cbi(ENCODERS_MICROB_SEL_PORT,ENCODERS_MICROB_SEL_BIT)
00072 #define SEL_B() sbi(ENCODERS_MICROB_SEL_PORT,ENCODERS_MICROB_SEL_BIT)
00073
00074
00075
00076 #define READ(n) \
00077 do { \
00078 val = ENCODERS_MICROB##n##_PIN; \
00079 nop(); \
00080 if (val != ENCODERS_MICROB##n##_PIN) \
00081 val = ENCODERS_MICROB##n##_PIN; \
00082 \
00083 res = (val - g_encoders_microb_previous[n]); \
00084 g_encoders_microb_previous[n] = val; \
00085 \
00086 g_encoders_microb_values[n] += (int32_t)res ; \
00087 } while(0)
00088
00089
00090
00091 #define INIT(n) do { \
00092 g_encoders_microb_values[n] = 0; \
00093 } while(0)
00094
00095
00096
00097
00098
00099
00100
00104 void encoders_microb_init(void)
00105 {
00106 uint8_t flags;
00107
00108 sbi(DDR(ENCODERS_MICROB_SEL_PORT),ENCODERS_MICROB_SEL_BIT);
00109 SEL_A();
00110
00111 wait_4cyc(50);
00112 encoders_microb_manage(NULL);
00113 wait_4cyc(50);
00114 encoders_microb_manage(NULL);
00115
00116 IRQ_LOCK(flags);
00117 #ifdef ENCODERS_MICROB0_ENABLED
00118 INIT(0);
00119 #endif
00120
00121 #ifdef ENCODERS_MICROB2_ENABLED
00122 INIT(2);
00123 #endif
00124
00125 #ifdef ENCODERS_MICROB4_ENABLED
00126 INIT(4);
00127 #endif
00128
00129 #ifdef ENCODERS_MICROB6_ENABLED
00130 INIT(6);
00131 #endif
00132
00133
00134 SEL_B();
00135
00136 wait_4cyc(50);
00137
00138 #ifdef ENCODERS_MICROB1_ENABLED
00139 INIT(1);
00140 #endif
00141
00142 #ifdef ENCODERS_MICROB3_ENABLED
00143 INIT(3);
00144 #endif
00145
00146 #ifdef ENCODERS_MICROB5_ENABLED
00147 INIT(5);
00148 #endif
00149
00150 #ifdef ENCODERS_MICROB7_ENABLED
00151 INIT(7);
00152 #endif
00153 IRQ_UNLOCK(flags);
00154
00155 }
00156
00157
00166 void encoders_microb_manage(void * dummy)
00167 {
00168 static uint8_t sel=0;
00169 uint8_t flags;
00170
00171
00172 int8_t val, res;
00173
00174 IRQ_LOCK(flags);
00175
00176 if ( ! sel)
00177 {
00178 #ifdef ENCODERS_MICROB0_ENABLED
00179 READ(0);
00180 #endif
00181
00182 #ifdef ENCODERS_MICROB2_ENABLED
00183 READ(2);
00184 #endif
00185
00186 #ifdef ENCODERS_MICROB4_ENABLED
00187 READ(4);
00188 #endif
00189
00190 #ifdef ENCODERS_MICROB6_ENABLED
00191 READ(6);
00192 #endif
00193
00194 SEL_B();
00195 }
00196 else
00197 {
00198 #ifdef ENCODERS_MICROB1_ENABLED
00199 READ(1);
00200 #endif
00201
00202 #ifdef ENCODERS_MICROB3_ENABLED
00203 READ(3);
00204 #endif
00205
00206 #ifdef ENCODERS_MICROB5_ENABLED
00207 READ(5);
00208 #endif
00209
00210 #ifdef ENCODERS_MICROB7_ENABLED
00211 READ(7);
00212 #endif
00213
00214 SEL_A();
00215 }
00216
00217 sel = !sel;
00218
00219 IRQ_UNLOCK(flags);
00220
00221 }
00222
00223
00224
00230 int32_t encoders_microb_get_value(void * number)
00231 {
00232 int32_t value;
00233 uint8_t flags;
00234
00235 IRQ_LOCK(flags);
00236 value = g_encoders_microb_values[(int)number];
00237 IRQ_UNLOCK(flags);
00238
00239 return value;
00240 }
00241
00248 void encoders_microb_set_value(void * number, int32_t v)
00249 {
00250 uint8_t flags;
00251
00252 IRQ_LOCK(flags);
00253 g_encoders_microb_values[(int)number] = v;
00254 IRQ_UNLOCK(flags);
00255 }