Andrew Boyson / lpc1768

Dependents:   test-lpc1768 oldheating gps motorhome ... more

Committer:
andrewboyson
Date:
Mon May 27 10:11:37 2019 +0000
Revision:
51:fb18aa3ec115
Child:
63:b8443aef8d0d
Brought in the timer, random and scan modules as they fit better here.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 51:fb18aa3ec115 1 #include <stdint.h>
andrewboyson 51:fb18aa3ec115 2 #include <stdbool.h>
andrewboyson 51:fb18aa3ec115 3
andrewboyson 51:fb18aa3ec115 4 #include "mstimer.h"
andrewboyson 51:fb18aa3ec115 5 #include "hrtimer.h"
andrewboyson 51:fb18aa3ec115 6
andrewboyson 51:fb18aa3ec115 7 uint32_t MsTimerCount = 0;
andrewboyson 51:fb18aa3ec115 8
andrewboyson 51:fb18aa3ec115 9 bool MsTimerAbsolute(uint32_t untilMs) //This uses signed comparison so it is limited to 24 days
andrewboyson 51:fb18aa3ec115 10 {
andrewboyson 51:fb18aa3ec115 11 return (int)(MsTimerCount - untilMs) > 0;
andrewboyson 51:fb18aa3ec115 12 }
andrewboyson 51:fb18aa3ec115 13
andrewboyson 51:fb18aa3ec115 14 bool MsTimerRelative(uint32_t baseMsCount, uint32_t intervalMs) //This uses unsigned comparison so it is limited to 49 days
andrewboyson 51:fb18aa3ec115 15 {
andrewboyson 51:fb18aa3ec115 16 return MsTimerCount - baseMsCount >= intervalMs;
andrewboyson 51:fb18aa3ec115 17 }
andrewboyson 51:fb18aa3ec115 18
andrewboyson 51:fb18aa3ec115 19 bool MsTimerRepetitive(uint32_t* pBaseMsCount, uint32_t intervalMs)
andrewboyson 51:fb18aa3ec115 20 {
andrewboyson 51:fb18aa3ec115 21 if (MsTimerCount - *pBaseMsCount >= intervalMs) //All unsigned wrap around arithmetic
andrewboyson 51:fb18aa3ec115 22 {
andrewboyson 51:fb18aa3ec115 23 *pBaseMsCount += intervalMs;
andrewboyson 51:fb18aa3ec115 24 return true;
andrewboyson 51:fb18aa3ec115 25 }
andrewboyson 51:fb18aa3ec115 26 return false;
andrewboyson 51:fb18aa3ec115 27 }
andrewboyson 51:fb18aa3ec115 28
andrewboyson 51:fb18aa3ec115 29 void MsTimerMain()
andrewboyson 51:fb18aa3ec115 30 {
andrewboyson 51:fb18aa3ec115 31 static uint32_t baseCount = 0;
andrewboyson 51:fb18aa3ec115 32
andrewboyson 51:fb18aa3ec115 33 if (HrTimerRepetitiveTick(&baseCount, HR_TIMER_COUNT_PER_SECOND / 1000)) MsTimerCount++;
andrewboyson 51:fb18aa3ec115 34 }