# HG changeset patch # User Malte Bayer # Date 1322658477 -3600 # Node ID 8b7cdb655e7b4dfb11d904a3b369a963a01234ae # Parent e333cf0e4d84b6a49bc861a2387c8ac6b41e70cf finished track switch to support single lane diff -r e333cf0e4d84 -r 8b7cdb655e7b trackswitch/Makefile --- 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 diff -r e333cf0e4d84 -r 8b7cdb655e7b trackswitch/main.c --- 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<