3 This file is part of the MSP Märklin suite
4 Copyright © 2010 Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
9 #include <avr/interrupt.h>
11 #define BIT(n) (1<<(n))
12 #define NOP() __asm__("nop");
29 uint16_t read_input(void);
30 uint16_t read_input_filtered(void);
31 void write_serial(uint8_t);
32 void write_7seg(uint8_t);
36 volatile uint8_t ticks = 0;
37 volatile uint8_t rx_buf[3];
38 volatile uint8_t rx_fill = 0;
40 ISR(TIMER1_COMPA_vect)
52 rx_buf[rx_fill++] = c;
56 rx_buf[rx_fill++] = c;
57 if(rx_buf[0]=='A' && rx_fill==3)
59 uint8_t n = (rx_buf[1]-'0')*10+(rx_buf[2]-'0');
64 else if(rx_buf[0]=='S' && rx_fill==3)
66 speed = (rx_buf[1]-'0')*10+(rx_buf[2]-'0');
76 DDRD = 0x02; // 00000010
77 PORTD = 0xFC; // 11111100
78 DDRB = 0x3F; // 00111111
79 PORTB = 0x0F; // 00001111
87 UCSR0C = BIT(UCSZ00) | BIT(UCSZ01);
88 UCSR0B = BIT(RXEN0) | BIT(TXEN0) | BIT(RXCIE0);
91 TCCR1B = BIT(WGM12) | BIT(CS12);
104 input = read_input_filtered();
107 toggled = state^input;
110 if((toggled&3)==2 && (state&1))
135 write_serial('0'+speed/10);
136 write_serial('0'+speed%10);
147 if(toggled&~state&bit)
156 uint16_t fbit = 1<<f;
158 write_serial((funcs&fbit) ? 'H' : 'L');
166 uint16_t read_input(void)
171 for(row=0; row<4; ++row)
175 PORTB = (PORTB|0x0F)&~(1<<row);
184 input |= (pins&0x3C)<<(row*4);
190 uint16_t read_input_filtered(void)
192 uint16_t valid = 0xFFFF;
200 input = read_input();
201 valid &= ~(input^prev);
208 void write_serial(uint8_t c)
210 while(!(UCSR0A&(1<<UDRE0))) ;
214 void write_7seg(uint8_t n)
216 uint8_t segs = ~digits[n];