hw-rollerderby-scoreboard/avr.c

48 lines
824 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"
2014-01-11 21:02:24 -07:00
#include "config.h"
2013-03-31 21:13:51 -06:00
2013-07-02 19:54:39 -06:00
/* Clock must be a multiple of 2MHz or there will be clock drift */
2014-01-11 21:54:50 -07:00
#define TICK_HZ (CLOCK_HZ / 8 / 64)
#define TICKS_PER_JIFFY (TICK_HZ / 10)
2013-06-30 14:57:44 -06:00
2013-03-31 21:13:51 -06:00
#define cbi(byt, bit) (byt &= ~_BV(bit))
#define sbi(byt, bit) (byt |= _BV(bit))
extern volatile bool tick;
extern volatile uint32_t jiffies;
2013-06-30 14:57:44 -06:00
// Interrupt called every jiffy
ISR(TIM1_COMPA_vect)
2013-03-31 21:13:51 -06:00
{
2013-06-30 14:57:44 -06:00
jiffies += 1;
tick = true;
2013-03-31 21:13:51 -06:00
}
void
2014-02-02 12:31:33 -07:00
avr_init(void)
2013-03-31 21:13:51 -06:00
{
2013-06-30 14:57:44 -06:00
int i;
2013-03-31 21:13:51 -06:00
2013-05-04 19:46:18 -06:00
DDRA = ~(_BV(NESOUT));
2013-03-31 21:13:51 -06:00
DDRB = 0xff;
2013-06-30 14:57:44 -06:00
TCCR1A = 0;
TCCR1B = 0;
2014-01-11 21:54:50 -07:00
TCNT1 = 0; // reset counter
2013-06-30 14:57:44 -06:00
OCR1A = TICKS_PER_JIFFY - 1;
2013-07-02 19:54:39 -06:00
TCCR1B |= _BV(WGM12);
2014-01-11 21:54:50 -07:00
TCCR1B |= _BV(CS11) | _BV(CS10); // prescale: clk_io / 64
2013-07-02 19:54:39 -06:00
TIMSK1 |= _BV(OCIE1A);
2013-07-14 07:51:30 -06:00
bit(PORTA, _BV(7), true);
2014-02-02 12:31:33 -07:00
PORTB = 0xff;
2013-06-30 14:57:44 -06:00
sei();
2013-03-31 21:13:51 -06:00
}