My attempt at using the mbed without the mbed libraries.
Here is a main.cpp, that uses the registers library.
main.cpp@2:e26c096f1946, 2012-12-30 (annotated)
- Committer:
- elevatorguy
- Date:
- Sun Dec 30 04:58:42 2012 +0000
- Revision:
- 2:e26c096f1946
- Parent:
- 0:5fa5046f2ff5
- Child:
- 3:88da6c0412b0
Timer work
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elevatorguy | 2:e26c096f1946 | 1 | #include "registers.h" |
elevatorguy | 0:5fa5046f2ff5 | 2 | DigitalOut led1(LED1); |
elevatorguy | 0:5fa5046f2ff5 | 3 | DigitalOut led2(LED2); |
elevatorguy | 2:e26c096f1946 | 4 | DigitalOut led3(LED3); |
elevatorguy | 2:e26c096f1946 | 5 | DigitalOut led4(LED4); |
elevatorguy | 0:5fa5046f2ff5 | 6 | DigitalIn switch1(p21); |
elevatorguy | 0:5fa5046f2ff5 | 7 | |
elevatorguy | 2:e26c096f1946 | 8 | Serial pc; |
elevatorguy | 2:e26c096f1946 | 9 | Timer t(1000); //ms resolution for a serial stopwatch demo |
elevatorguy | 2:e26c096f1946 | 10 | |
elevatorguy | 2:e26c096f1946 | 11 | int main() { //Timer demonstration |
elevatorguy | 0:5fa5046f2ff5 | 12 | switch1.mode(PULLDOWN); |
elevatorguy | 2:e26c096f1946 | 13 | t.start(); |
elevatorguy | 2:e26c096f1946 | 14 | |
elevatorguy | 2:e26c096f1946 | 15 | while(1) |
elevatorguy | 2:e26c096f1946 | 16 | { |
elevatorguy | 2:e26c096f1946 | 17 | double time = t; |
elevatorguy | 2:e26c096f1946 | 18 | int h = 0; |
elevatorguy | 2:e26c096f1946 | 19 | int m = 0; |
elevatorguy | 2:e26c096f1946 | 20 | while(time > 3600) |
elevatorguy | 2:e26c096f1946 | 21 | { |
elevatorguy | 2:e26c096f1946 | 22 | time = time - 3600; |
elevatorguy | 2:e26c096f1946 | 23 | h = h + 1; |
elevatorguy | 2:e26c096f1946 | 24 | } |
elevatorguy | 2:e26c096f1946 | 25 | while(time > 60) |
elevatorguy | 2:e26c096f1946 | 26 | { |
elevatorguy | 2:e26c096f1946 | 27 | time = time - 60; |
elevatorguy | 2:e26c096f1946 | 28 | m = m + 1; |
elevatorguy | 2:e26c096f1946 | 29 | } |
elevatorguy | 2:e26c096f1946 | 30 | if(h < 10.0) |
elevatorguy | 2:e26c096f1946 | 31 | { |
elevatorguy | 2:e26c096f1946 | 32 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 33 | } |
elevatorguy | 2:e26c096f1946 | 34 | if(h != 0) |
elevatorguy | 2:e26c096f1946 | 35 | { |
elevatorguy | 2:e26c096f1946 | 36 | pc.printf(h); |
elevatorguy | 2:e26c096f1946 | 37 | } |
elevatorguy | 2:e26c096f1946 | 38 | else |
elevatorguy | 2:e26c096f1946 | 39 | { |
elevatorguy | 2:e26c096f1946 | 40 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 41 | } |
elevatorguy | 2:e26c096f1946 | 42 | pc.putc(':'); |
elevatorguy | 2:e26c096f1946 | 43 | if(m < 10.0) |
elevatorguy | 2:e26c096f1946 | 44 | { |
elevatorguy | 2:e26c096f1946 | 45 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 46 | } |
elevatorguy | 2:e26c096f1946 | 47 | if(m != 0) |
elevatorguy | 2:e26c096f1946 | 48 | { |
elevatorguy | 2:e26c096f1946 | 49 | pc.printf(m); |
elevatorguy | 2:e26c096f1946 | 50 | } |
elevatorguy | 2:e26c096f1946 | 51 | else |
elevatorguy | 2:e26c096f1946 | 52 | { |
elevatorguy | 2:e26c096f1946 | 53 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 54 | } |
elevatorguy | 2:e26c096f1946 | 55 | pc.putc(':'); |
elevatorguy | 2:e26c096f1946 | 56 | if(time < 10.0) |
elevatorguy | 2:e26c096f1946 | 57 | { |
elevatorguy | 2:e26c096f1946 | 58 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 59 | } |
elevatorguy | 2:e26c096f1946 | 60 | if(time < 1.0) |
elevatorguy | 2:e26c096f1946 | 61 | { |
elevatorguy | 2:e26c096f1946 | 62 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 63 | } |
elevatorguy | 2:e26c096f1946 | 64 | pc.printf(time, 100); |
elevatorguy | 2:e26c096f1946 | 65 | int timeAsInt = time; |
elevatorguy | 2:e26c096f1946 | 66 | if((time-timeAsInt)*100 < 10.0) |
elevatorguy | 2:e26c096f1946 | 67 | { |
elevatorguy | 2:e26c096f1946 | 68 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 69 | } |
elevatorguy | 2:e26c096f1946 | 70 | if((time-timeAsInt)*100 < 1.0) |
elevatorguy | 2:e26c096f1946 | 71 | { |
elevatorguy | 2:e26c096f1946 | 72 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 73 | } |
elevatorguy | 2:e26c096f1946 | 74 | pc.printf("\r\n"); |
elevatorguy | 0:5fa5046f2ff5 | 75 | } |
elevatorguy | 0:5fa5046f2ff5 | 76 | } |