car004f/main.c

changeset 151
0e71b51c83a5
parent 149
1c3425af9aa0
child 152
e787f47c084b
equal deleted inserted replaced
150:cc8721bd2c03 151:0e71b51c83a5
122 #define BRAKE 0 122 #define BRAKE 0
123 123
124 #define LIGHT_MODES 1 // anzahl der lichtmodi (ohne den modus "aus") 124 #define LIGHT_MODES 1 // anzahl der lichtmodi (ohne den modus "aus")
125 #define BRAKE_OFF_TIMEOUT 60 // value * 10ms 125 #define BRAKE_OFF_TIMEOUT 60 // value * 10ms
126 126
127 #define CAR_DEBUG 1 127 //#define CAR_DEBUG 1
128 #define EE_CONFIG_ADDR 64 128 #define EE_CONFIG_ADDR 64
129 #define DOUBLE_CLICK_TIMEOUT 50 // 500ms
129 130
130 void config_save(void) { 131 void config_save(void) {
131 eeprom_write_block( &config, &eeconfig, sizeof(config_t) ); 132 eeprom_write_block( &config, &eeconfig, sizeof(config_t) );
132 } 133 }
133 134
134 135
135 void brake_on(void) { 136 void brake_on(void) {
136 LIGHT_PORT |= _BV(LIGHT_BRAKE); // brake light on 137 LIGHT_PORT |= _BV(LIGHT_BRAKE); // brake light on
137 BRAKE_PORT |= _BV(BRAKE); // brake on 138 BRAKE_PORT |= _BV(BRAKE); // brake on
139 DDRB &= ~_BV(1); // PB1 PWM Output enable
138 brake_timeout = BRAKE_OFF_TIMEOUT; 140 brake_timeout = BRAKE_OFF_TIMEOUT;
139 } 141 }
140 142
141 void brake_off(void) { 143 void brake_off(void) {
142 LIGHT_PORT &= ~_BV(LIGHT_BRAKE); // brake light off 144 LIGHT_PORT &= ~_BV(LIGHT_BRAKE); // brake light off
143 BRAKE_PORT &= ~_BV(BRAKE); // brake off 145 BRAKE_PORT &= ~_BV(BRAKE); // brake off
146 DDRB &= ~_BV(1); // PB1 PWM Output disable
144 brake_timeout = 0; 147 brake_timeout = 0;
145 } 148 }
146 149
147 uint8_t scan_id(void) { 150 uint8_t set_id(void) {
151 _delay_ms(100); // short wait
148 uint8_t temp; 152 uint8_t temp;
149 timeout = 1; 153 timeout = 1;
150 // scan for any key press and assign to that controller 154 brake_timeout = 0xff;
151 while (car_speed[config.slot] == 0) { 155 // wait for key press and assign on double click
152 for (uint8_t i=0; i<6; i++) { 156 while ((car_speed[config.slot] == 0) && (brake_timeout > 1)) {
153 if (car_switch[i] == 1) { 157 temp = car_switch[config.program];
154 // wait for second key press within timeout period to assign successfully 158 if (temp == 0) {
155 brake_timeout = 0xff; 159 // wait for second key press within timeout period to assign successfully
156 temp = car_switch[i]; 160 brake_timeout = DOUBLE_CLICK_TIMEOUT;
157 while (brake_timeout > 1) { 161 timeout = 1;
158 if (temp != car_switch[i]) { 162 while (brake_timeout > 1) {
159 temp = car_switch[i]; 163 if (temp != car_switch[config.program]) {
160 if (temp == 1) { 164 temp = car_switch[config.program];
161 config.slot = i; 165 if (temp == 0) {
162 return 1; 166 config.slot = config.program;
163 } 167 return 1;
164 }
165 // toggle lights if timeout
166 if (timeout == 1) {
167 LIGHT_PORT ^= _BV(LIGHT_FRONT);
168 timeout = 5;
169 }
170 } 168 }
171 return 0;
172 } 169 }
173 } 170 // toggle lights if timeout
174 // toggle lights if timeout 171 if (timeout == 1) {
175 if (timeout == 1) { 172 LIGHT_PORT ^= _BV(LIGHT_FRONT);
176 LIGHT_PORT ^= _BV(LIGHT_FRONT); 173 timeout = 5;
177 timeout = 10; 174 }
178 } 175 }
179 } 176 return 0;
177 }
178
179 // toggle lights if timeout
180 if (timeout == 1) {
181 LIGHT_PORT ^= _BV(LIGHT_FRONT);
182 timeout = 10;
183 }
184 }
180 return 0; 185 return 0;
181 } 186 }
182 187
183 int main(void) 188 int main(void)
184 { 189 {
205 // and set at BOTTOM. Clock Prescaler is 1. 210 // and set at BOTTOM. Clock Prescaler is 1.
206 211
207 212
208 //OCR1A = 63; // Dutycycle of OC1A = 25% 213 //OCR1A = 63; // Dutycycle of OC1A = 25%
209 //OCR1B = 127; // Dutycycle of OC1B = 50% 214 //OCR1B = 127; // Dutycycle of OC1B = 50%
210 OCR1A = 0; 215 OCR1A = 0xff; // brake PWM?!
211 OCR1B = 0; 216 OCR1B = 0; // Motor drive PWM
212 DDRB &= ~_BV(2); // PB2 PWM Output disable 217 DDRB &= ~_BV(2); // PB2 PWM Output disable
218 DDRB &= ~_BV(1); // PB1 PWM Output disable
213 219
214 // configure TIMER0 to overflow every 10ms at 4 MHz 220 // configure TIMER0 to overflow every 10ms at 4 MHz
215 TIMSK = _BV(TOIE0); // Timer0 Overflow INT erlauben 221 TIMSK = _BV(TOIE0); // Timer0 Overflow INT erlauben
216 TCNT0 = 100; // TIMER0 vorladen mit 100 222 TCNT0 = 100; // TIMER0 vorladen mit 100
217 TCCR0 = _BV(CS02) ; // Vorteiler auf 256, ab hier läuft der TIMER0 223 TCCR0 = _BV(CS02) ; // Vorteiler auf 256, ab hier läuft der TIMER0
220 226
221 // config (from eeprom!) 227 // config (from eeprom!)
222 eeprom_read_block( &config, &eeconfig, sizeof(config_t) ); 228 eeprom_read_block( &config, &eeconfig, sizeof(config_t) );
223 229
224 230
225 #ifdef CAR_DEBUG 231 if (config.initialized == 0xff) {
226 if (config.initialized == 0) {
227 LIGHT_PORT &= ~_BV(LIGHT_FRONT);
228 for (temp = 0; temp <= config.slot; temp++) {
229 LIGHT_PORT ^= _BV(LIGHT_FRONT);
230 _delay_ms(250);
231 LIGHT_PORT ^= _BV(LIGHT_FRONT);
232 _delay_ms(250);
233 }
234 }
235 #endif
236
237
238
239 if (config.initialized != 0) {
240 config.slot = 0; 232 config.slot = 0;
241 config.light = 0; 233 config.light = 0;
242 config.program = 0; 234 config.program = 0xff;
243 config.initialized = 0; 235 config.initialized = 0;
244 config_save(); 236 config_save();
245 } 237 }
246 238
247 if ((config.program != 0xff) || (config.slot > 5 )) { 239 if ((config.program != 0xff) || (config.slot > 5 )) {
248 temp = scan_id(); 240 temp = set_id();
249 config.program = 0xff; 241 config.program = 0xff;
250 config_save(); 242 config_save();
251 if (temp == 1) { 243 if (temp == 1) {
252 // acknowledge with the engine 244 // acknowledge with the engine
253 OCR1B = 25; 245 OCR1B = 25;
254 DDRB &= ~_BV(2); // PB2 PWM Output disable 246 DDRB &= ~_BV(2); // PB2 PWM Output disable
255 for (temp = 0xff; temp > 0; temp--) { 247 for (temp = 128; temp > 0; temp--) {
256 DDRB ^= _BV(2); // PB2 PWM Output toggle 248 DDRB ^= _BV(2); // PB2 PWM Output toggle
257 _delay_ms(5); // 50 hz 249 _delay_ms(2);
258 DDRB ^= _BV(2); // PB2 PWM Output toggle 250 DDRB ^= _BV(2); // PB2 PWM Output toggle
259 _delay_ms(15); // 50 hz 251 _delay_ms(3);
260 } 252 }
261 253
262 } 254 }
263 timeout = 0; 255 timeout = 0;
264 } 256 }
265 257
258
259 my_switch = car_switch[config.slot]; // initialize
260 my_speed = car_speed[config.slot]; // initialize
266 261
267 while (1) { 262 while (1) {
268 // main loop 263 // main loop
269 264
270 if (brake_timeout == 1) brake_off(); 265 if (brake_timeout == 1) brake_off();
283 278
284 // Light cycle if switch pressed without speed 279 // Light cycle if switch pressed without speed
285 if (car_speed[config.slot] == 0) { 280 if (car_speed[config.slot] == 0) {
286 if (my_switch != car_switch[config.slot]) { 281 if (my_switch != car_switch[config.slot]) {
287 my_switch = car_switch[config.slot]; 282 my_switch = car_switch[config.slot];
288 if (my_switch != 0) { 283 if (my_switch == 0) {
289 // cycle light 284 // cycle light
290 if (config.light == LIGHT_MODES) config.light = 0; else config.light++; 285 if (config.light == LIGHT_MODES) config.light = 0; else config.light++;
291 if (timeout > 1) { 286 if (timeout > 1) {
292 // zweiter Tastendruck, Program Mode im EEPROM setzen 287 // zweiter Tastendruck, Program Mode im EEPROM setzen
293 config.program = 1; // TODO: hier muss der slot rein welcher doppelclicked wurde (natuerlich dann auch nicht in der Lichtschaltelogik abfragen!) 288 config.program = config.slot; // TODO: hier muss der slot rein welcher doppelclicked wurde (natuerlich dann auch nicht in der Lichtschaltelogik abfragen!)
294 } else { 289 } else {
295 // erster Tastendruck, timeout setzen 290 // erster Tastendruck, timeout setzen
296 timeout = 80; 291 timeout = DOUBLE_CLICK_TIMEOUT;
297 } 292 }
298 config_save(); 293 config_save();
299 } 294 }
300 } 295 }
301 } 296 }
309 break; 304 break;
310 } 305 }
311 306
312 307
313 // timeout reset 308 // timeout reset
314 timeout = 0; 309 if (timeout == 1) timeout = 0;
315 } // main loop end 310 } // main loop end
316 }; 311 };
317 312

mercurial