![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
For use in my acoustic touchscreen project, found here: http://hackaday.io/project/1990-Low-Cost-Touchscreen-Anywhere
main.cpp@4:99bd88e2b2e7, 2014-10-31 (annotated)
- Committer:
- ThatcherC
- Date:
- Fri Oct 31 19:13:12 2014 +0000
- Revision:
- 4:99bd88e2b2e7
- Parent:
- 3:5d221e8f4012
Added more comments;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ThatcherC | 0:d678323f9a72 | 1 | #include "mbed.h" |
ThatcherC | 0:d678323f9a72 | 2 | |
ThatcherC | 2:2ccbee38f4a8 | 3 | Serial pc(dp16,dp15); |
ThatcherC | 0:d678323f9a72 | 4 | DigitalOut myled(dp1); |
ThatcherC | 0:d678323f9a72 | 5 | DigitalOut led2(dp4); |
ThatcherC | 0:d678323f9a72 | 6 | InterruptIn int1(dp14); |
ThatcherC | 0:d678323f9a72 | 7 | InterruptIn int2(dp13); |
ThatcherC | 0:d678323f9a72 | 8 | Timer t; |
ThatcherC | 0:d678323f9a72 | 9 | Timeout reset; |
ThatcherC | 0:d678323f9a72 | 10 | bool first = true; |
ThatcherC | 0:d678323f9a72 | 11 | bool timeAvailable = false; |
ThatcherC | 0:d678323f9a72 | 12 | |
ThatcherC | 0:d678323f9a72 | 13 | void reset1(); |
ThatcherC | 0:d678323f9a72 | 14 | void reset2(); |
ThatcherC | 0:d678323f9a72 | 15 | |
ThatcherC | 0:d678323f9a72 | 16 | float timeArray[3]; //important. this stores the delays between vibration detections |
ThatcherC | 0:d678323f9a72 | 17 | //right now only indices 0 and 1 are used |
ThatcherC | 3:5d221e8f4012 | 18 | |
ThatcherC | 3:5d221e8f4012 | 19 | |
ThatcherC | 3:5d221e8f4012 | 20 | //Code from: http://mbed.org/users/ThatcherC/code/WatchDogTest/ |
ThatcherC | 3:5d221e8f4012 | 21 | class Watchdog { |
ThatcherC | 3:5d221e8f4012 | 22 | public: |
ThatcherC | 3:5d221e8f4012 | 23 | // Load timeout value in watchdog timer and enable |
ThatcherC | 3:5d221e8f4012 | 24 | void kick(float s) { |
ThatcherC | 3:5d221e8f4012 | 25 | LPC_SYSCON->SYSAHBCLKCTRL = LPC_SYSCON->SYSAHBCLKCTRL|(1<<15); |
ThatcherC | 3:5d221e8f4012 | 26 | LPC_SYSCON->WDTCLKSEL = 0x1; // Set CLK src to Main Clock |
ThatcherC | 3:5d221e8f4012 | 27 | LPC_SYSCON->WDTCLKUEN = 0x01; /* Update clock */ |
ThatcherC | 3:5d221e8f4012 | 28 | LPC_SYSCON->WDTCLKUEN = 0x00; /* Toggle update register once */ |
ThatcherC | 3:5d221e8f4012 | 29 | LPC_SYSCON->WDTCLKUEN = 0x01; |
ThatcherC | 3:5d221e8f4012 | 30 | LPC_SYSCON->WDTCLKDIV = 0x10; |
ThatcherC | 3:5d221e8f4012 | 31 | uint32_t clk = SystemCoreClock/16; // WD has a fixed /4 prescaler, PCLK default is /4 |
ThatcherC | 3:5d221e8f4012 | 32 | LPC_WDT->TC = s * (float)clk; |
ThatcherC | 3:5d221e8f4012 | 33 | LPC_WDT->MOD = 0x3; // Enabled and Reset |
ThatcherC | 3:5d221e8f4012 | 34 | kick(); |
ThatcherC | 3:5d221e8f4012 | 35 | } |
ThatcherC | 3:5d221e8f4012 | 36 | // "kick" or "feed" the dog - reset the watchdog timer |
ThatcherC | 3:5d221e8f4012 | 37 | // by writing this required bit pattern |
ThatcherC | 3:5d221e8f4012 | 38 | void kick() { |
ThatcherC | 3:5d221e8f4012 | 39 | LPC_WDT->FEED = 0xAA; |
ThatcherC | 3:5d221e8f4012 | 40 | LPC_WDT->FEED = 0x55; |
ThatcherC | 3:5d221e8f4012 | 41 | } |
ThatcherC | 3:5d221e8f4012 | 42 | }; |
ThatcherC | 3:5d221e8f4012 | 43 | |
ThatcherC | 4:99bd88e2b2e7 | 44 | Watchdog w; //used to reset the microcontroller when/if it halts |
ThatcherC | 3:5d221e8f4012 | 45 | |
ThatcherC | 0:d678323f9a72 | 46 | void flip1(){ |
ThatcherC | 0:d678323f9a72 | 47 | if(first){ |
ThatcherC | 0:d678323f9a72 | 48 | t.start(); |
ThatcherC | 0:d678323f9a72 | 49 | int1.fall(NULL); |
ThatcherC | 0:d678323f9a72 | 50 | first = false; |
ThatcherC | 0:d678323f9a72 | 51 | reset.attach(&reset1,0.01); |
ThatcherC | 0:d678323f9a72 | 52 | }else{ |
ThatcherC | 0:d678323f9a72 | 53 | t.stop(); |
ThatcherC | 0:d678323f9a72 | 54 | timeArray[0]=t.read(); |
ThatcherC | 0:d678323f9a72 | 55 | reset.detach(); |
ThatcherC | 0:d678323f9a72 | 56 | timeAvailable=true; |
ThatcherC | 0:d678323f9a72 | 57 | first = true; |
ThatcherC | 0:d678323f9a72 | 58 | } |
ThatcherC | 0:d678323f9a72 | 59 | myled = 1; |
ThatcherC | 0:d678323f9a72 | 60 | } |
ThatcherC | 0:d678323f9a72 | 61 | |
ThatcherC | 0:d678323f9a72 | 62 | void flip2(){ |
ThatcherC | 0:d678323f9a72 | 63 | if(first){ |
ThatcherC | 0:d678323f9a72 | 64 | t.start(); |
ThatcherC | 0:d678323f9a72 | 65 | int2.fall(NULL); |
ThatcherC | 0:d678323f9a72 | 66 | first = false; |
ThatcherC | 0:d678323f9a72 | 67 | reset.attach(&reset2,0.01); |
ThatcherC | 0:d678323f9a72 | 68 | }else{ |
ThatcherC | 0:d678323f9a72 | 69 | t.stop(); |
ThatcherC | 0:d678323f9a72 | 70 | timeArray[1]=t.read(); |
ThatcherC | 0:d678323f9a72 | 71 | reset.detach(); |
ThatcherC | 0:d678323f9a72 | 72 | timeAvailable=true; |
ThatcherC | 0:d678323f9a72 | 73 | first = true; |
ThatcherC | 0:d678323f9a72 | 74 | } |
ThatcherC | 0:d678323f9a72 | 75 | led2 = 1; |
ThatcherC | 0:d678323f9a72 | 76 | } |
ThatcherC | 0:d678323f9a72 | 77 | |
ThatcherC | 4:99bd88e2b2e7 | 78 | void reset1(){ //used to reset interrupt 1 if too much time passes |
ThatcherC | 0:d678323f9a72 | 79 | t.stop(); |
ThatcherC | 0:d678323f9a72 | 80 | t.reset(); |
ThatcherC | 0:d678323f9a72 | 81 | first = true; |
ThatcherC | 0:d678323f9a72 | 82 | int1.fall(&flip1); |
ThatcherC | 0:d678323f9a72 | 83 | } |
ThatcherC | 0:d678323f9a72 | 84 | |
ThatcherC | 4:99bd88e2b2e7 | 85 | void reset2(){ //used to reset interrupt 1 if too much time passes |
ThatcherC | 0:d678323f9a72 | 86 | t.stop(); |
ThatcherC | 0:d678323f9a72 | 87 | t.reset(); |
ThatcherC | 0:d678323f9a72 | 88 | first = true; |
ThatcherC | 0:d678323f9a72 | 89 | int2.fall(&flip2); |
ThatcherC | 0:d678323f9a72 | 90 | } |
ThatcherC | 0:d678323f9a72 | 91 | |
ThatcherC | 0:d678323f9a72 | 92 | int main() { |
ThatcherC | 2:2ccbee38f4a8 | 93 | pc.baud(115200); |
ThatcherC | 2:2ccbee38f4a8 | 94 | pc.printf("Ready\n"); |
ThatcherC | 3:5d221e8f4012 | 95 | w.kick(.1); |
ThatcherC | 0:d678323f9a72 | 96 | int1.fall(&flip1); |
ThatcherC | 0:d678323f9a72 | 97 | int2.fall(&flip2); |
ThatcherC | 0:d678323f9a72 | 98 | timeArray[0] = 0; |
ThatcherC | 0:d678323f9a72 | 99 | timeArray[1] = 0; |
ThatcherC | 0:d678323f9a72 | 100 | timeArray[2] = 0; //for now this isn't used (only two sensors) |
ThatcherC | 0:d678323f9a72 | 101 | while(1){ |
ThatcherC | 0:d678323f9a72 | 102 | if(timeAvailable){ |
ThatcherC | 2:2ccbee38f4a8 | 103 | int tA0 = int(timeArray[0]*1000000); |
ThatcherC | 2:2ccbee38f4a8 | 104 | int tA1 = int(timeArray[1]*1000000); |
ThatcherC | 3:5d221e8f4012 | 105 | pc.printf("%i\t",tA0-tA1); |
ThatcherC | 0:d678323f9a72 | 106 | t.reset(); |
ThatcherC | 0:d678323f9a72 | 107 | timeArray[0] = 0; |
ThatcherC | 0:d678323f9a72 | 108 | timeArray[1] = 0; |
ThatcherC | 0:d678323f9a72 | 109 | timeArray[2] = 0; //for now this isn't used (only two sensors) |
ThatcherC | 0:d678323f9a72 | 110 | timeAvailable=false; |
ThatcherC | 0:d678323f9a72 | 111 | wait(.1); |
ThatcherC | 0:d678323f9a72 | 112 | int1.fall(&flip1); |
ThatcherC | 0:d678323f9a72 | 113 | int2.fall(&flip2); |
ThatcherC | 0:d678323f9a72 | 114 | } |
ThatcherC | 3:5d221e8f4012 | 115 | w.kick(); |
ThatcherC | 0:d678323f9a72 | 116 | } |
ThatcherC | 0:d678323f9a72 | 117 | } |