Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: oldheating gps motorhome heating
timer.c@29:9332cf906aad, 2018-01-27 (annotated)
- Committer:
- andrewboyson
- Date:
- Sat Jan 27 12:17:09 2018 +0000
- Revision:
- 29:9332cf906aad
- Parent:
- 26:0421132e6eaf
- Child:
- 30:212ca42b8779
libraries updated
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andrewboyson | 24:6c9833e2a049 | 1 | #include <stdint.h> |
| andrewboyson | 26:0421132e6eaf | 2 | #include <stdbool.h> |
| andrewboyson | 24:6c9833e2a049 | 3 | |
| andrewboyson | 29:9332cf906aad | 4 | #include "defs.h" |
| andrewboyson | 25:81014a201736 | 5 | #include "timer.h" |
| andrewboyson | 24:6c9833e2a049 | 6 | |
| andrewboyson | 26:0421132e6eaf | 7 | uint32_t TimerNowCount() |
| andrewboyson | 26:0421132e6eaf | 8 | { |
| andrewboyson | 26:0421132e6eaf | 9 | return LPC_TIM0->TC; |
| andrewboyson | 24:6c9833e2a049 | 10 | } |
| andrewboyson | 26:0421132e6eaf | 11 | uint32_t TimerIntervalCount(uint32_t* pLastCount) |
| andrewboyson | 24:6c9833e2a049 | 12 | { |
| andrewboyson | 24:6c9833e2a049 | 13 | uint32_t thisCount = LPC_TIM0->TC; |
| andrewboyson | 24:6c9833e2a049 | 14 | uint32_t period = thisCount - *pLastCount; |
| andrewboyson | 24:6c9833e2a049 | 15 | *pLastCount = thisCount; |
| andrewboyson | 24:6c9833e2a049 | 16 | return period; |
| andrewboyson | 24:6c9833e2a049 | 17 | } |
| andrewboyson | 24:6c9833e2a049 | 18 | uint32_t TimerSinceCount(uint32_t startCount) |
| andrewboyson | 24:6c9833e2a049 | 19 | { |
| andrewboyson | 24:6c9833e2a049 | 20 | return LPC_TIM0->TC - startCount; |
| andrewboyson | 24:6c9833e2a049 | 21 | } |
| andrewboyson | 24:6c9833e2a049 | 22 | uint32_t TimerSinceMs(uint32_t startCount) |
| andrewboyson | 24:6c9833e2a049 | 23 | { |
| andrewboyson | 24:6c9833e2a049 | 24 | uint32_t count = LPC_TIM0->TC - startCount; |
| andrewboyson | 25:81014a201736 | 25 | return count / TIMER_COUNT_PER_MS; |
| andrewboyson | 24:6c9833e2a049 | 26 | } |
| andrewboyson | 26:0421132e6eaf | 27 | |
| andrewboyson | 26:0421132e6eaf | 28 | static uint32_t secondsBaseCount = 0; |
| andrewboyson | 26:0421132e6eaf | 29 | |
| andrewboyson | 26:0421132e6eaf | 30 | uint32_t TimerCountSinceLastSecond() |
| andrewboyson | 26:0421132e6eaf | 31 | { |
| andrewboyson | 26:0421132e6eaf | 32 | return LPC_TIM0->TC - secondsBaseCount; |
| andrewboyson | 26:0421132e6eaf | 33 | } |
| andrewboyson | 26:0421132e6eaf | 34 | int32_t TimerMultiplyFractionalPart(int32_t value, uint32_t timerCountSinceLastSecond) |
| andrewboyson | 26:0421132e6eaf | 35 | { |
| andrewboyson | 26:0421132e6eaf | 36 | int64_t fraction; |
| andrewboyson | 26:0421132e6eaf | 37 | |
| andrewboyson | 26:0421132e6eaf | 38 | fraction = timerCountSinceLastSecond; |
| andrewboyson | 26:0421132e6eaf | 39 | fraction <<= 32; |
| andrewboyson | 26:0421132e6eaf | 40 | fraction /= TIMER_COUNT_PER_SECOND; |
| andrewboyson | 26:0421132e6eaf | 41 | |
| andrewboyson | 26:0421132e6eaf | 42 | return (value * fraction) >> 32; |
| andrewboyson | 26:0421132e6eaf | 43 | } |
| andrewboyson | 26:0421132e6eaf | 44 | |
| andrewboyson | 26:0421132e6eaf | 45 | bool TimerTicked = false; |
| andrewboyson | 26:0421132e6eaf | 46 | |
| andrewboyson | 26:0421132e6eaf | 47 | void TimerMain() |
| andrewboyson | 26:0421132e6eaf | 48 | { |
| andrewboyson | 26:0421132e6eaf | 49 | TimerTicked = TimerCountSinceLastSecond() > TIMER_COUNT_PER_SECOND; |
| andrewboyson | 26:0421132e6eaf | 50 | |
| andrewboyson | 26:0421132e6eaf | 51 | if (TimerTicked) secondsBaseCount += TIMER_COUNT_PER_SECOND; |
| andrewboyson | 26:0421132e6eaf | 52 | } |
| andrewboyson | 26:0421132e6eaf | 53 | void TimerInit() |
| andrewboyson | 26:0421132e6eaf | 54 | { |
| andrewboyson | 26:0421132e6eaf | 55 | LPC_TIM0->TCR = 2; // 21.6.2 Timer Control Register - Reset TC and PC. |
| andrewboyson | 26:0421132e6eaf | 56 | LPC_TIM0->CTCR = 0; // 21.6.3 Count Control Register - Timer mode |
| andrewboyson | 26:0421132e6eaf | 57 | LPC_TIM0->PR = 0; // 21.6.5 Prescale register - Don't prescale 96MHz clock (divide by PR+1). |
| andrewboyson | 26:0421132e6eaf | 58 | LPC_TIM0->MCR = 0; // 21.6.8 Match Control Register - no interrupt or reset |
| andrewboyson | 26:0421132e6eaf | 59 | LPC_TIM0->TCR = 1; // 21.6.2 Timer Control Register - Enable TC and PC |
| andrewboyson | 26:0421132e6eaf | 60 | } |