Interrupt-based timing

This commit is contained in:
Neale Pickett 2013-03-29 22:39:44 -06:00
parent e6de181523
commit 26c67bee8f
1 changed files with 35 additions and 14 deletions

49
main.c
View File

@ -1,4 +1,5 @@
#include <avr/io.h> #include <avr/io.h>
#include <avr/interrupt.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
@ -25,6 +26,9 @@ enum {
uint8_t last_controller = 0; uint8_t last_controller = 0;
#define cbi(byt, bit) (byt &= ~_BV(bit))
#define sbi(byt, bit) (byt |= _BV(bit))
#define MODE _BV(0) #define MODE _BV(0)
#define SIN _BV(1) #define SIN _BV(1)
#define SCLK _BV(2) #define SCLK _BV(2)
@ -53,7 +57,7 @@ uint8_t last_controller = 0;
#define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit)) #define bit(pin, bit, on) pin = (on ? (pin | bit) : (pin & ~bit))
const uint8_t seven_segment_digits[] = { const uint8_t seven_segment_digits[] = {
0x7b, 0x09, 0xb3, 0x9b, 0xc9, 0xda, 0xfa, 0x0b, 0xfb, 0xdb, 0x7e, 0x48, 0x3d, 0x6d, 0x4b, 0x67, 0x77, 0x4c, 0x7f, 0x6f
}; };
#define mode(on) bit(PORTD, MODE, on) #define mode(on) bit(PORTD, MODE, on)
@ -148,6 +152,11 @@ draw()
{ {
uint16_t clk; uint16_t clk;
//XXX testing
#if 1
write_num(jam_clock / 10, 2);
#else
write_num(score_a, 3); write_num(score_a, 3);
if ((state == TIMEOUT) && (jam_clock % 8 == 0)) { if ((state == TIMEOUT) && (jam_clock % 8 == 0)) {
@ -165,6 +174,7 @@ draw()
write_num(clk, 4); write_num(clk, 4);
write_num(score_b, 2); write_num(score_b, 2);
#endif
latch(); latch();
pulse(); pulse();
@ -278,18 +288,27 @@ main(void)
//setup_gs(); //setup_gs();
setup_dc(); setup_dc();
// this combination is for the standard 168/328/1280/2560
TCCR0B = 0x03;
// enable timer 0 overflow interrupt
TIMSK0 = 0x01;
// Enable interrupts
sei();
// Now actually run // Now actually run
for (;;) { for (;;) {
uint32_t i; uint32_t i;
update_controller(); update_controller();
if (tick) { if (tick) {
tick = false; tick = false;
jiffies += 1; jiffies += 1;
if (jiffies == 6) { if (jiffies == 10) {
jiffies = 0; jiffies = 0;
time += 1; time += 1;
@ -298,19 +317,21 @@ main(void)
PORTB ^= 0xff; PORTB ^= 0xff;
} }
} }
// XXX: fix timer_a
for (i = 0; i < 20000; i += 1) {
tick = !tick;
}
tick = true;
} }
} }
// XXX: reenable this
// Timer A0 interrupt service routine volatile uint32_t micros = 0;
//__attribute__((interrupt(TIMER0_A0_VECTOR)))
void timer_a(void) // This is called every 1024 µs
SIGNAL(TIMER0_OVF_vect)
{ {
tick = true; uint32_t m = micros;
m += 1024;
if (m >= 10000) {
tick = true;
m %= 10000;
}
micros = m;
} }