![](/media/cache/profiles/4b602c3f2f6b86b43ad406fd438e09a8.jpg.50x50_q85.jpg)
Basic timer on mBed 1768
Fork of HelloWorld by
main.cpp@2:253054128736, 2014-09-28 (annotated)
- Committer:
- jatinsha
- Date:
- Sun Sep 28 03:02:57 2014 +0000
- Revision:
- 2:253054128736
- Parent:
- 0:fb6bbc10ffa0
- Child:
- 3:d18d3609c800
timer with Start, Pause/Unpause and Reset ; 24ms error (slow) in 3.9 seconds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jatinsha | 2:253054128736 | 1 | /* |
jatinsha | 2:253054128736 | 2 | CIS 541 - Embedded Systems for Life Critical Applications |
jatinsha | 2:253054128736 | 3 | Assignment3 |
jatinsha | 2:253054128736 | 4 | Timer using mBed 1768 |
jatinsha | 2:253054128736 | 5 | |
jatinsha | 2:253054128736 | 6 | Developers - Jatin Sharma and Samarth Shah |
jatinsha | 2:253054128736 | 7 | |
jatinsha | 2:253054128736 | 8 | References |
jatinsha | 2:253054128736 | 9 | 1. Timer : http://mbed.org/handbook/Timer |
jatinsha | 2:253054128736 | 10 | 2. String format modifiers : http://www.cdf.toronto.edu/~ajr/209/notes/printf.html |
jatinsha | 2:253054128736 | 11 | 3. Keyboard Input : http://mbed.org/handbook/SerialPC |
jatinsha | 2:253054128736 | 12 | 4. Interrupt attach : http://mbed.org/handbook/Serial |
jatinsha | 2:253054128736 | 13 | 5. Enum tutorial : http://stackoverflow.com/questions/1102542/how-to-define-an-enumerated-type-enum-in-c |
jatinsha | 2:253054128736 | 14 | 6. |
jatinsha | 2:253054128736 | 15 | |
jatinsha | 2:253054128736 | 16 | */ |
jatinsha | 2:253054128736 | 17 | |
simon | 0:fb6bbc10ffa0 | 18 | |
jatinsha | 2:253054128736 | 19 | #include "mbed.h" |
jatinsha | 2:253054128736 | 20 | #include "TextLCD.h" |
jatinsha | 2:253054128736 | 21 | |
jatinsha | 2:253054128736 | 22 | // RS, E, D4 - D7 for 4 bit mode |
jatinsha | 2:253054128736 | 23 | #define RS p15 |
jatinsha | 2:253054128736 | 24 | #define E p16 |
jatinsha | 2:253054128736 | 25 | #define D4 p17 |
jatinsha | 2:253054128736 | 26 | #define D5 p18 |
jatinsha | 2:253054128736 | 27 | #define D6 p19 |
jatinsha | 2:253054128736 | 28 | #define D7 p20 |
jatinsha | 2:253054128736 | 29 | #define interval 10 |
jatinsha | 2:253054128736 | 30 | |
jatinsha | 2:253054128736 | 31 | TextLCD lcd(RS, E, D4, D5, D6, D7, TextLCD::LCD16x2); |
jatinsha | 2:253054128736 | 32 | Timer timerDisplay, timerReference; |
jatinsha | 2:253054128736 | 33 | Serial pc(USBTX, USBRX); // tx, rx |
jatinsha | 2:253054128736 | 34 | |
jatinsha | 2:253054128736 | 35 | int miliSeconds, centiSeconds, seconds, minutes; |
jatinsha | 2:253054128736 | 36 | int currentReferenceTime, previousReferenceTime; |
jatinsha | 2:253054128736 | 37 | int calculatedTimeInterval, referenceTimeInterval, runningError; |
jatinsha | 2:253054128736 | 38 | char keyboardInput; |
simon | 0:fb6bbc10ffa0 | 39 | |
jatinsha | 2:253054128736 | 40 | typedef enum {Running, Paused, Reset} state_t; |
jatinsha | 2:253054128736 | 41 | state_t timerState = Reset; |
jatinsha | 2:253054128736 | 42 | |
jatinsha | 2:253054128736 | 43 | void callback() |
jatinsha | 2:253054128736 | 44 | { |
jatinsha | 2:253054128736 | 45 | // Keyboard input : you need to actually read from the serial to clear the RX interrupt |
jatinsha | 2:253054128736 | 46 | if(keyboardInput = pc.getc()) |
jatinsha | 2:253054128736 | 47 | { |
jatinsha | 2:253054128736 | 48 | if((keyboardInput == 's' || keyboardInput == 'S')) |
jatinsha | 2:253054128736 | 49 | { |
jatinsha | 2:253054128736 | 50 | if(timerState == Reset) |
jatinsha | 2:253054128736 | 51 | { |
jatinsha | 2:253054128736 | 52 | timerReference.start(); |
jatinsha | 2:253054128736 | 53 | timerDisplay.start(); |
jatinsha | 2:253054128736 | 54 | timerState = Running; |
jatinsha | 2:253054128736 | 55 | } |
jatinsha | 2:253054128736 | 56 | } |
jatinsha | 2:253054128736 | 57 | if((keyboardInput == 'p' || keyboardInput == 'P')) |
jatinsha | 2:253054128736 | 58 | { |
jatinsha | 2:253054128736 | 59 | if(timerState == Running) |
jatinsha | 2:253054128736 | 60 | { |
jatinsha | 2:253054128736 | 61 | timerReference.stop(); |
jatinsha | 2:253054128736 | 62 | timerDisplay.stop(); |
jatinsha | 2:253054128736 | 63 | timerState = Paused; |
jatinsha | 2:253054128736 | 64 | } |
jatinsha | 2:253054128736 | 65 | else if(timerState == Paused) |
jatinsha | 2:253054128736 | 66 | { |
jatinsha | 2:253054128736 | 67 | timerReference.start(); |
jatinsha | 2:253054128736 | 68 | timerDisplay.start(); |
jatinsha | 2:253054128736 | 69 | timerState = Running; |
jatinsha | 2:253054128736 | 70 | } |
jatinsha | 2:253054128736 | 71 | } |
jatinsha | 2:253054128736 | 72 | if((keyboardInput == 'r' || keyboardInput == 'R')) |
jatinsha | 2:253054128736 | 73 | { |
jatinsha | 2:253054128736 | 74 | if(timerState == Paused) |
jatinsha | 2:253054128736 | 75 | { |
jatinsha | 2:253054128736 | 76 | // Reset timer |
jatinsha | 2:253054128736 | 77 | timerReference.reset(); |
jatinsha | 2:253054128736 | 78 | timerDisplay.reset(); |
jatinsha | 2:253054128736 | 79 | timerState = Reset; |
jatinsha | 2:253054128736 | 80 | |
jatinsha | 2:253054128736 | 81 | // Reset variables |
jatinsha | 2:253054128736 | 82 | miliSeconds = seconds = minutes = 0; |
jatinsha | 2:253054128736 | 83 | currentReferenceTime = previousReferenceTime = 0; |
jatinsha | 2:253054128736 | 84 | calculatedTimeInterval = referenceTimeInterval = runningError = 0; |
jatinsha | 2:253054128736 | 85 | lcd.locate(0,0); |
jatinsha | 2:253054128736 | 86 | lcd.printf("00:00:00"); |
jatinsha | 2:253054128736 | 87 | lcd.locate(0,1); |
jatinsha | 2:253054128736 | 88 | lcd.printf("000000"); |
jatinsha | 2:253054128736 | 89 | } |
jatinsha | 2:253054128736 | 90 | } |
simon | 0:fb6bbc10ffa0 | 91 | } |
jatinsha | 2:253054128736 | 92 | } |
jatinsha | 2:253054128736 | 93 | |
jatinsha | 2:253054128736 | 94 | int main() |
jatinsha | 2:253054128736 | 95 | { |
jatinsha | 2:253054128736 | 96 | // interrupt service routine |
jatinsha | 2:253054128736 | 97 | pc.attach(&callback); |
jatinsha | 2:253054128736 | 98 | |
jatinsha | 2:253054128736 | 99 | // local variable |
jatinsha | 2:253054128736 | 100 | miliSeconds = seconds = minutes = 0; |
jatinsha | 2:253054128736 | 101 | currentReferenceTime = previousReferenceTime = 0; |
jatinsha | 2:253054128736 | 102 | calculatedTimeInterval = referenceTimeInterval = runningError = 0; |
jatinsha | 2:253054128736 | 103 | //timerReference.start(); |
jatinsha | 2:253054128736 | 104 | //timerDisplay.start(); |
jatinsha | 2:253054128736 | 105 | |
jatinsha | 2:253054128736 | 106 | // Initial display work |
jatinsha | 2:253054128736 | 107 | lcd.locate(0,0); |
jatinsha | 2:253054128736 | 108 | lcd.printf("00:00:00"); |
jatinsha | 2:253054128736 | 109 | |
jatinsha | 2:253054128736 | 110 | while(1) |
jatinsha | 2:253054128736 | 111 | { |
jatinsha | 2:253054128736 | 112 | if( timerDisplay.read_ms() >= interval) |
jatinsha | 2:253054128736 | 113 | { |
jatinsha | 2:253054128736 | 114 | timerDisplay.reset(); |
jatinsha | 2:253054128736 | 115 | |
jatinsha | 2:253054128736 | 116 | // Calculate display variables |
jatinsha | 2:253054128736 | 117 | miliSeconds += interval; |
jatinsha | 2:253054128736 | 118 | |
jatinsha | 2:253054128736 | 119 | centiSeconds += (miliSeconds / 10); |
jatinsha | 2:253054128736 | 120 | miliSeconds %= 10; |
jatinsha | 2:253054128736 | 121 | |
jatinsha | 2:253054128736 | 122 | seconds += (centiSeconds / 100); |
jatinsha | 2:253054128736 | 123 | centiSeconds %= 100; |
jatinsha | 2:253054128736 | 124 | |
jatinsha | 2:253054128736 | 125 | minutes += (seconds / 60); |
jatinsha | 2:253054128736 | 126 | seconds %= 60; |
jatinsha | 2:253054128736 | 127 | |
jatinsha | 2:253054128736 | 128 | // Error correction using referenceClock |
jatinsha | 2:253054128736 | 129 | currentReferenceTime = timerReference.read_us(); |
jatinsha | 2:253054128736 | 130 | referenceTimeInterval = currentReferenceTime - previousReferenceTime; |
jatinsha | 2:253054128736 | 131 | runningError += (referenceTimeInterval - interval*1000); |
jatinsha | 2:253054128736 | 132 | |
jatinsha | 2:253054128736 | 133 | //miliSeconds += runningError; |
jatinsha | 2:253054128736 | 134 | previousReferenceTime = currentReferenceTime; |
jatinsha | 2:253054128736 | 135 | |
jatinsha | 2:253054128736 | 136 | |
jatinsha | 2:253054128736 | 137 | // Display time on LCD |
jatinsha | 2:253054128736 | 138 | lcd.locate(0,0); |
jatinsha | 2:253054128736 | 139 | lcd.printf("%02d:%02d:%02d",minutes, seconds, centiSeconds); |
jatinsha | 2:253054128736 | 140 | |
jatinsha | 2:253054128736 | 141 | lcd.locate(0,1); |
jatinsha | 2:253054128736 | 142 | lcd.printf("%06d", runningError); |
jatinsha | 2:253054128736 | 143 | } |
jatinsha | 2:253054128736 | 144 | |
jatinsha | 2:253054128736 | 145 | |
jatinsha | 2:253054128736 | 146 | |
jatinsha | 2:253054128736 | 147 | |
jatinsha | 2:253054128736 | 148 | |
jatinsha | 2:253054128736 | 149 | } |
jatinsha | 2:253054128736 | 150 | |
simon | 0:fb6bbc10ffa0 | 151 | } |