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
CFLAGS += -DARCH=$(ARCH)
include Makefile.$(ARCH)

View File

@ -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 $< $@

32
avr.c
View File

@ -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;
}

19
avr.h
View File

@ -5,24 +5,25 @@
#include <avr/io.h>
#include <avr/interrupt.h>
// 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);

View File

@ -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);

3
main.c
View File

@ -5,7 +5,7 @@
#include <avr/io.h>
#include <util/delay.h>
#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;