Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

Committer:
andrewboyson
Date:
Mon Jan 22 18:54:23 2018 +0000
Revision:
25:81014a201736
Parent:
24:6c9833e2a049
Child:
26:0421132e6eaf
Moved timer defines from tick to timer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 24:6c9833e2a049 1 #include <stdint.h>
andrewboyson 24:6c9833e2a049 2
andrewboyson 24:6c9833e2a049 3 #include "peripherals.h"
andrewboyson 25:81014a201736 4 #include "timer.h"
andrewboyson 24:6c9833e2a049 5
andrewboyson 24:6c9833e2a049 6 void TimerInit()
andrewboyson 24:6c9833e2a049 7 {
andrewboyson 24:6c9833e2a049 8 LPC_TIM0->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC.
andrewboyson 24:6c9833e2a049 9 LPC_TIM0->CTCR = 0; // 21.6.3 Count Control Register - Timer mode
andrewboyson 24:6c9833e2a049 10 LPC_TIM0->PR = 0; // 21.6.5 Prescale register - Don't prescale 96MHz clock (divide by PR+1).
andrewboyson 24:6c9833e2a049 11 LPC_TIM0->MCR = 0; // 21.6.8 Match Control Register - no interrupt or reset
andrewboyson 24:6c9833e2a049 12 LPC_TIM0->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC
andrewboyson 24:6c9833e2a049 13 }
andrewboyson 24:6c9833e2a049 14 uint32_t TimerPeriodCount(uint32_t* pLastCount)
andrewboyson 24:6c9833e2a049 15 {
andrewboyson 24:6c9833e2a049 16 uint32_t thisCount = LPC_TIM0->TC;
andrewboyson 24:6c9833e2a049 17 uint32_t period = thisCount - *pLastCount;
andrewboyson 24:6c9833e2a049 18 *pLastCount = thisCount;
andrewboyson 24:6c9833e2a049 19 return period;
andrewboyson 24:6c9833e2a049 20 }
andrewboyson 24:6c9833e2a049 21 uint32_t TimerNowCount()
andrewboyson 24:6c9833e2a049 22 {
andrewboyson 24:6c9833e2a049 23 return LPC_TIM0->TC;
andrewboyson 24:6c9833e2a049 24 }
andrewboyson 24:6c9833e2a049 25 uint32_t TimerSinceCount(uint32_t startCount)
andrewboyson 24:6c9833e2a049 26 {
andrewboyson 24:6c9833e2a049 27 return LPC_TIM0->TC - startCount;
andrewboyson 24:6c9833e2a049 28 }
andrewboyson 24:6c9833e2a049 29 uint32_t TimerSinceMs(uint32_t startCount)
andrewboyson 24:6c9833e2a049 30 {
andrewboyson 24:6c9833e2a049 31 uint32_t count = LPC_TIM0->TC - startCount;
andrewboyson 25:81014a201736 32 return count / TIMER_COUNT_PER_MS;
andrewboyson 24:6c9833e2a049 33 }