// bike odometer with binary display // written by Matt Ball, July 2009, for attiny2313 // thanks to theusch on avrfreaks.net for external clock-counter idea // todo: // sleep routine #include #include #include int tenths = 0; int ones = 0; int tens = 0; void init_tc0(void) { TCCR0B = 0x07; // external rising-edge clock source on T0 pin (the hall sensor) // TCNT0 stores the number of revs; note falling-edge may also be initialized (reed switch) TCCR0A = 0x02; // CTC-mode TIMSK |= (1 << OCIE0A); // enable interrupt on compare match sei(); // enable global interrupts OCR0A = 46; // 458 revs in one km for my bike; compare match will (roughly) count tenths of a km } int main(void) { int button_tenths = (PINB & 0x04); // the display switches int button_ones = (PINB & 0x05); int button_tens = (PINB & 0x06); DDRB = 0b00001111; // leds on PB0:3 init_tc0(); // initialize timer/counter0 if (button_tenths == 0) { PORTB = tenths; _delay_ms(1); PORTB = 0; _delay_ms(9); // display at 10% duty cycle } if (button_ones == 0) { PORTB = ones; _delay_ms(1); PORTB = 0; _delay_ms(9); } if (button_tens == 0) { PORTB = tens; _delay_ms(1); PORTB = 0; _delay_ms(9); } return 0; } ISR(TIMER0_COMPA_vect) // triggers every tenth of a km { tenths++; if (tenths >= 10) { ones++; tenths = 0; if (ones >= 10) { tens++; ones = 0; } } }