car: finished set_id function

Mon, 23 Dec 2013 10:59:14 +0100

author
mbayer
date
Mon, 23 Dec 2013 10:59:14 +0100
changeset 151
0e71b51c83a5
parent 150
cc8721bd2c03
child 152
e787f47c084b

car: finished set_id function

car004f/Makefile file | annotate | diff | comparison | revisions
car004f/main.c file | annotate | diff | comparison | revisions
--- a/car004f/Makefile	Sun Dec 22 02:42:03 2013 +0100
+++ b/car004f/Makefile	Mon Dec 23 10:59:14 2013 +0100
@@ -1,8 +1,8 @@
 PRG=main
 
 MCU=atmega8
-FUSES=-U lfuse:w:0xAE:m -U hfuse:w:0xd9:m
-#FUSES=-U lfuse:w:0x83:m -U hfuse:w:0xd9:m
+#FUSES=-U lfuse:w:0xAE:m -U hfuse:w:0xd9:m
+FUSES=-U lfuse:w:0x83:m -U hfuse:w:0xd8:m
 # original fuses: 83 / d8
 BOOTFUSES=-U lfuse:w:0xff:m -U hfuse:w:0xce:m
 
--- a/car004f/main.c	Sun Dec 22 02:42:03 2013 +0100
+++ b/car004f/main.c	Mon Dec 23 10:59:14 2013 +0100
@@ -124,8 +124,9 @@
 #define LIGHT_MODES             1       // anzahl der lichtmodi (ohne den modus "aus")
 #define BRAKE_OFF_TIMEOUT       60      // value * 10ms
 
-#define CAR_DEBUG       1
+//#define CAR_DEBUG       1
 #define EE_CONFIG_ADDR  64
