Demo program to use the internal timer for long times
Fork of Timer_HelloWorld by
main.cpp@1:455a5cf52a84, 2013-07-25 (annotated)
- Committer:
- pegcjs
- Date:
- Thu Jul 25 11:45:33 2013 +0000
- Revision:
- 1:455a5cf52a84
- Parent:
- 0:27e1de20d3cb
Demo code to use timer to time long times
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:27e1de20d3cb | 1 | #include "mbed.h" |
pegcjs | 1:455a5cf52a84 | 2 | // Expanding the TIMER function to allow timing to very long times |
pegcjs | 1:455a5cf52a84 | 3 | // Demo written for lpc1768 to compare to onboard realtime clock. |
pegcjs | 1:455a5cf52a84 | 4 | // uses highusecs to encode the top 32 bits of the timer output |
pegcjs | 1:455a5cf52a84 | 5 | // and an int to uint conversion to get rid of the annoying negative. |
pegcjs | 1:455a5cf52a84 | 6 | |
mbed_official | 0:27e1de20d3cb | 7 | Timer t; |
pegcjs | 1:455a5cf52a84 | 8 | |
mbed_official | 0:27e1de20d3cb | 9 | |
mbed_official | 0:27e1de20d3cb | 10 | int main() { |
pegcjs | 1:455a5cf52a84 | 11 | set_time(1256729737); // Set RTC time to Wed, 28 Oct 2009 11:35:37 |
pegcjs | 1:455a5cf52a84 | 12 | float umax=0,seconds=0; |
pegcjs | 1:455a5cf52a84 | 13 | unsigned int realusecs=0,oldusecs=0,highusecs=0,minutes=0;; |
pegcjs | 1:455a5cf52a84 | 14 | int dtime,usecs=0; |
pegcjs | 1:455a5cf52a84 | 15 | |
mbed_official | 0:27e1de20d3cb | 16 | t.start(); |
pegcjs | 1:455a5cf52a84 | 17 | usecs=t.read_us(); |
pegcjs | 1:455a5cf52a84 | 18 | realusecs=(unsigned int)usecs; |
pegcjs | 1:455a5cf52a84 | 19 | time_t rtcstart = time(NULL); |
pegcjs | 1:455a5cf52a84 | 20 | printf("\n\n\r"); |
pegcjs | 1:455a5cf52a84 | 21 | while(1) |
pegcjs | 1:455a5cf52a84 | 22 | { |
pegcjs | 1:455a5cf52a84 | 23 | wait(1); |
pegcjs | 1:455a5cf52a84 | 24 | oldusecs=realusecs; |
pegcjs | 1:455a5cf52a84 | 25 | usecs=t.read_us(); |
pegcjs | 1:455a5cf52a84 | 26 | realusecs=(unsigned int)usecs; |
pegcjs | 1:455a5cf52a84 | 27 | //if(realusecs==0) t.reset(); // timer counts back up to 0 and finishes there |
pegcjs | 1:455a5cf52a84 | 28 | if(realusecs<oldusecs) { // its rolled over |
pegcjs | 1:455a5cf52a84 | 29 | highusecs++; // increment highbyte |
pegcjs | 1:455a5cf52a84 | 30 | } |
pegcjs | 1:455a5cf52a84 | 31 | seconds=((float)realusecs/1000000.0)+4294.967296*(float)highusecs; |
pegcjs | 1:455a5cf52a84 | 32 | minutes=(int)(seconds/60); |
pegcjs | 1:455a5cf52a84 | 33 | |
pegcjs | 1:455a5cf52a84 | 34 | if(umax<realusecs) umax=realusecs; |
pegcjs | 1:455a5cf52a84 | 35 | time_t rtcsecs = time(NULL); |
pegcjs | 1:455a5cf52a84 | 36 | dtime=rtcsecs-rtcstart; |
pegcjs | 1:455a5cf52a84 | 37 | printf("Seconds=%6.2f mins=%d realus=%u hus=%u us=%d rtcs=%d oldusecs=%u \r",seconds,minutes,realusecs,highusecs,usecs,dtime,oldusecs); |
pegcjs | 1:455a5cf52a84 | 38 | |
pegcjs | 1:455a5cf52a84 | 39 | } |
pegcjs | 1:455a5cf52a84 | 40 | |
pegcjs | 1:455a5cf52a84 | 41 | |
mbed_official | 0:27e1de20d3cb | 42 | } |