]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - tmr32.c
Add tmr32 module
[rgblamp.git] / tmr32.c
diff --git a/tmr32.c b/tmr32.c
new file mode 100644 (file)
index 0000000..a5dc5d5
--- /dev/null
+++ b/tmr32.c
@@ -0,0 +1,43 @@
+/*
+ * 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);
+  }
+}