trackswitch/main.c

Fri, 02 Dec 2011 22:52:10 +0100

author
Malte Bayer <mbayer@neo-soft.org>
date
Fri, 02 Dec 2011 22:52:10 +0100
changeset 24
84f6f0592555
parent 22
9af54a45ae42
child 40
c36bf33489f9
permissions
-rw-r--r--

basic example gtk ui to be coded later

19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
1 #include <avr/interrupt.h>
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
2 #include <avr/io.h>
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
3 #include <avr/wdt.h>
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
4 #include <avr/eeprom.h>
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
5 #include <stdlib.h>
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
6 #include <stdint.h>
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
7 #include <avr/pgmspace.h>
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
8
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
9 #include "main.h"
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
10
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
11 #include "driver/rs232.h"
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
12 #include "util/delay.h"
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
13
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
14
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
15 ISR ( USART_RXC_vect ) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
16 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
17
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
18 #define PULSE_PORT PORTD
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
19 #define PULSE_BIT PD2
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
20 #define SOLENOID_A_PORT PORTB
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
21 #define SOLENOID_B_PORT PORTB
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
22
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
23 #define TRACKSWITCH_TYPE 2 // 1=double, 2=single
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
24
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
25 #define SOLENOID_A_PIN PB1
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
26 #define SOLENOID_B_PIN PB2
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
27
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
28 // internal analog comparator doesnt work well
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
29 //#define ANALOG_COMPARATOR 1
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
30
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
31 volatile uint16_t data = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
32 volatile uint8_t data_len = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
33 volatile uint8_t bitbuf_len = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
34 volatile uint16_t bitbuf = 0;
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
35 volatile uint8_t car_speed[8];
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
36 volatile uint8_t car_switch[8];
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
37 volatile uint16_t car0, car1;
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
38 volatile uint16_t car0_new, car0_old;
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
39 volatile uint16_t car1_new, car1_old;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
40
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
41 ISR ( INT0_vect ) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
42 GICR &= ~_BV(INT0) ; // Disable INT0
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
43 // Startsignal erkannt, ab hier den Timer2 starten,
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
44 // der liest dann alle 50µs den Zustand ein und schreibt das
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
45 // empfangene Bit in den Puffer
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
46 bitbuf = 0; // init
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
47 bitbuf_len = 0b10000000; // init 1 pulse received
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
48 TCNT2 = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
49 TIMSK |= _BV(OCIE2); //enable timer2 interrupt
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
50 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
51
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
52 ISR ( TIMER2_COMP_vect ) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
53 uint8_t clock;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
54 uint8_t state;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
55 uint8_t state2;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
56 if ((bitbuf_len & 0b10000000) == 0) clock = 0; else clock = 0xff;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
57 if ((bitbuf_len & 0b01000000) == 0) state = 0; else state = 0xff;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
58 if ((PIN(PULSE_PORT) & _BV(PULSE_BIT)) == 0) state2 = 0xff; else state2 = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
59
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
60 if (clock) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
61 bitbuf_len &= ~_BV(7); // switch clock to low
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
62 // second pulse of bit
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
63 if ((state==state2) & state2) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
64 // two cycles high: packet end received
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
65 data_len = (bitbuf_len & 0b00111111);
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
66 TIMSK &= ~_BV(OCIE2); //disable timer2 interrupt
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
67 GICR |= _BV(INT0) ; // Enable INT0
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
68
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
69 //data = bitbuf; // output data
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
70 // write data of controllers to array
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
71 if (data_len == 10) { // controller data packet
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
72 clock = (bitbuf >> 6) & 0b00000111;
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
73 car_speed[clock] = (bitbuf >> 1) & 0x0F;
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
74 car_switch[clock] = (bitbuf >> 5) & 1;
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
75 }
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
76
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
77
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
78 } else {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
79 bitbuf_len++; // increment bit counter
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
80 bitbuf = bitbuf << 1; // shift bits
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
81 if (state2 == 0) bitbuf |= 1; // receive logic one
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
82 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
83 } else {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
84 bitbuf_len |= _BV(7); // switch clock to high
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
85 // first pulse of bit
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
86 if (state2) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
87 bitbuf_len |= _BV(6); // store new state
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
88 } else {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
89 bitbuf_len &= ~_BV(6); // store new state
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
90 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
91 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
92 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
93
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
94
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
95 ISR (TIMER1_OVF_vect) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
96 // reset both car counters to overflow
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
97 car0_old = 0xffff;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
98 car1_old = 0xffff;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
99 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
100
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
101 ISR (INT1_vect) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
102 // car0 detector
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
103 uint16_t tmp = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
104 car0_new = TCNT1; // get current counter
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
105 if (car0_old < car0_new) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
106 // calculate difference
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
107 if (car0 == 0) tmp = car0_new-car0_old;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
108 if ( (tmp > 54) && (tmp < 74) ) car0 = 1;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
109 if ( (tmp > 118) && (tmp < 138) ) car0 = 2;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
110 if ( (tmp > 186) && (tmp < 206) ) car0 = 3;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
111 if ( (tmp > 246) && (tmp < 266) ) car0 = 4;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
112 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
113 car0_old = car0_new;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
114 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
115
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
116 // ISR (TIMER1_CAPT_vect) {
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
117 #ifdef ANALOG_COMPARATOR
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
118 ISR (ANA_COMP_vect) {
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
119 // car1 detector
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
120 uint16_t tmp = 0;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
121 car1_new = TCNT1; // get current counter
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
122 if (car1_old < car1_new) {
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
123 // calculate difference
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
124 if (car1 == 0) tmp = car1_new-car1_old;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
125 if ( (tmp > 50) && (tmp < 78) ) car1 = 1;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
126 if ( (tmp > 114) && (tmp < 146) ) car1 = 2;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
127 if ( (tmp > 183) && (tmp < 210) ) car1 = 3;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
128 if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
129 }
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
130 car1_old = car1_new;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
131 }
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
132 #else
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
133 // ALTERNATIV:
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
134 ISR (TIMER1_CAPT_vect) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
135 // car1 detector
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
136 uint16_t tmp = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
137 car1_new = TCNT1; // get current counter
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
138 if (car1_old < car1_new) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
139 // calculate difference
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
140 if (car1 == 0) tmp = car1_new-car1_old;
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
141 if ( (tmp > 50) && (tmp < 78) ) car1 = 1;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
142 if ( (tmp > 114) && (tmp < 146) ) car1 = 2;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
143 if ( (tmp > 183) && (tmp < 210) ) car1 = 3;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
144 if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
145 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
146 car1_old = car1_new;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
147 }
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
148 #endif
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
149
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
150 void solenoid_delay(void) {
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
151 _delay_ms(10);
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
152 }
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
153
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
154
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
155
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
156 int main(void)
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
157 {
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
158 uint8_t car0_state, car1_state;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
159
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
160 // setup data bit timer2
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
161 TCCR2 = (1<<CS21) | (1<<WGM21); //divide by 8, set compare match
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
162 OCR2 = TIMER2_50US;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
163
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
164 // initialize timer1 for IR signal detection
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
165 #ifdef ANALOG_COMPARATOR
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
166 TCCR1B = _BV(CS01) ; // 1mhz clock
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
167 TIMSK = _BV(OCIE2) | _BV(TOIE1) ; //enable timer1+2
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
168 #else
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
169 TCCR1B = _BV(CS01) | _BV(ICNC1) | _BV(ICES1); // 1mhz clock, enable ICP on rising edge
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
170 TIMSK = _BV(OCIE2) | _BV(TOIE1) | _BV(TICIE1); //enable timer1+2 / ICP1
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
171 #endif
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
172
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
173 // enable both external interrupts
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
174 // int 0 = data RX
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
175 // int 1 = car0 input
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
176 MCUCR = _BV(ISC00) | _BV(ISC01) | _BV(ISC10) | _BV(ISC11); // INT0/1 rising edge
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
177 GICR = _BV(INT0) | _BV(INT1) ; // Enable INT0 + INT1
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
178
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
179 #ifdef ANALOG_COMPARATOR
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
180 ACSR = _BV(ACIE) | _BV(ACIS1) | _BV(ACIS0); // setup analog comparator
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
181 #endif
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
182
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
183 // oscillator calibration
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
184 // atmega8@1mhz = 0xac
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
185 // @4mhz = ca 0xa0
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
186 //OSCCAL = 0xa0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
187 //OSCCAL = 0x9A;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
188 //OSCCAL = 0xa0; // internal oscillator @ 4 mhz.... doesnt work accurate!
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
189
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
190 RS232_init(); // initialize RS232 interface
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
191 RS232_puts_p(PSTR("Freeslot TrackSwitch v1.1\n"));
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
192 sei();
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
193
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
194
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
195 DDR(SOLENOID_A_PORT) |= _BV(SOLENOID_A_PIN);
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
196 DDR(SOLENOID_B_PORT) |= _BV(SOLENOID_B_PIN);
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
197
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
198 while (1) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
199 // main loop
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
200
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
201 if (car0 != car0_state) {
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
202 car0_state = car0;
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
203 if ( (car0_state != 0) && (car_switch[car0_state-1] == 0) && (car_speed[car0_state-1]>0) ) {
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
204 // trigger solenoid A
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
205 RS232_putc('A');
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
206 RS232_putc('B');
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
207 RS232_putc('0'+car0_state);
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
208 RS232_putc('\n');
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
209
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
210 SOLENOID_A_PORT |= _BV(SOLENOID_A_PIN);
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
211 solenoid_delay();
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
212 SOLENOID_A_PORT &= ~_BV(SOLENOID_A_PIN);
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
213 solenoid_delay();
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
214 }
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
215 if (car0_state != 0) {
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
216 RS232_putc('A');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
217 RS232_putc('A');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
218 RS232_putc('0'+car0_state);
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
219 RS232_putc('\n');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
220 }
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
221 } car0 = 0;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
222
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
223
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
224 if (car1 != car1_state) {
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
225 car1_state = car1;
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
226 if ( (car1_state != 0) && (car_switch[car1_state-1] == 0) && (car_speed[car1_state-1]>0) ) {
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
227 // trigger solenoid A
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
228 RS232_putc('B');
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
229 RS232_putc('A');
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
230 RS232_putc('0'+car1_state);
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
231 RS232_putc('\n');
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
232
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
233 SOLENOID_B_PORT |= _BV(SOLENOID_B_PIN);
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
234 solenoid_delay();
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
235 SOLENOID_B_PORT &= ~_BV(SOLENOID_B_PIN);
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
236 solenoid_delay();
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
237 }
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
238 if (car1_state != 0) {
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
239 RS232_putc('B');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
240 RS232_putc('B');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
241 RS232_putc('0'+car1_state);
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
242 RS232_putc('\n');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
243 }
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
244 } car1 = 0;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
245
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
246
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
247 } // main loop end
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
248 };
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
249

mercurial