]> oss.titaniummirror.com Git - rgblamp.git/blobdiff - timer.c
Clock loop start to loop start using Timer 0
[rgblamp.git] / timer.c
diff --git a/timer.c b/timer.c
new file mode 100644 (file)
index 0000000..a76e2c2
--- /dev/null
+++ b/timer.c
@@ -0,0 +1,40 @@
+/*
+ * File:   timer.c
+ *
+ * Timer 0 + Timer 1 for timekeeping
+ */
+
+
+#include <htc.h>
+
+void timer_uwait(unsigned us)
+{
+  unsigned t0 = TMR0;
+
+  TMR0IF = 0;
+  while (us >= 32768) {
+    timer_owait();
+    us -= 32768;
+  }
+  while (us >= 16384) {
+    timer_cwait(128);
+    us -= 16384;
+  }
+  timer_cwait(us / 128);
+}
+
+void timer_mwait(unsigned ms)
+{
+  unsigned t0 = TMR0;
+
+  TMR0IF = 0;
+  while (ms >= 32) {
+    timer_owait();
+    ms -= 32;
+  }
+  while (ms >= 16) {
+    timer_cwait(128);
+    ms -= 16;
+  }
+  timer_cwait(ms * 8);
+}