finished track switch to support single lane

Wed, 30 Nov 2011 14:07:57 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Wed, 30 Nov 2011 14:07:57 +0100
changeset 21
8b7cdb655e7b
parent 20
e333cf0e4d84
child 22
9af54a45ae42

finished track switch to support single lane

trackswitch/Makefile file | annotate | diff | comparison | revisions
trackswitch/main.c file | annotate | diff | comparison | revisions
--- a/trackswitch/Makefile	Tue Nov 29 15:06:08 2011 +0100
+++ b/trackswitch/Makefile	Wed Nov 30 14:07:57 2011 +0100
@@ -3,7 +3,8 @@
 MCU=atmega8
 FUSES=-U lfuse:w:0xAE:m -U hfuse:w:0xd9:m
 #FUSES=-U lfuse:w:0x83:m -U hfuse:w:0xd9:m
-    BOOTFUSES=-U lfuse:w:0xff:m -U hfuse:w:0xce:m
+# original fuses: 83 / d8
+BOOTFUSES=-U lfuse:w:0xff:m -U hfuse:w:0xce:m
 
 #F_CPU=4185600
 #F_CPU = 14318000
--- a/trackswitch/main.c	Tue Nov 29 15:06:08 2011 +0100
+++ b/trackswitch/main.c	Wed Nov 30 14:07:57 2011 +0100
@@ -18,8 +18,11 @@
 #define PULSE_PORT      PORTD
 #define PULSE_BIT       PD2
 #define SOLENOID_A_PORT PORTB
+#define SOLENOID_B_PORT PORTB
+
+#define TRACKSWITCH_TYPE 2 // 1=double, 2=single
+
 #define SOLENOID_A_PIN  PB1
-#define SOLENOID_B_PORT PORTB
 #define SOLENOID_B_PIN  PB2
 
 
@@ -108,6 +111,23 @@
     car0_old = car0_new;
 }
 
+// ISR (TIMER1_CAPT_vect) {
+ISR (ANA_COMP_vect) {
+    // car1 detector
+    uint16_t tmp = 0;
+    car1_new = TCNT1; // get current counter
+    if (car1_old < car1_new) {
+        // calculate difference
+        if (car1 == 0) tmp = car1_new-car1_old;
+        if ( (tmp > 50) && (tmp < 78) ) car1 = 1;
+        if ( (tmp > 114) && (tmp < 146) ) car1 = 2;
+        if ( (tmp > 183) && (tmp < 210) ) car1 = 3;
+        if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
+    }
+    car1_old = car1_new;
+}
+
+// ALTERNATIV:
 ISR (TIMER1_CAPT_vect) {
     // car1 detector
     uint16_t tmp = 0;
@@ -115,10 +135,10 @@
     if (car1_old < car1_new) {
         // calculate difference
         if (car1 == 0) tmp = car1_new-car1_old;
-        if ( (tmp > 54) && (tmp < 74) ) car1 = 1;
-        if ( (tmp > 118) && (tmp < 138) ) car1 = 2;
-        if ( (tmp > 186) && (tmp < 206) ) car1 = 3;
-        if ( (tmp > 246) && (tmp < 266) ) car1 = 4;
+        if ( (tmp > 50) && (tmp < 78) ) car1 = 1;
+        if ( (tmp > 114) && (tmp < 146) ) car1 = 2;
+        if ( (tmp > 183) && (tmp < 210) ) car1 = 3;
+        if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
     }
     car1_old = car1_new;
 }
@@ -129,19 +149,24 @@
 }
 
 
+//#define ANALOG_COMPARATOR       1
+
 int main(void)
 {
     uint8_t car0_state, car1_state;
-    car0_state = 1;
-    car1_state = 1;
-
 
     // setup data bit timer2
     TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match
     OCR2 = TIMER2_50US;
 
     // initialize timer1 for IR signal detection
+#ifdef ANALOG_COMPARATOR
+    TCCR1B = _BV(CS01) ; // 1mhz clock
+    TIMSK = _BV(OCIE2) | _BV(TOIE1) ; //enable timer1+2
+#else
     TCCR1B = _BV(CS01) | _BV(ICNC1) | _BV(ICES1); // 1mhz clock, enable ICP on rising edge
+    TIMSK = _BV(OCIE2) | _BV(TOIE1) | _BV(TICIE1); //enable timer1+2 / ICP1
+#endif
 
     // enable both external interrupts
     // int 0 = data RX
@@ -149,8 +174,9 @@
     MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge
     GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1
 
-    // enable timer interrupts
-    TIMSK = _BV(OCIE2) | _BV(TOIE1) | _BV(TICIE1); //enable timer1+2
+#ifdef ANALOG_COMPARATOR
+    ACSR = _BV(ACIE) | _BV(ACIS1) | _BV(ACIS0); // setup analog comparator
+#endif
 
     // oscillator calibration
     // atmega8@1mhz = 0xac
@@ -160,11 +186,12 @@
     //OSCCAL = 0xa0; // internal oscillator @ 4 mhz.... doesnt work accurate!
 
     RS232_init(); // initialize RS232 interface
-    RS232_puts_p(PSTR("Freeslot TrackSwitch v1.0\n"));
+    RS232_puts_p(PSTR("Freeslot TrackSwitch v1.1\n"));
     sei();
 
-    DDR(SOLENOID_A_PORT) |= SOLENOID_A_PIN;
-    DDR(SOLENOID_B_PORT) |= SOLENOID_B_PIN;
+
+    DDR(SOLENOID_A_PORT) |= _BV(SOLENOID_A_PIN);
+    DDR(SOLENOID_B_PORT) |= _BV(SOLENOID_B_PIN);
 
     while (1) {
         // main loop

mercurial