hw-rollerderby-scoreboard/avr.c

53 lines
906 B
C
Raw Normal View History

2013-03-31 21:13:51 -06:00
#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include "avr.h"
#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;
2013-05-04 19:46:18 -06:00
// Interrupt called every 256 * 64 microseconds
ISR(TIM0_OVF_vect)
2013-03-31 21:13:51 -06:00
{
uint32_t m = micros;
2013-05-04 19:46:18 -06:00
m += (256 * 64);
2013-03-31 21:13:51 -06:00
if (m >= JIFFY_uS) {
m %= JIFFY_uS;
tick = true;
jiffies += 1;
}
micros = m;
}
void
init(void)
{
2013-05-04 19:46:18 -06:00
// Set prescaler to 16, so we get clock ticks at exactly 1MHz
CLKPR = _BV(CLKPCE);
CLKPR = 0x04;
2013-03-31 21:13:51 -06:00
// Set timer 0 interrupt clock divider to 64
2013-05-04 19:46:18 -06:00
TCCR0B = _BV(CS01) + _BV(CS00);
2013-03-31 21:13:51 -06:00
// enable timer 0 overflow interrupt
2013-05-04 19:46:18 -06:00
TIMSK0 |= _BV(TOIE0); //enable compare match interrupt;
2013-03-31 21:13:51 -06:00
// Enable interrupts
sei();
2013-05-04 19:46:18 -06:00
DDRA = ~(_BV(NESOUT));
2013-03-31 21:13:51 -06:00
DDRB = 0xff;
2013-05-04 19:46:18 -06:00
PORTA = 0;
PORTB = 0xff;
2013-03-31 21:13:51 -06:00
}