Problematique

Dependencies:   mbed mbed-rtos

Revision:
0:dc7095ab4963
Child:
1:261bb31628e8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Feb 09 19:02:50 2014 +0000
@@ -0,0 +1,82 @@
+#include "mbed.h"
+
+Serial pc(USBTX, USBRX);
+
+//const bool preambule[8] = {false, true, false, true, false, true, false, true};
+//const bool start_end[8] = {false, true, true, true, true, true, true, false};
+
+bool Trame[16] = {false, true, false, true, true, true, true, false, false, false, true, false, false, true, true, true};
+
+void readTrame()
+{
+    pc.printf("Duree impulsion : %d microsecondes.\n\r", LPC_TIM2->CR1/(SystemCoreClock/1000000));
+
+    LPC_TIM2->IR = 0xFF;
+}
+
+void sendTrame(bool *trame)
+{
+    for (int a = 0; a < 16; a++)
+    {
+        if (trame[a])
+        {
+            LPC_PWM1->MR1 = 1;
+            LPC_PWM1->MR2 = SystemCoreClock/2;
+        }
+        else
+        {
+            LPC_PWM1->MR1 = SystemCoreClock/2;
+            LPC_PWM1->MR2 = 1;
+        }
+        
+        pc.printf("trame: %d\n\r", trame[a]);
+        
+        while(LPC_PWM1->IR != 0x01);
+        
+        LPC_PWM1->IR = 0xFF;
+    }
+}
+
+void initialize()
+{
+    // Set system control
+    LPC_SC->PCONP |= (1 << 22) | (1 << 6); // Enable Timer2 et PWM
+    LPC_SC->PCLKSEL0 |= (1 << 12); // PClk PWM = CCLK
+    LPC_SC->PCLKSEL1 |= (1 << 12); // PClk Timer2 = CCLK
+
+    // Set pin connection
+    LPC_PINCON->PINSEL0 |= (3 << 10); // Pin 29 Capture
+    LPC_PINCON->PINSEL4 |= (1 << 2);  // Pin 25 PWM
+
+    //Initialize Timer2 for capture
+    NVIC_SetVector(TIMER2_IRQn, uint32_t(readTrame));
+
+    LPC_TIM2->TC = 0;           // Initialize Time Counter
+    LPC_TIM2->PC = 0;           // Initialize Prescale Counter
+    LPC_TIM2->PR = 0;           // Initialize Prescale Register
+    LPC_TIM2->TCR |= (1 << 1);  // Reset Timer Control Register
+    LPC_TIM2->IR = 0xFF;        // Reset Interrupt Register
+    LPC_TIM2->CCR |= (1 << 5) | (1 << 4) | (1 << 3);      // Initialize Capture Control Register
+    LPC_TIM2->CTCR |= (1 << 2) | (1 << 0);                // TC is incremented on rising edge
+
+    LPC_TIM2->TCR = 0x01;       // Start Timer Control Register
+    
+    //Initialize PWM
+    LPC_PWM1->MCR |= (1 << 1) | (1 << 0);   // Initialize Match Control Register Interrupt/Reset
+    LPC_PWM1->PCR |= (1 << 10) | (1 << 2);  // Initialize PWM Control Register Output/Double-edge
+    
+    LPC_PWM1->MR0 = SystemCoreClock;        // Period
+    LPC_PWM1->LER |= (1 << 2) | (1 << 1);   // Initialize Latch Enable Register
+    LPC_PWM1->TCR |= (1 << 0);              // Enable counter
+    
+    LPC_PWM1->IR = 0xFF; // Reset Interrupt Register
+}
+
+int main()
+{
+    initialize();
+    
+    sendTrame(Trame);
+    
+    while(true);
+}