2 #include <avr/interrupt.h>
4 #define BIT(n) (1<<(n))
5 #define NOP() __asm__("nop");
22 uint16_t read_input(void);
23 uint16_t read_input_filtered(void);
24 void write_serial(uint8_t);
25 void write_7seg(uint8_t);
29 volatile uint8_t ticks = 0;
30 volatile uint8_t rx_buf[3];
31 volatile uint8_t rx_fill = 0;
33 ISR(TIMER1_COMPA_vect)
45 rx_buf[rx_fill++] = c;
49 rx_buf[rx_fill++] = c;
50 if(rx_buf[0]=='A' && rx_fill==3)
52 uint8_t n = (rx_buf[1]-'0')*10+(rx_buf[2]-'0');
57 else if(rx_buf[0]=='S' && rx_fill==3)
59 speed = (rx_buf[1]-'0')*10+(rx_buf[2]-'0');
69 DDRD = 0x02; // 00000010
70 PORTD = 0xFC; // 11111100
71 DDRB = 0x3F; // 00111111
72 PORTB = 0x0F; // 00001111
80 UCSR0C = BIT(UCSZ00) | BIT(UCSZ01);
81 UCSR0B = BIT(RXEN0) | BIT(TXEN0) | BIT(RXCIE0);
84 TCCR1B = BIT(WGM12) | BIT(CS12);
97 input = read_input_filtered();
100 toggled = state^input;
103 if((toggled&3)==2 && (state&1))
128 write_serial('0'+speed/10);
129 write_serial('0'+speed%10);
140 if(toggled&~state&bit)
149 uint16_t fbit = 1<<f;
151 write_serial((funcs&fbit) ? 'H' : 'L');
159 uint16_t read_input(void)
164 for(row=0; row<4; ++row)
168 PORTB = (PORTB|0x0F)&~(1<<row);
177 input |= (pins&0x3C)<<(row*4);
183 uint16_t read_input_filtered(void)
185 uint16_t valid = 0xFFFF;
193 input = read_input();
194 valid &= ~(input^prev);
201 void write_serial(uint8_t c)
203 while(!(UCSR0A&(1<<UDRE0))) ;
207 void write_7seg(uint8_t n)
209 uint8_t segs = ~digits[n];