APP 4

Dependencies:   mbed CRC16 mbed-rtos

Revision:
13:195826b8c61b
Parent:
11:097ae746d8ac
Child:
14:9505b98c6623
--- a/APP.cpp	Sun Feb 21 21:47:13 2016 +0000
+++ b/APP.cpp	Mon Feb 22 17:52:26 2016 +0000
@@ -3,11 +3,15 @@
 #include "Frame.h"
 
 Serial pc(USBTX, USBRX);
-DigitalIn in(p9);
+DigitalOut out(p8);
+DigitalIn in(p30);
 
 bool clockTick = false;
-bitset<FRAMESIZE> message;
+bitset<FRAMESIZE> message (string("1010101010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"));
 int counter = 0;
+unsigned int period = 0;
+bool readBuffer[2] = {false, false};
+bool readBufferReady = false;
 
 int benchmark(void (*function) (void))
 {
@@ -16,13 +20,13 @@
     return LPC_TIM2->TC - count;
 }
 
-extern "C" void TIMER2_IRQHandler()
+extern "C" void TIMER1_IRQHandler()
 {
-    if ((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
+    if ((LPC_TIM1->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
     {
-        LPC_TIM2->IR |= 1 << 0;        // Clear MR0 interrupt flag
+        LPC_TIM1->IR |= 1 << 0;        // Clear MR0 interrupt flag
         clockTick = !clockTick;
-        LPC_TIM2->EMR = encode(message[counter] & 0x1, clockTick);
+        out = encode(message[counter] & 0x1, clockTick);
 
         if (clockTick)
         {
@@ -35,61 +39,66 @@
         }
     }
 }
-/*
-extern "C" void TIMER1_IRQHandler()
+
+extern "C" void TIMER2_IRQHandler()
 {
-    if ((LPC_TIM2->IR & 0x01) == 0x01) // if MR0 interrupt, proceed
-    {
-        LPC_TIM2->IR |= 1 << 0;        // Clear MR0 interrupt flag
-        clockTick = !clockTick;
-        LPC_TIM2->EMR = encode(message[counter], clockTick);
+    unsigned int clocks = LPC_TIM2->CR0;
+    period = clocks;    // preambule 01010101
 
-        if (clockTick)
-        {
-            counter++;
-        }
+    if (!readBufferReady)
+    {
+        readBuffer[0] = in.read();
+    }
+    else
+    {
+        readBuffer[1] = in.read();
+        // Appel MEF
+        bool value = decode(readBuffer[0], readBuffer[1]);
+    }
 
-        if (counter >= FRAMESIZE)
-        {
-            counter = 0;
-        }
-    }
-}*/
+    LPC_TIM2->TC = 0;
 
- 
+    LPC_TIM2->IR |= 0xFFFFFFFF;     // clear Timer interrupt register
+}
+
 void initTimers()
 {
-    //Timer 2 (match)
-    LPC_SC->PCLKSEL1 |= (1 << 12);          // pclk = cclk timer2
-    LPC_SC->PCONP |= (1 << 22);             // timer2 power on
-    LPC_TIM2->MR0 = CLOCKS_TO_SECOND / 10;  // 100 ms
-    LPC_TIM2->MCR = 3;                      // interrupt and reset control
-                                            // Interrupt & reset timer2 on match
-    LPC_TIM2->EMR = (2 << 4);               // toggle
-    NVIC_EnableIRQ(TIMER2_IRQn);            // enable timer2 interrupt
-    LPC_TIM2->TCR = 1;                      // enable Timer2
-    /*
-    //Timer 1 (cap)
-    LPC_SC->PCLKSEL0 |= (1 << 4);           // pclk = cclk timer2
+    //Timer 1 (match)
+    LPC_SC->PCLKSEL0 |= (1 << 4);           // pclk = cclk timer1
     LPC_SC->PCONP |= (1 << 2);              // timer1 power on
     LPC_TIM1->MR0 = CLOCKS_TO_SECOND / 10;  // 100 ms
     LPC_TIM1->MCR = 3;                      // interrupt and reset control
-                                            // Interrupt & reset timer1 on match
-    NVIC_EnableIRQ(TIMER1_IRQn);            // enable timer1 interrupt        
-    LPC_TIM1->TCR = 1;                      // enable Timer1
-    */
+                                            // Interrupt & reset timer on match
+    LPC_TIM1->EMR = (3 << 4);
+    NVIC_EnableIRQ(TIMER1_IRQn);            // enable timer interrupt
+    LPC_TIM1->TCR = 1;                      // enable Timer
+
+    //Timer 2 (cap)
+    LPC_SC->PCLKSEL1 |= (1 << 12);          // pclk = cclk timer2
+    LPC_SC->PCONP |= (1 << 22);             // timer2 power on
+    LPC_TIM2->TC = 0;                       // clear timer counter
+    LPC_TIM2->PC = 0;                       // clear prescale counter
+    LPC_TIM2->PR = 0;                       // clear prescale register
+    LPC_TIM2->TCR |= (1 << 1);              // reset timer
+    LPC_TIM2->TCR &= ~(1 << 1);             // release reset
+    LPC_TIM2->IR = 0xFFFFFFFF;              // clear interrupt register
+    LPC_TIM2->CCR |= 0x0000007;             // enable rising-edge and falling-edge capture on 2.0
+    NVIC_EnableIRQ(TIMER2_IRQn);            // enable timer interrupt
+    LPC_TIM2->TCR = 1;                      // start Timer
 }
 
 int main()
 {
-    message = buildFrame(convertToBits("ASDF", 4, &pc), 4, &pc);
+    //message = buildFrame(convertToBits("ASDF", 4, &pc), 4, &pc);
 
-    LPC_PINCON->PINSEL0 |= (3 << 12);  // P0.6 = MAT2.0
+    LPC_PINCON->PINSEL0 |= (3 << 8);   // P0.4 = CAP2.0
     initTimers();
 
     while (true)
     {
-        pc.printf("%i ", decode(in.read(), clockTick));
-        wait(0.2);
+        pc.printf("%i ", period);
+        pc.printf("%i \r\n", in.read());
+        //pc.printf("%i ", decode(in.read(), clockTick));
+        wait(0.1);
     }
 }