diff --git a/Makefile b/Makefile index 0cacd4d..61330a7 100644 --- a/Makefile +++ b/Makefile @@ -12,9 +12,9 @@ LDFLAGS += -mmcu=$(MCU) AVDFLAGS += -p $(MCU) AVDFLAGS += -c usbtiny -CLOCK_HZ = 16000000 -FUSES += -U lfuse:w:0xff:m -FUSES += -U hfuse:w:0xd9:m +CLOCK_HZ = 2000000 +FUSES += -U lfuse:w:0x7f:m +FUSES += -U hfuse:w:0xdd:m FUSES += -U efuse:w:0xff:m @@ -28,6 +28,7 @@ fuses: avrdude $(AVDFLAGS) $(FUSES) main: main.o avr.o +avr.o: CFLAGS += -DCLOCK_HZ=$(CLOCK_HZ) $(PROG).hex: $(PROG) avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@ diff --git a/avr.c b/avr.c index c6b1ba9..4fcb928 100644 --- a/avr.c +++ b/avr.c @@ -4,9 +4,8 @@ #include #include "avr.h" -/* This only works out for a 16MHz or 8MHz clock */ -#define CLOCK_HZ 16000000 -#define TICKS_PER_SECOND (CLOCK_HZ / 256) +/* Clock must be a multiple of 2MHz or there will be clock drift */ +#define TICKS_PER_SECOND (CLOCK_HZ / 64) #define TICKS_PER_JIFFY (TICKS_PER_SECOND / 10) #define cbi(byt, bit) (byt &= ~_BV(bit)) @@ -34,9 +33,9 @@ init(void) TCCR1B = 0; TCNT1 = 0; OCR1A = TICKS_PER_JIFFY - 1; - TCCR1B |= (1 << WGM12); - TCCR1B |= (1 << CS12); - TIMSK1 |= (1 << OCIE1A); + TCCR1B |= _BV(WGM12); + TCCR1B |= _BV(CS11) | _BV(CS10); + TIMSK1 |= _BV(OCIE1A); sei(); } diff --git a/avr.h b/avr.h index 12da855..4c2d5ce 100644 --- a/avr.h +++ b/avr.h @@ -5,10 +5,6 @@ #include #include -// Make sure JIFFY_uS is going to be an integer and not a float! -#define JIFFIES_PER_SECOND 100 -#define JIFFY_uS (1000000 / JIFFIES_PER_SECOND) - #define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit)) #define mode(on) bit(PORTA, _BV(0), on) diff --git a/main.c b/main.c index 197a88c..180c794 100644 --- a/main.c +++ b/main.c @@ -133,7 +133,7 @@ draw() if ((state == TIMEOUT) && (jam_clock % 8 == 0)) { // Blank it out for (clk = 0; clk < 4; clk += 1) { - write(0); + write(setup_digits[1]); } } else { clk = (abs(period_clock / 10) / 60) * 100; @@ -254,24 +254,22 @@ loop() PORTB ^= 0xff; } - if (jiffies == 0) { - switch (state) { - case SETUP: - break; - case JAM: - case LINEUP: - if (period_clock) { - period_clock += 1; - } - // fall through - case TIMEOUT: - if (jam_clock) { - jam_clock += 1; - } - } - - draw(); + switch (state) { + case SETUP: + break; + case JAM: + case LINEUP: + if (period_clock) { + period_clock += 1; + } + // fall through + case TIMEOUT: + if (jam_clock) { + jam_clock += 1; + } } + + draw(); } }