summaryrefslogtreecommitdiff
path: root/tick.c
diff options
context:
space:
mode:
authorDavid Lamparter <equinox@diac24.net>2012-09-20 05:18:03 +0200
committerDavid Lamparter <equinox@diac24.net>2012-09-20 05:18:52 +0200
commit82e31518b134cfd90eda8c096b971903a97708c0 (patch)
tree482e855d4ef5ab96a426f0d5a293712973ad3460 /tick.c
parent2bae1f8a73bdd5fca71fd3e60807d6b618e675d1 (diff)
lightctrl: 1 second system tick
Diffstat (limited to 'tick.c')
-rw-r--r--tick.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/tick.c b/tick.c
new file mode 100644
index 0000000..87dcbc6
--- /dev/null
+++ b/tick.c
@@ -0,0 +1,37 @@
+#define HAVE_TICK
+
+static union {
+ uint32_t u32;
+ uint8_t u8[4];
+} systick = { .u32 = 0 };
+
+ISR(TIMER1_COMPA_vect)
+{
+ systick.u32++;
+}
+
+static void uart_puttick(void)
+{
+ uint16_t frac = TCNT1;
+ _uart_putch('@');
+ uart_puthex(systick.u8[3]);
+ uart_puthex(systick.u8[2]);
+ uart_puthex(systick.u8[1]);
+ uart_puthex(systick.u8[0]);
+ _uart_putch('.');
+ uart_puthex16(frac);
+ _uart_putch(' ');
+}
+
+static void tick_init(void)
+{
+ TCCR1A = 0;
+ TCCR1B = (1 << WGM12) | (1 << CS12) | (0 << CS11) | (0 << CS10); // 8 MHz / 256 = 31250 Hz
+ asm volatile ("" ::: "memory");
+ TCNT1 = 0;
+ OCR1A = 31249; // 1 Hz
+ asm volatile ("" ::: "memory");
+ TIFR1 = (1 << OCF1A);
+ TIMSK1 = (1 << OCIE1A);
+}
+