trackswitch/main.c

Tue, 25 Jun 2013 22:18:17 +0200

author
Malte Bayer <mbayer@neo-soft.org>
date
Tue, 25 Jun 2013 22:18:17 +0200
changeset 138
a570dd24a413
parent 71
2a49d2586fbf
permissions
-rw-r--r--

Added tag FIRMWARE_BEFORE_BINARY_COMMUNICATION for changeset 94d71156517b

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
71
2a49d2586fbf tuning: decreased solenoid switch time
Malte Bayer <mbayer@neo-soft.org>
parents: 70
diff changeset
18 void solenoid_delay(void) {
2a49d2586fbf tuning: decreased solenoid switch time
Malte Bayer <mbayer@neo-soft.org>
parents: 70
diff changeset
19 _delay_ms(2);
2a49d2586fbf tuning: decreased solenoid switch time
Malte Bayer <mbayer@neo-soft.org>
parents: 70
diff changeset
20 }
2a49d2586fbf tuning: decreased solenoid switch time
Malte Bayer <mbayer@neo-soft.org>
parents: 70
diff changeset
21
2a49d2586fbf tuning: decreased solenoid switch time
Malte Bayer <mbayer@neo-soft.org>
parents: 70
diff changeset
22
70
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
23 // TODO: TYPE should be configured somewhere else
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
24 #define TRACKSWITCH_TYPE 1 // 1=double, 2=single left, 3=single right, 4=pitlane
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
25 #define TYPE_DOUBLE 1
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
26 #define TYPE_SINGLE_L 2
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
27 #define TYPE_SINGLE_R 3
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
28 #define TYPE_PITLANE 4
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
29
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
30
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
31 #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
32 #define PULSE_BIT PD2
69
27c0c0095e26 implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 62
diff changeset
33
27c0c0095e26 implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 62
diff changeset
34 #define RESPONSE_PORT PORTC
27c0c0095e26 implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 62
diff changeset
35 #define RESPONSE_PIN PC1
27c0c0095e26 implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 62
diff changeset
36
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
37 #define SOLENOID_A_PORT PORTB
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
38 #define SOLENOID_B_PORT PORTB
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
39
70
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
40 #ifdef WE_HAVE_TO_REVERSE_PORTS_ON_DOUBLE_SWITCH
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
41 //#if (TRACKSWITCH_TYPE == TYPE_DOUBLE)
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
42 // switch outputs - note: car0+1 have also be swapped!
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
43 // todo in future
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
44 #define SOLENOID_A_PIN PB2
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
45 #define SOLENOID_B_PIN PB1
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
46 #else
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
47 #define SOLENOID_A_PIN PB1
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
48 #define SOLENOID_B_PIN PB2
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
49 #endif
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
50
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
51 // 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
52 //#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
53
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
54 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
55 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
56 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
57 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
58 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
59 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
60 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
61 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
62 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
63
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
64 volatile uint8_t response = 0;
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
65 uint8_t self_id = 2; // TODO - muss ermittelt werden und systemweit eindeutig sein
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
66
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
67 void send_response(uint16_t data) {
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
68 /* frame format:
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
69 1 startbit
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
70 2 car id
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
71 3 car id
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
72 4 car id
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
73 5 track change status bit 1
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
74 6 track change status bit 2
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
75 7 sender id
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
76 8 sender id
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
77 9 sender id
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
78 9 sender id
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
79 10 device type
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
80 11 device type
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
81 12 device type
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
82 13 device type
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
83 14 reserved
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
84 15 reserved
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
85 16 stopbit
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
86 */
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
87 uint8_t index = 16; // bit count maximum
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
88 uint8_t enable = DDR(RESPONSE_PORT) | _BV(RESPONSE_PIN);
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
89 uint8_t disable = DDR(RESPONSE_PORT) & ~_BV(RESPONSE_PIN);
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
90 data |= 0b100000000000001; // make sure start/stop bits are set
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
91 while (index != 0) {
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
92 if ((data & 1) != 0) {
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
93 DDR(RESPONSE_PORT) = enable; // enable response output
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
94 } else {
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
95 DDR(RESPONSE_PORT) = disable; // disable response output
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
96 }
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
97 data = data >> 1; // next bit prepare
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
98 index--; // decrement index
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
99 _delay_us(49); // bit valid phase
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
100 }
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
101 // finally be sure to release the bus!
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
102 DDR(RESPONSE_PORT) = disable; // disable response output
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
103 }
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
104
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
105
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
106 ISR ( INT0_vect ) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
107 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
108 // 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
109 // 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
110 // 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
111 bitbuf = 0; // init
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
112 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
113 TCNT2 = 0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
114 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
115 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
116
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
117 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
118 uint8_t clock;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
119 uint8_t state;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
120 uint8_t state2;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
121 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
122 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
123 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
124
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
125 if (clock) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
126 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
127 // 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
128 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
129 // 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
130 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
131 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
132 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
133
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
134 //data = bitbuf; // output data
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
135 // 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
136 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
137 clock = (bitbuf >> 6) & 0b00000111;
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
138 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
139 car_switch[clock] = (bitbuf >> 5) & 1;
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
140 // current response for this car?
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
141 if (response != 0) {
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
142 if ( ((response & 0b00001110) >> 1) == clock) {
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
143 // add our ID to response:
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
144 send_response(response | self_id << 6);
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
145 response = 0;
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
146 }
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
147 }
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
148 }
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
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
151 } else {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
152 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
153 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
154 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
155 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
156 } else {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
157 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
158 // 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
159 if (state2) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
160 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
161 } else {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
162 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
163 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
164 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
165 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
166
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
167
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
168 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
169 // 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
170 car0_old = 0xffff;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
171 car1_old = 0xffff;
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
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
174 ISR (INT1_vect) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
175 // car0 detector
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
176 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
177 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
178 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
179 // calculate difference
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
180 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
181 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
182 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
183 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
184 if ( (tmp > 246) && (tmp < 266) ) car0 = 4;
62
66dd5763dfb3 added car5+6 to trackswitch detector
Malte Bayer <mbayer@neo-soft.org>
parents: 40
diff changeset
185 if ( (tmp > 310) && (tmp < 330) ) car0 = 5;
66dd5763dfb3 added car5+6 to trackswitch detector
Malte Bayer <mbayer@neo-soft.org>
parents: 40
diff changeset
186 if ( (tmp > 374) && (tmp < 394) ) car0 = 6;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
187 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
188 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
189 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
190
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
191 // 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
192 #ifdef ANALOG_COMPARATOR
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
193 ISR (ANA_COMP_vect) {
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
194 // car1 detector
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
195 uint16_t tmp = 0;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
196 car1_new = TCNT1; // get current counter
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
197 if (car1_old < car1_new) {
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
198 // calculate difference
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
199 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
200 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
201 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
202 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
203 if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
62
66dd5763dfb3 added car5+6 to trackswitch detector
Malte Bayer <mbayer@neo-soft.org>
parents: 40
diff changeset
204 if ( (tmp > 310) && (tmp < 330) ) car1 = 5;
66dd5763dfb3 added car5+6 to trackswitch detector
Malte Bayer <mbayer@neo-soft.org>
parents: 40
diff changeset
205 if ( (tmp > 374) && (tmp < 394) ) car1 = 6;
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
206 }
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
207 car1_old = car1_new;
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
208 }
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
209 #else
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
210 // ALTERNATIV:
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
211 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
212 // car1 detector
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
213 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
214 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
215 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
216 // calculate difference
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
217 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
218 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
219 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
220 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
221 if ( (tmp > 242) && (tmp < 270) ) car1 = 4;
62
66dd5763dfb3 added car5+6 to trackswitch detector
Malte Bayer <mbayer@neo-soft.org>
parents: 40
diff changeset
222 if ( (tmp > 310) && (tmp < 330) ) car1 = 5;
66dd5763dfb3 added car5+6 to trackswitch detector
Malte Bayer <mbayer@neo-soft.org>
parents: 40
diff changeset
223 if ( (tmp > 374) && (tmp < 394) ) car1 = 6;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
224 }
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
225 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
226 }
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
227 #endif
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
228
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
229
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
230
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
231
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
232
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
233 int main(void)
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
234 {
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
235 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
236
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
237 // 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
238 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
239 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
240
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
241 // 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
242 #ifdef ANALOG_COMPARATOR
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
243 TCCR1B = _BV(CS01) ; // 1mhz clock
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
244 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
245 #else
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
246 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
247 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
248 #endif
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
249
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
250 // 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
251 // 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
252 // 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
253 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
254 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
255
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
256 #ifdef ANALOG_COMPARATOR
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
257 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
258 #endif
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
259
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
260 // oscillator calibration
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
261 // atmega8@1mhz = 0xac
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
262 // @4mhz = ca 0xa0
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
263 //OSCCAL = 0xa0;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
264 //OSCCAL = 0x9A;
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
265 //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
266
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
267 RS232_init(); // initialize RS232 interface
69
27c0c0095e26 implemented responsewire in CU firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 62
diff changeset
268 RS232_puts_p(PSTR("Freeslot TrackSwitch v1.3\n"));
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
269 sei();
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
270
21
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
271
8b7cdb655e7b finished track switch to support single lane
Malte Bayer <mbayer@neo-soft.org>
parents: 20
diff changeset
272 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
273 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
274
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
275 DDR(RESPONSE_PORT) &= ~_BV(RESPONSE_PIN); // switch response off
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
276 RESPONSE_PORT &= ~_BV(RESPONSE_PIN); // switch response off
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
277
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
278 while (1) {
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
279 // main loop
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
280
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
281 /*
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
282 0 = AA
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
283 1 = AB
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
284 2 = BB
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
285 3 = BA
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
286 */
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
287 if (car0 != car0_state) {
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
288 car0_state = car0;
70
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
289 #if (TRACKSWITCH_TYPE == TYPE_DOUBLE) || (TRACKSWITCH_TYPE == TYPE_SINGLE_R)
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
290 if ( (car0_state != 0) && (car_switch[car0_state-1] == 0) && (car_speed[car0_state-1]>0) ) {
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
291 response = (1 | ((car0_state-1)<<1) | (1 << 4));
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
292 // trigger solenoid A
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
293 RS232_putc('A');
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
294 RS232_putc('B');
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
295 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
296 RS232_putc('\n');
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
297
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
298 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
299 solenoid_delay();
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
300 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
301 solenoid_delay();
70
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
302 } else
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
303 #endif
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
304 if (car0_state != 0) {
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
305 response = (1 | ((car0_state-1)<<1));
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
306 RS232_putc('A');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
307 RS232_putc('A');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
308 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
309 RS232_putc('\n');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
310 }
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
311 } car0 = 0;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
312
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
313
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
314 if (car1 != car1_state) {
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
315 car1_state = car1;
70
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
316 #if (TRACKSWITCH_TYPE == TYPE_DOUBLE) || (TRACKSWITCH_TYPE == TYPE_SINGLE_L)
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
317 if ( (car1_state != 0) && (car_switch[car1_state-1] == 0) && (car_speed[car1_state-1]>0) ) {
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
318 response = (1 | ((car1_state-1)<<1) | (3 << 4));
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
319 // trigger solenoid B
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
320 RS232_putc('B');
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
321 RS232_putc('A');
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
322 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
323 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
324
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
325 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
326 solenoid_delay();
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
327 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
328 solenoid_delay();
70
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
329 } else
3a57b7a70683 finished trackswitch firmware
Malte Bayer <mbayer@neo-soft.org>
parents: 69
diff changeset
330 #endif
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
331 if (car1_state != 0) {
40
c36bf33489f9 added 16bit response to track switch
Malte Bayer <mbayer@neo-soft.org>
parents: 22
diff changeset
332 response = (1 | ((car1_state-1)<<1) | (2 << 4));
22
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
333 RS232_putc('B');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
334 RS232_putc('B');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
335 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
336 RS232_putc('\n');
9af54a45ae42 track switch now only triggers when car speed >0
Malte Bayer <mbayer@neo-soft.org>
parents: 21
diff changeset
337 }
20
e333cf0e4d84 finished trackswitch v1.0 - works as original firmware now
Malte Bayer <mbayer@neo-soft.org>
parents: 19
diff changeset
338 } car1 = 0;
19
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
339
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
340
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
341 } // main loop end
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
342 };
40a309c9c135 added initial trackswitch code, data rx and car id detect working
Malte Bayer <mbayer@neo-soft.org>
parents:
diff changeset
343

mercurial