Fix timers in main program too

This commit is contained in:
Neale Pickett 2013-06-30 14:57:44 -06:00
parent a5936d5590
commit fa12096630
7 changed files with 104 additions and 200 deletions

View File

@ -1,6 +1,40 @@
ARCH = avr
PROG = main
MCU = attiny84
CC = avr-gcc
CFLAGS += -mmcu=$(MCU)
CFLAGS += -Os
CFLAGS += -w
LDFLAGS += -mmcu=$(MCU)
AVDFLAGS += -p $(MCU)
AVDFLAGS += -c usbtiny
CLOCK_HZ = 16000000
FUSES += -U lfuse:w:0xff:m
FUSES += -U hfuse:w:0xd9:m
FUSES += -U efuse:w:0xff:m
upload: .upload
.upload: $(PROG).hex
avrdude $(AVDFLAGS) -U flash:w:$<
touch $@
fuses:
avrdude $(AVDFLAGS) $(FUSES)
main: main.o avr.o
$(PROG).hex: $(PROG)
avr-objcopy -O ihex -R .eeprom -R .fuse -R .lock -R .signature $< $@
clean:
rm -f $(PROG) *.o *.hex .upload
CFLAGS += -DARCH=$(ARCH)
include Makefile.$(ARCH)

View File

@ -1,42 +0,0 @@
PROG = main
MCU = attiny84
CC = avr-gcc
CFLAGS += -mmcu=$(MCU)
CFLAGS += -Os
CFLAGS += -w
LDFLAGS += -mmcu=$(MCU)
AVDFLAGS += -p $(MCU)
AVDFLAGS += -c usbtiny
FUSES += -U lfuse:w:0xff:m
FUSES += -U hfuse:w:0xd9:m
FUSES += -U efuse:w:0xff:m
upload: .upload
.upload: $(PROG).hex
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 $< $@
main: main.o avr.o
#main.o: avr.h
#avr.o: avr.h
clean:
rm -f $(PROG) *.o *.hex .upload

View File

@ -1,17 +0,0 @@
CC = msp430-gcc
LDFLAGS += -mmcu=msp430g2553
CFLAGS += -mmcu=msp430g2553
CFLAGS += -Wall
CFLAGS += -Os
CFLAGS += -w
upload: .upload
.upload: main
mspdebug rf2500 "prog $<"
touch $@
main: main.o ti.o
clean:
rm -f main *.o .upload

46
avr.c
View File

@ -4,49 +4,39 @@
#include <avr/interrupt.h>
#include "avr.h"
/* This only works out for a 16MHz or 8MHz clock */
#define CLOCK_HZ 16000000
#define TICKS_PER_SECOND (CLOCK_HZ / 256)
#define TICKS_PER_JIFFY (TICKS_PER_SECOND / 10)
#define cbi(byt, bit) (byt &= ~_BV(bit))
#define sbi(byt, bit) (byt |= _BV(bit))
extern volatile bool tick;
extern volatile uint32_t jiffies;
// Count microseconds
volatile uint32_t micros = 0;
// Interrupt called every 256 * 64 microseconds
ISR(TIM0_OVF_vect)
// Interrupt called every jiffy
ISR(TIM1_COMPA_vect)
{
uint32_t m = micros;
m += (256 * 64);
if (m >= JIFFY_uS) {
m -= JIFFY_uS;
tick = true;
jiffies += 1;
}
micros = m;
tick = true;
}
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 = _BV(CS01) + _BV(CS00);
// enable timer 0 overflow interrupt
TIMSK0 |= _BV(TOIE0); //enable compare match interrupt;
// Enable interrupts
sei();
int i;
DDRA = ~(_BV(NESOUT));
DDRB = 0xff;
PORTA = 0;
PORTB = 0xff;
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
OCR1A = TICKS_PER_JIFFY - 1;
TCCR1B |= (1 << WGM12);
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << OCIE1A);
sei();
}

27
main.c
View File

@ -5,11 +5,7 @@
#include <avr/io.h>
#include <util/delay.h>
#if ARCH == avr
# include "avr.h"
#else
# include "ti.h"
#endif
#include "avr.h"
// Number of shift registers in your scoreboard
// If you want scores to go over 199, you need 8
@ -19,9 +15,16 @@ const int nsr = 6;
// Timing stuff
//
volatile uint32_t jiffies = 0;
//
// 2**32 deciseconds = 13.610221 years
//
// As long as you unplug your scoreboard once every 10 years or so,
// you're good.
//
volatile uint32_t jiffies = 0; // Elapsed time in deciseconds
volatile bool tick = false; // Set high when jiffy clock ticks
// Clocks are in deciseconds
uint16_t score_a = 0;
uint16_t score_b = 0;
@ -108,7 +111,7 @@ draw()
{
uint16_t clk;
//XXX testing
// XXX testing
write_num(score_b, 2);
@ -223,7 +226,7 @@ setup()
{
int i;
// Datasheet says you have to do this before DC initialization.
// TLC5941 datasheet says you have to do this before DC initialization.
// In practice it doesn't seem to matter, but what the hey.
draw();
@ -247,10 +250,11 @@ loop()
update_controller();
if (jiffies % 66 == 0) {
if (jiffies % 10 == 0) {
PORTB ^= 0xff;
}
if (jiffies % (JIFFIES_PER_SECOND / 10) == 0) {
if (jiffies == 0) {
switch (state) {
case SETUP:
break;
@ -281,6 +285,3 @@ main(void)
}
return 0;
}

34
ti.c
View File

@ -1,34 +0,0 @@
#include <stdbool.h>
#include <stdint.h>
#include "ti.h"
extern volatile bool tick;
extern volatile uint32_t jiffies;
// Count microseconds
volatile uint32_t micros = 0;
// Interrupt called every 1024 µs
__attribute__((interrupt(TIMER0_A0_VECTOR)))
void
timer_a(void)
{
tick = true;
jiffies += 1;
}
void
init()
{
WDTCTL = WDTPW + WDTHOLD; // Disable Watchdog Timer
P1DIR = ~(_BV(NESOUT));
P1OUT = 0;
CCTL0 |= CCIE; // Trigger interrupt on A checkpoint
TACTL = TASSEL_2 + MC_1; // Set timer A to SMCLCK, up mode
TACCR0 = 0x4444; // Interrupt 60 times per second
__enable_interrupt();
}

28
ti.h
View File

@ -1,28 +0,0 @@
#ifndef TI_H
#define TI_H
#include <stdint.h>
#include <msp430.h>
#define _BV(x) (1 << x)
#define JIFFIES_PER_SECOND 60
#define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit))
#define mode(on) bit(P1OUT, _BV(0), on)
#define sin(on) bit(P1OUT, _BV(1), on)
#define sclk(on) bit(P1OUT, _BV(2), on)
#define xlat(on) bit(P1OUT, _BV(3), on)
// Connect GSCLK to SCLK
// Connect BLANK to XLAT
// TRUST ME, THIS TOTALLY WORKS
#define NESOUT 6
#define nesclk(on) bit(P1OUT, _BV(4), on)
#define nesltch(on) bit(P1OUT, _BV(5), on)
#define nesout() ((P1IN & _BV(NESOUT)) << NESOUT)
void init();
#endif