Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Revision:
24:6c9833e2a049
Child:
25:81014a201736
diff -r 07b19cd5f6d0 -r 6c9833e2a049 timer.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/timer.c	Mon Jan 22 18:35:11 2018 +0000
@@ -0,0 +1,32 @@
+#include <stdint.h>
+
+#include "peripherals.h"
+
+void TimerInit()
+{    
+    LPC_TIM0->TCR     =     2; // 21.6.2 Timer Control Register - Reset TC and PC.
+    LPC_TIM0->CTCR    =     0; // 21.6.3 Count Control Register - Timer mode
+    LPC_TIM0->PR      =     0; // 21.6.5 Prescale register      - Don't prescale 96MHz clock (divide by PR+1).
+    LPC_TIM0->MCR     =     0; // 21.6.8 Match Control Register - no interrupt or reset
+    LPC_TIM0->TCR     =     1; // 21.6.2 Timer Control Register - Enable TC and PC
+}
+uint32_t TimerPeriodCount(uint32_t* pLastCount)
+{
+    uint32_t thisCount = LPC_TIM0->TC;
+    uint32_t period = thisCount - *pLastCount;    
+    *pLastCount = thisCount;
+    return period;
+}
+uint32_t TimerNowCount()
+{
+    return LPC_TIM0->TC;
+}
+uint32_t TimerSinceCount(uint32_t startCount)
+{
+    return LPC_TIM0->TC - startCount; 
+}
+uint32_t TimerSinceMs(uint32_t startCount)
+{
+    uint32_t count = LPC_TIM0->TC - startCount;
+    return count / 96000;
+}