Andrew Boyson / clock

Dependents:   oldheating gps motorhome heating

timer.c

Committer:
andrewboyson
Date:
2018-01-22
Revision:
24:6c9833e2a049
Child:
25:81014a201736

File content as of revision 24:6c9833e2a049:

#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;
}