My attempt at using the mbed without the mbed libraries.
Here is a main.cpp, that uses the registers library.
main.cpp@6:d40cd917854d, 2013-01-03 (annotated)
- Committer:
- elevatorguy
- Date:
- Thu Jan 03 05:10:49 2013 +0000
- Revision:
- 6:d40cd917854d
- Parent:
- 4:6ed8900881e8
- Child:
- 7:411ae6b21c9b
better timeout example
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elevatorguy | 2:e26c096f1946 | 1 | #include "registers.h" |
elevatorguy | 3:88da6c0412b0 | 2 | DigitalOut led1(p20); |
elevatorguy | 6:d40cd917854d | 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 | 3:88da6c0412b0 | 8 | Serial pc(115200); |
elevatorguy | 2:e26c096f1946 | 9 | Timer t(1000); //ms resolution for a serial stopwatch demo |
elevatorguy | 2:e26c096f1946 | 10 | |
elevatorguy | 4:6ed8900881e8 | 11 | Timeout tock; |
elevatorguy | 3:88da6c0412b0 | 12 | Ticker tock2; |
elevatorguy | 3:88da6c0412b0 | 13 | |
elevatorguy | 6:d40cd917854d | 14 | extern "C" void turnOff(void) { |
elevatorguy | 6:d40cd917854d | 15 | led1 = 0; |
elevatorguy | 3:88da6c0412b0 | 16 | } |
elevatorguy | 3:88da6c0412b0 | 17 | |
elevatorguy | 6:d40cd917854d | 18 | extern "C" void turnOn(void) { |
elevatorguy | 6:d40cd917854d | 19 | led1 = 1; |
elevatorguy | 6:d40cd917854d | 20 | tock.attach(&turnOff, 0.05); |
elevatorguy | 3:88da6c0412b0 | 21 | } |
elevatorguy | 3:88da6c0412b0 | 22 | |
elevatorguy | 3:88da6c0412b0 | 23 | int main() { //Timer and Ticker demonstration |
elevatorguy | 0:5fa5046f2ff5 | 24 | switch1.mode(PULLDOWN); |
elevatorguy | 2:e26c096f1946 | 25 | t.start(); |
elevatorguy | 6:d40cd917854d | 26 | tock2.attach(&turnOn, 0.5); |
elevatorguy | 2:e26c096f1946 | 27 | while(1) |
elevatorguy | 2:e26c096f1946 | 28 | { |
elevatorguy | 2:e26c096f1946 | 29 | double time = t; |
elevatorguy | 2:e26c096f1946 | 30 | int h = 0; |
elevatorguy | 2:e26c096f1946 | 31 | int m = 0; |
elevatorguy | 2:e26c096f1946 | 32 | while(time > 3600) |
elevatorguy | 2:e26c096f1946 | 33 | { |
elevatorguy | 2:e26c096f1946 | 34 | time = time - 3600; |
elevatorguy | 2:e26c096f1946 | 35 | h = h + 1; |
elevatorguy | 2:e26c096f1946 | 36 | } |
elevatorguy | 2:e26c096f1946 | 37 | while(time > 60) |
elevatorguy | 2:e26c096f1946 | 38 | { |
elevatorguy | 2:e26c096f1946 | 39 | time = time - 60; |
elevatorguy | 2:e26c096f1946 | 40 | m = m + 1; |
elevatorguy | 2:e26c096f1946 | 41 | } |
elevatorguy | 2:e26c096f1946 | 42 | if(h < 10.0) |
elevatorguy | 2:e26c096f1946 | 43 | { |
elevatorguy | 2:e26c096f1946 | 44 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 45 | } |
elevatorguy | 2:e26c096f1946 | 46 | if(h != 0) |
elevatorguy | 2:e26c096f1946 | 47 | { |
elevatorguy | 2:e26c096f1946 | 48 | pc.printf(h); |
elevatorguy | 2:e26c096f1946 | 49 | } |
elevatorguy | 2:e26c096f1946 | 50 | else |
elevatorguy | 2:e26c096f1946 | 51 | { |
elevatorguy | 2:e26c096f1946 | 52 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 53 | } |
elevatorguy | 2:e26c096f1946 | 54 | pc.putc(':'); |
elevatorguy | 2:e26c096f1946 | 55 | if(m < 10.0) |
elevatorguy | 2:e26c096f1946 | 56 | { |
elevatorguy | 2:e26c096f1946 | 57 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 58 | } |
elevatorguy | 2:e26c096f1946 | 59 | if(m != 0) |
elevatorguy | 2:e26c096f1946 | 60 | { |
elevatorguy | 2:e26c096f1946 | 61 | pc.printf(m); |
elevatorguy | 2:e26c096f1946 | 62 | } |
elevatorguy | 2:e26c096f1946 | 63 | else |
elevatorguy | 2:e26c096f1946 | 64 | { |
elevatorguy | 2:e26c096f1946 | 65 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 66 | } |
elevatorguy | 2:e26c096f1946 | 67 | pc.putc(':'); |
elevatorguy | 2:e26c096f1946 | 68 | if(time < 10.0) |
elevatorguy | 2:e26c096f1946 | 69 | { |
elevatorguy | 2:e26c096f1946 | 70 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 71 | } |
elevatorguy | 2:e26c096f1946 | 72 | if(time < 1.0) |
elevatorguy | 2:e26c096f1946 | 73 | { |
elevatorguy | 2:e26c096f1946 | 74 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 75 | } |
elevatorguy | 2:e26c096f1946 | 76 | pc.printf(time, 100); |
elevatorguy | 2:e26c096f1946 | 77 | int timeAsInt = time; |
elevatorguy | 2:e26c096f1946 | 78 | if((time-timeAsInt)*100 < 10.0) |
elevatorguy | 2:e26c096f1946 | 79 | { |
elevatorguy | 2:e26c096f1946 | 80 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 81 | } |
elevatorguy | 2:e26c096f1946 | 82 | if((time-timeAsInt)*100 < 1.0) |
elevatorguy | 2:e26c096f1946 | 83 | { |
elevatorguy | 2:e26c096f1946 | 84 | pc.putc('0'); |
elevatorguy | 2:e26c096f1946 | 85 | } |
elevatorguy | 2:e26c096f1946 | 86 | pc.printf("\r\n"); |
elevatorguy | 0:5fa5046f2ff5 | 87 | } |
elevatorguy | 0:5fa5046f2ff5 | 88 | } |