+#define DOUBLE_CLICK_TIMEOUT    50 // 500ms
 
 void config_save(void) {
     eeprom_write_block( &config, &eeconfig, sizeof(config_t) );
@@ -135,48 +136,52 @@
 void brake_on(void) {
     LIGHT_PORT |= _BV(LIGHT_BRAKE); // brake light on
     BRAKE_PORT |= _BV(BRAKE); // brake on
+    DDRB &= ~_BV(1);                  // PB1 PWM Output enable
     brake_timeout = BRAKE_OFF_TIMEOUT;
 }
 
 void brake_off(void) {
     LIGHT_PORT &= ~_BV(LIGHT_BRAKE); // brake light off
     BRAKE_PORT &= ~_BV(BRAKE); // brake off
+    DDRB &= ~_BV(1);                  // PB1 PWM Output disable
     brake_timeout = 0;
 }
 
-uint8_t scan_id(void) {
+uint8_t set_id(void) {
+    _delay_ms(100); // short wait
     uint8_t temp;
     timeout = 1;
-        // scan for any key press and assign to that controller
-        while (car_speed[config.slot] == 0) {
-            for (uint8_t i=0; i<6; i++) {
-                if (car_switch[i] == 1) {
-                    // wait for second key press within timeout period to assign successfully
-                    brake_timeout = 0xff;
-                    temp = car_switch[i];
-                    while (brake_timeout > 1) {
-                        if (temp != car_switch[i]) {
-                            temp = car_switch[i];
-                            if (temp == 1) {
-                                config.slot = i;
-                                return 1;
-                            }
-                        }
-                        // toggle lights if timeout
-                        if (timeout == 1) {
-                            LIGHT_PORT ^= _BV(LIGHT_FRONT);
-                            timeout = 5;
-                        }
+    brake_timeout = 0xff;
+    // wait for key press and assign on double click
+    while ((car_speed[config.slot] == 0) && (brake_timeout > 1)) {
+        temp = car_switch[config.program];
+        if (temp == 0) {
+            // wait for second key press within timeout period to assign successfully
+            brake_timeout = DOUBLE_CLICK_TIMEOUT;
+            timeout = 1;
+            while (brake_timeout > 1) {
+                if (temp != car_switch[config.program]) {
+                    temp = car_switch[config.program];
+                    if (temp == 0) {
+                        config.slot = config.program;
+                        return 1;
                     }
-                    return 0;
+                }
+                // toggle lights if timeout
+                if (timeout == 1) {
+                    LIGHT_PORT ^= _BV(LIGHT_FRONT);
+                    timeout = 5;
                 }
             }
-            // toggle lights if timeout
-            if (timeout == 1) {
-                LIGHT_PORT ^= _BV(LIGHT_FRONT);
-                timeout = 10;
-            }
+            return 0;
         }
+
+        // toggle lights if timeout
+        if (timeout == 1) {
+            LIGHT_PORT ^= _BV(LIGHT_FRONT);
+            timeout = 10;
+        }
+    }
     return 0;
 }
 
@@ -207,9 +212,10 @@
 
     //OCR1A = 63;                       // Dutycycle of OC1A = 25%
     //OCR1B = 127;                      // Dutycycle of OC1B = 50%
-    OCR1A = 0;
-    OCR1B = 0;
+    OCR1A = 0xff; // brake PWM?!
+    OCR1B = 0; // Motor drive PWM
     DDRB &= ~_BV(2);                  // PB2 PWM Output disable
+    DDRB &= ~_BV(1);                  // PB1 PWM Output disable
 
     // configure TIMER0 to overflow every 10ms at 4 MHz
     TIMSK = _BV(TOIE0);         // Timer0 Overflow INT erlauben
@@ -222,41 +228,27 @@
     eeprom_read_block( &config, &eeconfig, sizeof(config_t) );
 
 
-#ifdef CAR_DEBUG
-    if (config.initialized == 0) {
-    LIGHT_PORT &= ~_BV(LIGHT_FRONT);
-    for (temp = 0; temp <= config.slot; temp++) {
-        LIGHT_PORT ^= _BV(LIGHT_FRONT);
-        _delay_ms(250);
-        LIGHT_PORT ^= _BV(LIGHT_FRONT);
-        _delay_ms(250);
-    }
-    }
-#endif
-
-
-
-    if (config.initialized != 0) {
+    if (config.initialized == 0xff) {
         config.slot = 0;
         config.light = 0;
-        config.program = 0;
+        config.program = 0xff;
         config.initialized = 0;
         config_save();
     }
 
     if ((config.program != 0xff) || (config.slot > 5 )) {
-        temp = scan_id();
+        temp = set_id();
         config.program = 0xff;
         config_save();
         if (temp == 1) {
             // acknowledge with the engine
             OCR1B = 25;
             DDRB &= ~_BV(2);                  // PB2 PWM Output disable
-            for (temp = 0xff; temp > 0; temp--) {
+            for (temp = 128; temp > 0; temp--) {
                 DDRB ^= _BV(2);                   // PB2 PWM Output toggle
-                _delay_ms(5); // 50 hz
+                _delay_ms(2);
                 DDRB ^= _BV(2);                   // PB2 PWM Output toggle
-                _delay_ms(15); // 50 hz
+                _delay_ms(3);
             }
 
         }
@@ -264,6 +256,9 @@
     }
 
 
+    my_switch = car_switch[config.slot]; // initialize
+    my_speed = car_speed[config.slot]; // initialize
+
     while (1) {
         // main loop
 
@@ -285,15 +280,15 @@
         if (car_speed[config.slot] == 0) {
             if (my_switch != car_switch[config.slot]) {
                 my_switch = car_switch[config.slot];
-                if (my_switch != 0) {
+                if (my_switch == 0) {
                     // cycle light
                     if (config.light == LIGHT_MODES) config.light = 0; else config.light++;
                     if (timeout > 1) {
                         // zweiter Tastendruck, Program Mode im EEPROM setzen
-                        config.program = 1; // TODO: hier muss der slot rein welcher doppelclicked wurde (natuerlich dann auch nicht in der Lichtschaltelogik abfragen!)
+                        config.program = config.slot; // TODO: hier muss der slot rein welcher doppelclicked wurde (natuerlich dann auch nicht in der Lichtschaltelogik abfragen!)
                     } else {
                         // erster Tastendruck, timeout setzen
-                        timeout = 80;
+                        timeout = DOUBLE_CLICK_TIMEOUT;
                     }
                     config_save();
                 }
@@ -311,7 +306,7 @@
 
 
         // timeout reset
-        timeout = 0;
+        if (timeout == 1) timeout = 0;
     } // main loop end
 };
 

mercurial