+/*
+ * File: timer.h
+ *
+ * Timer 0 + Timer 1 for timekeeping
+ */
+
+
+#ifndef _TIMER_H
+#define _TIMER_H
+
+/* Timer 0 with 1:256 prescale given Fosc (_XTAL_FREQ).
+ * Timer 0 is clocked by Fosc/4.
+ *
+ * Fosc Overflow, ms
+ * ------- ----------
+ * 32 MHz 8.192
+ * 16 MHz 16.384
+ * 8 MHz 32.768
+ * 4 MHz 65.536
+ * 2 MHz 131.072
+ * 1 MHz 262.144
+ * 500 KHz 524.288
+ * 250 KHz 1,048.576
+ * 125 KHz 2,097.152
+ */
+
+/* Configure Timer0 to overflow every 32 msec. Adjust with
+ * Fosc as set in picinit.[ch]. At 2 MHz, prescale is 1:64.
+ */
+#define timer_init() \
+ do { \
+ /* TMR0CS = 0, PSA = 0, PS = 0b101 */ \
+ OPTION_REG = (OPTION_REG & 0b11010000) + 0b0101; \
+ } while (0)
+
+/* Wait for the timer to overflow. This is 32 msec from the last overflow. */
+#define timer_owait() \
+ { \
+ while (!TMR0IF); \
+ TMR0IF = 0; \
+ } while (0)
+
+/* Wait for c clocks, 0 <= c < 128. */
+#define timer_cwait(c) \
+ { \
+ unsigned t0 = TMR0; \
+ while ((TMR0 - t0) <= c); \
+ } while (0)
+
+void timer_uwait(unsigned us);
+void timer_mwait(unsigned ms);
+
+#endif