From 4dd06dfa5f29b5db0ebed94b96ebcac7df41cd6d Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Sat, 4 May 2013 19:46:18 -0600 Subject: [PATCH] Accurate with 16MHz crystal --- Makefile | 2 ++ Makefile.avr | 16 ++++++++++------ avr.c | 32 ++++++++++++-------------------- avr.h | 19 ++++++++++--------- blink.c | 6 +++--- main.c | 3 ++- 6 files changed, 39 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 85a736e..23e3b14 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ ARCH = avr +CFLAGS += -DARCH=$(ARCH) + include Makefile.$(ARCH) diff --git a/Makefile.avr b/Makefile.avr index bcb8009..611776f 100644 --- a/Makefile.avr +++ b/Makefile.avr @@ -1,19 +1,20 @@ PROG = main -MCU = atmega328p +MCU = attiny84 CC = avr-gcc CFLAGS += -mmcu=$(MCU) -CFLAGS += -DF_CPU=16000000UL CFLAGS += -Os CFLAGS += -w LDFLAGS += -mmcu=$(MCU) -AVDFLAGS += -p m328p -AVDFLAGS += -c arduino -AVDFLAGS += -b 115200 -AVDFLAGS += -P /dev/ttyACM0 +AVDFLAGS += -p $(MCU) +AVDFLAGS += -c usbtiny + +FUSES += -U lfuse:w:0x7f:m +FUSES += -U hfuse:w:0xd9:m +FUSES += -U efuse:w:0xff:m upload: .upload @@ -22,6 +23,9 @@ upload: .upload avrdude $(AVDFLAGS) -U flash:w:$< touch $@ +fuses: + avrdude $(AVDFLAGS) $(FUSES) + $(PROG).hex: $(PROG) avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@ diff --git a/avr.c b/avr.c index f46b64c..661d130 100644 --- a/avr.c +++ b/avr.c @@ -13,12 +13,12 @@ extern volatile uint32_t jiffies; // Count microseconds volatile uint32_t micros = 0; -// Interrupt called every 1024 µs -SIGNAL(TIMER0_OVF_vect) +// Interrupt called every 256 * 64 microseconds +ISR(TIM0_OVF_vect) { uint32_t m = micros; - m += 1024; + m += (256 * 64); if (m >= JIFFY_uS) { m %= JIFFY_uS; tick = true; @@ -28,33 +28,25 @@ SIGNAL(TIMER0_OVF_vect) } -#define mode(on) bit(PORTD, _BV(0), on) -#define sin(on) bit(PORTD, _BV(1), on) -#define sclk(on) bit(PORTD, _BV(2), on) -#define xlat(on) bit(PORTD, _BV(3), on) -// Connect GSCLK to SCLK -// Connect BLANK to XLAT -// TRUST ME, THIS TOTALLY WORKS - -#define nesclk(on) bit(PORTD, _BV(4), on) -#define nesltch(on) bit(PORTD, _BV(5), on) -#define nesout() ((PIND & _BV(6)) << 6) - void init(void) { + // Set prescaler to 16, so we get clock ticks at exactly 1MHz + CLKPR = _BV(CLKPCE); + CLKPR = 0x04; + // Set timer 0 interrupt clock divider to 64 - TCCR0B = 0x03; + TCCR0B = _BV(CS01) + _BV(CS00); // enable timer 0 overflow interrupt - TIMSK0 = 0x01; + TIMSK0 |= _BV(TOIE0); //enable compare match interrupt; // Enable interrupts sei(); - DDRD = ~(_BV(NESOUT)); + DDRA = ~(_BV(NESOUT)); DDRB = 0xff; - PORTD = 0; - + PORTA = 0; + PORTB = 0xff; } diff --git a/avr.h b/avr.h index 66fd12d..ac611f0 100644 --- a/avr.h +++ b/avr.h @@ -5,24 +5,25 @@ #include #include + // Make sure JIFFY_uS is going to be an integer and not a float! -#define JIFFIES_PER_SECOND 50 -#define JIFFY_uS (1000000 / JIFFIES_PER_SECOND) +#define JIFFIES_PER_SECOND 100 +#define JIFFY_uS (10000000 / JIFFIES_PER_SECOND) #define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit)) -#define mode(on) bit(PORTD, _BV(0), on) -#define sin(on) bit(PORTD, _BV(1), on) -#define sclk(on) bit(PORTD, _BV(2), on) -#define xlat(on) bit(PORTD, _BV(3), on) +#define mode(on) bit(PORTA, _BV(0), on) +#define sin(on) bit(PORTA, _BV(1), on) +#define sclk(on) bit(PORTA, _BV(2), on) +#define xlat(on) bit(PORTA, _BV(3), on) // Connect GSCLK to SCLK // Connect BLANK to XLAT // TRUST ME, THIS TOTALLY WORKS #define NESOUT 6 -#define nesclk(on) bit(PORTD, _BV(4), on) -#define nesltch(on) bit(PORTD, _BV(5), on) -#define nesout() ((PIND & _BV(NESOUT)) << NESOUT) +#define nesclk(on) bit(PORTA, _BV(4), on) +#define nesltch(on) bit(PORTA, _BV(5), on) +#define nesout() ((PINA & _BV(NESOUT)) << NESOUT) void init(void); diff --git a/blink.c b/blink.c index aa63e92..02fb07d 100644 --- a/blink.c +++ b/blink.c @@ -15,10 +15,10 @@ int main (void) /* wait (10 * 120000) cycles = wait 1200000 cycles */ counter = 0; - while (counter != 50) + while (counter != 10) { /* wait (30000 x 4) cycles = wait 120000 cycles */ - _delay_loop_2(60000); + _delay_loop_2(30000); counter++; } @@ -27,7 +27,7 @@ int main (void) /* wait (10 * 120000) cycles = wait 1200000 cycles */ counter = 0; - while (counter != 50) + while (counter != 10) { /* wait (30000 x 4) cycles = wait 120000 cycles */ _delay_loop_2(30000); diff --git a/main.c b/main.c index fa5a5f7..de16dc4 100644 --- a/main.c +++ b/main.c @@ -5,7 +5,7 @@ #include #include -#if defined (__AVR_ATmega328P__) +#if ARCH == avr # include "avr.h" #else # include "ti.h" @@ -249,6 +249,7 @@ loop() update_controller(); if (jiffies % (JIFFIES_PER_SECOND / 10) == 0) { + PORTB ^= 0xff; switch (state) { case SETUP: break;