mBed + timer + keyboard + keypad

Dependencies:   TextLCD keypad mbed

Committer:
jatinsha
Date:
Sun Sep 28 04:32:01 2014 +0000
Revision:
2:0d5060721258
Parent:
1:9b8e060faeab
c2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jatinsha 0:73f8ad8f29bb 1 /*
jatinsha 0:73f8ad8f29bb 2 CIS 541 - Embedded Systems for Life Critical Applications
jatinsha 0:73f8ad8f29bb 3 Assignment3
jatinsha 1:9b8e060faeab 4 Timer using mBed 1768 + 4x4 keypad
jatinsha 0:73f8ad8f29bb 5
jatinsha 0:73f8ad8f29bb 6 Developers - Jatin Sharma and Samarth Shah
jatinsha 0:73f8ad8f29bb 7
jatinsha 0:73f8ad8f29bb 8 References
jatinsha 0:73f8ad8f29bb 9 1. Timer : http://mbed.org/handbook/Timer
jatinsha 0:73f8ad8f29bb 10 2. String format modifiers : http://www.cdf.toronto.edu/~ajr/209/notes/printf.html
jatinsha 0:73f8ad8f29bb 11 3. Keyboard Input : http://mbed.org/handbook/SerialPC
jatinsha 0:73f8ad8f29bb 12 4. Interrupt attach : http://mbed.org/handbook/Serial
jatinsha 0:73f8ad8f29bb 13 5. Enum tutorial : http://stackoverflow.com/questions/1102542/how-to-define-an-enumerated-type-enum-in-c
jatinsha 0:73f8ad8f29bb 14 6. Keypad interfacing : http://mbed.org/users/yoonghm/code/keypad/docs/tip/
jatinsha 0:73f8ad8f29bb 15
jatinsha 0:73f8ad8f29bb 16 */
jatinsha 0:73f8ad8f29bb 17
jatinsha 0:73f8ad8f29bb 18
jatinsha 0:73f8ad8f29bb 19 #include "mbed.h"
jatinsha 0:73f8ad8f29bb 20 #include "TextLCD.h"
jatinsha 0:73f8ad8f29bb 21 #include "Keypad.h"
jatinsha 0:73f8ad8f29bb 22
jatinsha 0:73f8ad8f29bb 23 // RS, E, D4 - D7 for 4 bit mode
jatinsha 0:73f8ad8f29bb 24 #define RS p15
jatinsha 0:73f8ad8f29bb 25 #define E p16
jatinsha 0:73f8ad8f29bb 26 #define D4 p17
jatinsha 0:73f8ad8f29bb 27 #define D5 p18
jatinsha 0:73f8ad8f29bb 28 #define D6 p19
jatinsha 0:73f8ad8f29bb 29 #define D7 p20
jatinsha 0:73f8ad8f29bb 30 #define interval 10
jatinsha 0:73f8ad8f29bb 31
jatinsha 0:73f8ad8f29bb 32 TextLCD lcd(RS, E, D4, D5, D6, D7, TextLCD::LCD16x2);
jatinsha 0:73f8ad8f29bb 33 Timer timerDisplay, timerReference;
jatinsha 0:73f8ad8f29bb 34 Serial pc(USBTX, USBRX); // tx, rx
jatinsha 0:73f8ad8f29bb 35
jatinsha 0:73f8ad8f29bb 36
jatinsha 0:73f8ad8f29bb 37 int miliSeconds, centiSeconds, seconds, minutes;
jatinsha 0:73f8ad8f29bb 38 int currentReferenceTime, previousReferenceTime;
jatinsha 0:73f8ad8f29bb 39 int calculatedTimeInterval, referenceTimeInterval, runningError;
jatinsha 0:73f8ad8f29bb 40 char keyboardInput;
jatinsha 0:73f8ad8f29bb 41 int32_t Index = -1; // keypad state
jatinsha 0:73f8ad8f29bb 42
jatinsha 0:73f8ad8f29bb 43 typedef enum {Running, Paused, Reset} state_t;
jatinsha 0:73f8ad8f29bb 44 state_t timerState = Reset;
jatinsha 0:73f8ad8f29bb 45
jatinsha 0:73f8ad8f29bb 46 // Define your own keypad values
jatinsha 0:73f8ad8f29bb 47 // Define your own keypad values
jatinsha 0:73f8ad8f29bb 48 char Keytable[] = { '1', '2', '3', 'A', // r0
jatinsha 0:73f8ad8f29bb 49 '4', '5', '6', 'B', // r1
jatinsha 0:73f8ad8f29bb 50 '7', '8', '9', 'C', // r2
jatinsha 0:73f8ad8f29bb 51 '*', '0', '#', 'D' // r3
jatinsha 0:73f8ad8f29bb 52 };
jatinsha 0:73f8ad8f29bb 53 // c0 c1 c2 c3
jatinsha 0:73f8ad8f29bb 54
jatinsha 0:73f8ad8f29bb 55
jatinsha 0:73f8ad8f29bb 56 uint32_t cbAfterInput(uint32_t _index)
jatinsha 0:73f8ad8f29bb 57 {
jatinsha 0:73f8ad8f29bb 58 Index = _index;
jatinsha 0:73f8ad8f29bb 59
jatinsha 1:9b8e060faeab 60 if (Index > -1)
jatinsha 1:9b8e060faeab 61 {
jatinsha 1:9b8e060faeab 62 //lcd.locate(0,1);
jatinsha 1:9b8e060faeab 63 //lcd.printf("Key:%c", Keytable[Index]);
jatinsha 1:9b8e060faeab 64
jatinsha 1:9b8e060faeab 65
jatinsha 1:9b8e060faeab 66 if(Keytable[Index] == '*')
jatinsha 1:9b8e060faeab 67 {
jatinsha 1:9b8e060faeab 68 if(timerState == Reset)
jatinsha 1:9b8e060faeab 69 {
jatinsha 1:9b8e060faeab 70 timerReference.start();
jatinsha 1:9b8e060faeab 71 timerDisplay.start();
jatinsha 1:9b8e060faeab 72 timerState = Running;
jatinsha 1:9b8e060faeab 73 }
jatinsha 1:9b8e060faeab 74 else if(timerState == Paused)
jatinsha 1:9b8e060faeab 75 {
jatinsha 1:9b8e060faeab 76 timerReference.start();
jatinsha 1:9b8e060faeab 77 timerDisplay.start();
jatinsha 1:9b8e060faeab 78 timerState = Running;
jatinsha 1:9b8e060faeab 79 }
jatinsha 0:73f8ad8f29bb 80 }
jatinsha 1:9b8e060faeab 81 if(Keytable[Index] == '0')
jatinsha 1:9b8e060faeab 82 {
jatinsha 1:9b8e060faeab 83 if(timerState == Running)
jatinsha 1:9b8e060faeab 84 {
jatinsha 1:9b8e060faeab 85 timerReference.stop();
jatinsha 1:9b8e060faeab 86 timerDisplay.stop();
jatinsha 1:9b8e060faeab 87 timerState = Paused;
jatinsha 1:9b8e060faeab 88 }
jatinsha 1:9b8e060faeab 89
jatinsha 1:9b8e060faeab 90 }
jatinsha 1:9b8e060faeab 91 if(Keytable[Index] == '#')
jatinsha 1:9b8e060faeab 92
jatinsha 1:9b8e060faeab 93 {
jatinsha 1:9b8e060faeab 94 if(timerState == Paused)
jatinsha 1:9b8e060faeab 95 {
jatinsha 1:9b8e060faeab 96 // Reset timer
jatinsha 1:9b8e060faeab 97 timerReference.reset();
jatinsha 1:9b8e060faeab 98 timerDisplay.reset();
jatinsha 1:9b8e060faeab 99 timerState = Reset;
jatinsha 1:9b8e060faeab 100
jatinsha 1:9b8e060faeab 101 // Reset variables
jatinsha 1:9b8e060faeab 102 miliSeconds = seconds = minutes = 0;
jatinsha 1:9b8e060faeab 103 currentReferenceTime = previousReferenceTime = 0;
jatinsha 1:9b8e060faeab 104 calculatedTimeInterval = referenceTimeInterval = runningError = 0;
jatinsha 1:9b8e060faeab 105 lcd.locate(0,0);
jatinsha 1:9b8e060faeab 106 lcd.printf("00:00:00");
jatinsha 2:0d5060721258 107 //lcd.locate(0,1);
jatinsha 2:0d5060721258 108 //lcd.printf("000000");
jatinsha 1:9b8e060faeab 109 }
jatinsha 1:9b8e060faeab 110 }
jatinsha 1:9b8e060faeab 111
jatinsha 1:9b8e060faeab 112 Index = -1;
jatinsha 1:9b8e060faeab 113 }
jatinsha 0:73f8ad8f29bb 114
jatinsha 0:73f8ad8f29bb 115 return 0;
jatinsha 0:73f8ad8f29bb 116 }
jatinsha 0:73f8ad8f29bb 117
jatinsha 0:73f8ad8f29bb 118 void callback()
jatinsha 0:73f8ad8f29bb 119 {
jatinsha 0:73f8ad8f29bb 120 // Keyboard input : you need to actually read from the serial to clear the RX interrupt
jatinsha 0:73f8ad8f29bb 121 if(keyboardInput = pc.getc())
jatinsha 0:73f8ad8f29bb 122 {
jatinsha 0:73f8ad8f29bb 123 if((keyboardInput == 's' || keyboardInput == 'S'))
jatinsha 0:73f8ad8f29bb 124 {
jatinsha 0:73f8ad8f29bb 125 if(timerState == Reset)
jatinsha 0:73f8ad8f29bb 126 {
jatinsha 0:73f8ad8f29bb 127 timerReference.start();
jatinsha 0:73f8ad8f29bb 128 timerDisplay.start();
jatinsha 0:73f8ad8f29bb 129 timerState = Running;
jatinsha 0:73f8ad8f29bb 130 }
jatinsha 1:9b8e060faeab 131 else if(timerState == Paused)
jatinsha 1:9b8e060faeab 132 {
jatinsha 1:9b8e060faeab 133 timerReference.start();
jatinsha 1:9b8e060faeab 134 timerDisplay.start();
jatinsha 1:9b8e060faeab 135 timerState = Running;
jatinsha 1:9b8e060faeab 136 }
jatinsha 0:73f8ad8f29bb 137 }
jatinsha 0:73f8ad8f29bb 138 if((keyboardInput == 'p' || keyboardInput == 'P'))
jatinsha 0:73f8ad8f29bb 139 {
jatinsha 0:73f8ad8f29bb 140 if(timerState == Running)
jatinsha 0:73f8ad8f29bb 141 {
jatinsha 0:73f8ad8f29bb 142 timerReference.stop();
jatinsha 0:73f8ad8f29bb 143 timerDisplay.stop();
jatinsha 0:73f8ad8f29bb 144 timerState = Paused;
jatinsha 0:73f8ad8f29bb 145 }
jatinsha 1:9b8e060faeab 146
jatinsha 0:73f8ad8f29bb 147 }
jatinsha 0:73f8ad8f29bb 148 if((keyboardInput == 'r' || keyboardInput == 'R'))
jatinsha 0:73f8ad8f29bb 149 {
jatinsha 0:73f8ad8f29bb 150 if(timerState == Paused)
jatinsha 0:73f8ad8f29bb 151 {
jatinsha 0:73f8ad8f29bb 152 // Reset timer
jatinsha 0:73f8ad8f29bb 153 timerReference.reset();
jatinsha 0:73f8ad8f29bb 154 timerDisplay.reset();
jatinsha 0:73f8ad8f29bb 155 timerState = Reset;
jatinsha 0:73f8ad8f29bb 156
jatinsha 0:73f8ad8f29bb 157 // Reset variables
jatinsha 0:73f8ad8f29bb 158 miliSeconds = seconds = minutes = 0;
jatinsha 0:73f8ad8f29bb 159 currentReferenceTime = previousReferenceTime = 0;
jatinsha 0:73f8ad8f29bb 160 calculatedTimeInterval = referenceTimeInterval = runningError = 0;
jatinsha 0:73f8ad8f29bb 161 lcd.locate(0,0);
jatinsha 0:73f8ad8f29bb 162 lcd.printf("00:00:00");
jatinsha 2:0d5060721258 163 //lcd.locate(0,1);
jatinsha 2:0d5060721258 164 //lcd.printf("000000");
jatinsha 0:73f8ad8f29bb 165 }
jatinsha 0:73f8ad8f29bb 166 }
jatinsha 0:73f8ad8f29bb 167 }
jatinsha 0:73f8ad8f29bb 168 }
jatinsha 0:73f8ad8f29bb 169
jatinsha 0:73f8ad8f29bb 170 int main()
jatinsha 0:73f8ad8f29bb 171 {
jatinsha 0:73f8ad8f29bb 172 // interrupt service routine
jatinsha 0:73f8ad8f29bb 173 pc.attach(&callback);
jatinsha 0:73f8ad8f29bb 174 // r0 r1 r2 r3 c0 c1 c2 c3
jatinsha 0:73f8ad8f29bb 175 Keypad keypad(p21, p22, p23, p24, p25, p26, p27, p28);
jatinsha 0:73f8ad8f29bb 176 keypad.attach(&cbAfterInput);
jatinsha 0:73f8ad8f29bb 177 keypad.start(); // energize the columns c0-c3 of the keypad
jatinsha 0:73f8ad8f29bb 178
jatinsha 0:73f8ad8f29bb 179 // local variable
jatinsha 0:73f8ad8f29bb 180 miliSeconds = seconds = minutes = 0;
jatinsha 0:73f8ad8f29bb 181 currentReferenceTime = previousReferenceTime = 0;
jatinsha 0:73f8ad8f29bb 182 calculatedTimeInterval = referenceTimeInterval = runningError = 0;
jatinsha 0:73f8ad8f29bb 183 //timerReference.start();
jatinsha 0:73f8ad8f29bb 184 //timerDisplay.start();
jatinsha 0:73f8ad8f29bb 185
jatinsha 0:73f8ad8f29bb 186 // Initial display work
jatinsha 0:73f8ad8f29bb 187 lcd.locate(0,0);
jatinsha 0:73f8ad8f29bb 188 lcd.printf("00:00:00");
jatinsha 0:73f8ad8f29bb 189
jatinsha 0:73f8ad8f29bb 190 while(1)
jatinsha 0:73f8ad8f29bb 191 {
jatinsha 0:73f8ad8f29bb 192 if( timerDisplay.read_ms() >= interval)
jatinsha 0:73f8ad8f29bb 193 {
jatinsha 0:73f8ad8f29bb 194 timerDisplay.reset();
jatinsha 0:73f8ad8f29bb 195
jatinsha 0:73f8ad8f29bb 196 // Calculate display variables
jatinsha 0:73f8ad8f29bb 197 miliSeconds += interval;
jatinsha 0:73f8ad8f29bb 198
jatinsha 0:73f8ad8f29bb 199 centiSeconds += (miliSeconds / 10);
jatinsha 0:73f8ad8f29bb 200 miliSeconds %= 10;
jatinsha 0:73f8ad8f29bb 201
jatinsha 0:73f8ad8f29bb 202 seconds += (centiSeconds / 100);
jatinsha 0:73f8ad8f29bb 203 centiSeconds %= 100;
jatinsha 0:73f8ad8f29bb 204
jatinsha 0:73f8ad8f29bb 205 minutes += (seconds / 60);
jatinsha 0:73f8ad8f29bb 206 seconds %= 60;
jatinsha 0:73f8ad8f29bb 207
jatinsha 0:73f8ad8f29bb 208 // Error correction using referenceClock
jatinsha 0:73f8ad8f29bb 209 currentReferenceTime = timerReference.read_us();
jatinsha 0:73f8ad8f29bb 210 referenceTimeInterval = currentReferenceTime - previousReferenceTime;
jatinsha 0:73f8ad8f29bb 211 runningError += (referenceTimeInterval - interval*1000);
jatinsha 0:73f8ad8f29bb 212
jatinsha 0:73f8ad8f29bb 213 //miliSeconds += runningError;
jatinsha 0:73f8ad8f29bb 214 previousReferenceTime = currentReferenceTime;
jatinsha 0:73f8ad8f29bb 215
jatinsha 0:73f8ad8f29bb 216
jatinsha 0:73f8ad8f29bb 217 // Display time on LCD
jatinsha 0:73f8ad8f29bb 218 lcd.locate(0,0);
jatinsha 0:73f8ad8f29bb 219 lcd.printf("%02d:%02d:%02d",minutes, seconds, centiSeconds);
jatinsha 0:73f8ad8f29bb 220
jatinsha 2:0d5060721258 221 //lcd.locate(0,1);
jatinsha 2:0d5060721258 222 //lcd.printf("%06d", runningError);
jatinsha 0:73f8ad8f29bb 223 }
jatinsha 0:73f8ad8f29bb 224
jatinsha 0:73f8ad8f29bb 225
jatinsha 0:73f8ad8f29bb 226
jatinsha 0:73f8ad8f29bb 227
jatinsha 0:73f8ad8f29bb 228
jatinsha 0:73f8ad8f29bb 229 }
jatinsha 0:73f8ad8f29bb 230
jatinsha 0:73f8ad8f29bb 231 }