--- /dev/null
+/*
+ * File: tmr32.c
+ *
+ * A very simple module that triggers an ISR once every 2 seconds. tmr32 relies
+ * on Timer 1 and a watch crystal.
+ */
+
+
+#include <htc.h>
+#include "tmr32.h"
+#include "isr.h"
+#include "task.h"
+
+static unsigned _tmr32_count;
+
+void tmr32_init()
+{
+ /* Configure Timer 1 */
+ T1CON = 0b10001001;
+ while (!T1OSCR);
+
+ /* Setup timer to run during sleep, and wake CPU on overflow */
+ nT1SYNC = 1;
+ TMR1IE = 1;
+ PEIE = 1;
+ GIE = 1;
+}
+
+void tmr32_set(unsigned twosecs)
+{
+ ndi();
+ _tmr32_count = twosecs;
+ nei();
+}
+
+void tmr32_isr()
+{
+ if (TMR1IF) {
+ TMR1IF = 0;
+ if (_tmr32_count && --_tmr32_count == 0)
+ task_post(TASK_TMR32);
+ }
+}