Accurate with 16MHz crystal

This commit is contained in:
Neale Pickett 2013-05-04 19:46:18 -06:00
parent 9400090f1b
commit 4dd06dfa5f
6 changed files with 39 additions and 39 deletions

View File

@ -1,4 +1,6 @@
ARCH = avr ARCH = avr
CFLAGS += -DARCH=$(ARCH)
include Makefile.$(ARCH) include Makefile.$(ARCH)

View File

@ -1,19 +1,20 @@
PROG = main PROG = main
MCU = atmega328p MCU = attiny84
CC = avr-gcc CC = avr-gcc
CFLAGS += -mmcu=$(MCU) CFLAGS += -mmcu=$(MCU)
CFLAGS += -DF_CPU=16000000UL
CFLAGS += -Os CFLAGS += -Os
CFLAGS += -w CFLAGS += -w
LDFLAGS += -mmcu=$(MCU) LDFLAGS += -mmcu=$(MCU)
AVDFLAGS += -p m328p AVDFLAGS += -p $(MCU)
AVDFLAGS += -c arduino AVDFLAGS += -c usbtiny
AVDFLAGS += -b 115200
AVDFLAGS += -P /dev/ttyACM0 FUSES += -U lfuse:w:0x7f:m
FUSES += -U hfuse:w:0xd9:m
FUSES += -U efuse:w:0xff:m
upload: .upload upload: .upload
@ -22,6 +23,9 @@ upload: .upload
avrdude $(AVDFLAGS) -U flash:w:$< avrdude $(AVDFLAGS) -U flash:w:$<
touch $@ touch $@
fuses:
avrdude $(AVDFLAGS) $(FUSES)
$(PROG).hex: $(PROG) $(PROG).hex: $(PROG)
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@ avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@

32
avr.c
View File

@ -13,12 +13,12 @@ extern volatile uint32_t jiffies;
// Count microseconds // Count microseconds
volatile uint32_t micros = 0; volatile uint32_t micros = 0;
// Interrupt called every 1024 µs // Interrupt called every 256 * 64 microseconds
SIGNAL(TIMER0_OVF_vect) ISR(TIM0_OVF_vect)
{ {
uint32_t m = micros; uint32_t m = micros;
m += 1024; m += (256 * 64);
if (m >= JIFFY_uS) { if (m >= JIFFY_uS) {
m %= JIFFY_uS; m %= JIFFY_uS;
tick = true; 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 void
init(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 // Set timer 0 interrupt clock divider to 64
TCCR0B = 0x03; TCCR0B = _BV(CS01) + _BV(CS00);
// enable timer 0 overflow interrupt // enable timer 0 overflow interrupt
TIMSK0 = 0x01; TIMSK0 |= _BV(TOIE0); //enable compare match interrupt;
// Enable interrupts // Enable interrupts
sei(); sei();
DDRD = ~(_BV(NESOUT)); DDRA = ~(_BV(NESOUT));
DDRB = 0xff; DDRB = 0xff;
PORTD = 0; PORTA = 0;
PORTB = 0xff;
} }

19
avr.h
View File

@ -5,24 +5,25 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h> #include <avr/interrupt.h>
// Make sure JIFFY_uS is going to be an integer and not a float! // Make sure JIFFY_uS is going to be an integer and not a float!
#define JIFFIES_PER_SECOND 50 #define JIFFIES_PER_SECOND 100
#define JIFFY_uS (1000000 / JIFFIES_PER_SECOND) #define JIFFY_uS (10000000 / JIFFIES_PER_SECOND)
#define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit)) #define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit))
#define mode(on) bit(PORTD, _BV(0), on) #define mode(on) bit(PORTA, _BV(0), on)
#define sin(on) bit(PORTD, _BV(1), on) #define sin(on) bit(PORTA, _BV(1), on)
#define sclk(on) bit(PORTD, _BV(2), on) #define sclk(on) bit(PORTA, _BV(2), on)
#define xlat(on) bit(PORTD, _BV(3), on) #define xlat(on) bit(PORTA, _BV(3), on)
// Connect GSCLK to SCLK // Connect GSCLK to SCLK
// Connect BLANK to XLAT // Connect BLANK to XLAT
// TRUST ME, THIS TOTALLY WORKS // TRUST ME, THIS TOTALLY WORKS
#define NESOUT 6 #define NESOUT 6
#define nesclk(on) bit(PORTD, _BV(4), on) #define nesclk(on) bit(PORTA, _BV(4), on)
#define nesltch(on) bit(PORTD, _BV(5), on) #define nesltch(on) bit(PORTA, _BV(5), on)
#define nesout() ((PIND & _BV(NESOUT)) << NESOUT) #define nesout() ((PINA & _BV(NESOUT)) << NESOUT)
void init(void); void init(void);

View File

@ -15,10 +15,10 @@ int main (void)
/* wait (10 * 120000) cycles = wait 1200000 cycles */ /* wait (10 * 120000) cycles = wait 1200000 cycles */
counter = 0; counter = 0;
while (counter != 50) while (counter != 10)
{ {
/* wait (30000 x 4) cycles = wait 120000 cycles */ /* wait (30000 x 4) cycles = wait 120000 cycles */
_delay_loop_2(60000); _delay_loop_2(30000);
counter++; counter++;
} }
@ -27,7 +27,7 @@ int main (void)
/* wait (10 * 120000) cycles = wait 1200000 cycles */ /* wait (10 * 120000) cycles = wait 1200000 cycles */
counter = 0; counter = 0;
while (counter != 50) while (counter != 10)
{ {
/* wait (30000 x 4) cycles = wait 120000 cycles */ /* wait (30000 x 4) cycles = wait 120000 cycles */
_delay_loop_2(30000); _delay_loop_2(30000);

3
main.c
View File

@ -5,7 +5,7 @@
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
#if defined (__AVR_ATmega328P__) #if ARCH == avr
# include "avr.h" # include "avr.h"
#else #else
# include "ti.h" # include "ti.h"
@ -249,6 +249,7 @@ loop()
update_controller(); update_controller();
if (jiffies % (JIFFIES_PER_SECOND / 10) == 0) { if (jiffies % (JIFFIES_PER_SECOND / 10) == 0) {
PORTB ^= 0xff;
switch (state) { switch (state) {
case SETUP: case SETUP:
break; break;