For use in my acoustic touchscreen project, found here: http://hackaday.io/project/1990-Low-Cost-Touchscreen-Anywhere

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 Serial pc(dp16,dp15);
00004 DigitalOut myled(dp1);
00005 DigitalOut led2(dp4);
00006 InterruptIn int1(dp14);
00007 InterruptIn int2(dp13);
00008 Timer t;
00009 Timeout reset;
00010 bool first = true;
00011 bool timeAvailable = false;
00012 
00013 void reset1();
00014 void reset2();
00015 
00016 float timeArray[3];  //important. this stores the delays between vibration detections
00017                    //right now only indices 0 and 1 are used
00018 
00019 
00020 //Code from: http://mbed.org/users/ThatcherC/code/WatchDogTest/
00021 class Watchdog {
00022 public:
00023 // Load timeout value in watchdog timer and enable
00024     void kick(float s) {
00025         LPC_SYSCON->SYSAHBCLKCTRL = LPC_SYSCON->SYSAHBCLKCTRL|(1<<15);
00026         LPC_SYSCON->WDTCLKSEL = 0x1;                // Set CLK src to Main Clock
00027         LPC_SYSCON->WDTCLKUEN = 0x01;       /* Update clock */
00028         LPC_SYSCON->WDTCLKUEN = 0x00;       /* Toggle update register once */
00029         LPC_SYSCON->WDTCLKUEN = 0x01;
00030         LPC_SYSCON->WDTCLKDIV = 0x10;
00031         uint32_t clk = SystemCoreClock/16;    // WD has a fixed /4 prescaler, PCLK default is /4
00032         LPC_WDT->TC = s * (float)clk;
00033         LPC_WDT->MOD = 0x3;                   // Enabled and Reset
00034         kick();
00035     }
00036 // "kick" or "feed" the dog - reset the watchdog timer
00037 // by writing this required bit pattern
00038     void kick() {
00039         LPC_WDT->FEED = 0xAA;
00040         LPC_WDT->FEED = 0x55;
00041     }
00042 };
00043 
00044 Watchdog w;     //used to reset the microcontroller when/if it halts
00045                    
00046 void flip1(){
00047     if(first){
00048         t.start();
00049         int1.fall(NULL);
00050         first = false;
00051         reset.attach(&reset1,0.01);
00052     }else{
00053         t.stop();
00054         timeArray[0]=t.read();
00055         reset.detach();
00056         timeAvailable=true;
00057         first = true;
00058     }
00059     myled = 1;
00060 }
00061 
00062 void flip2(){
00063     if(first){
00064         t.start();
00065         int2.fall(NULL);
00066         first = false;
00067         reset.attach(&reset2,0.01);
00068     }else{
00069         t.stop();
00070         timeArray[1]=t.read();
00071         reset.detach();
00072         timeAvailable=true;
00073         first = true;
00074     }
00075     led2 = 1;
00076 }
00077 
00078 void reset1(){              //used to reset interrupt 1 if too much time passes
00079     t.stop();
00080     t.reset();
00081     first = true;
00082     int1.fall(&flip1);
00083 }
00084 
00085 void reset2(){              //used to reset interrupt 1 if too much time passes
00086     t.stop();
00087     t.reset();
00088     first = true;
00089     int2.fall(&flip2);
00090 }
00091 
00092 int main() {
00093     pc.baud(115200);
00094     pc.printf("Ready\n");
00095     w.kick(.1);
00096     int1.fall(&flip1);
00097     int2.fall(&flip2);
00098     timeArray[0] = 0;
00099     timeArray[1] = 0;
00100     timeArray[2] = 0;   //for now this isn't used (only two sensors)
00101     while(1){
00102         if(timeAvailable){
00103             int tA0 = int(timeArray[0]*1000000);
00104             int tA1 = int(timeArray[1]*1000000);
00105             pc.printf("%i\t",tA0-tA1);
00106             t.reset();
00107             timeArray[0] = 0;
00108             timeArray[1] = 0;
00109             timeArray[2] = 0;   //for now this isn't used (only two sensors)
00110             timeAvailable=false;
00111             wait(.1);
00112             int1.fall(&flip1);
00113             int2.fall(&flip2);
00114         }
00115         w.kick();
00116     }
00117 }