led example with 2 timers

Dependencies:   mbed mbed-rtos

Revision:
5:726d7857fd33
Parent:
4:af325c921e79
--- a/sender.cpp	Tue Feb 11 20:38:43 2014 +0000
+++ b/sender.cpp	Wed Feb 12 07:07:48 2014 +0000
@@ -3,61 +3,84 @@
 extern Serial pc;
 extern Mail<message_t, 16> mailBox;
 DigitalOut myled(LED1);
-DigitalOut M0(p26);
-volatile unsigned short timer_count;
-unsigned int trameSize = 0; 
-unsigned int trameCounter = 0;
+unsigned int trameSize; 
+unsigned int trameCounter;
 char trameArray[696];
 bool trameReady;
 
-
 void Sender_init(void)
 {
     myled = 0;
-    trameCounter = 0;
+    trameCounter = 1;
+    trameSize = 0;
     trameReady = false;
     
-    LPC_SC->PCONP |=1<1;            //timer0 power on
-    LPC_TIM0->MR0 = 2398000;        //100 msec ??
-    LPC_TIM0->MCR = 3;              //interrupt and reset control
-                                    //3 = Interrupt & reset timer0 on match
-                                    //1 = Interrupt only, no reset of timer0
-    NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
-    LPC_TIM0->TCR = 1;              //enable Timer0
+    // Enable the pins on the device to use TIMER MAT3.0
+    LPC_PINCON->PINSEL0 |= 3<<20;
+    
+    NVIC_SetVector(TIMER3_IRQn, uint32_t(TIMER3_IRQHandler));
+    
+    LPC_SC->PCONP |=1<<23;            //timer3 power on
+    LPC_TIM3->MR0 = TIME;           //100 msec ??
+    LPC_TIM3->MCR |= 3;              //interrupt and reset control
+                                    //3 = Interrupt & reset timer3 on match
+                                    //1 = Interrupt only, no reset of timer3
+    LPC_TIM3->TC = 0;                   // clear timer counter
+    LPC_TIM3->PC = 0;                   // clear prescale counter
+    LPC_TIM3->PR = 0;                   // clear prescale register
+    LPC_TIM3->TCR = (1 << 1);               //reset Timer3
+    LPC_TIM3->IR |= 1 << 0;         // Clear MR0 interrupt flag
+    LPC_TIM3->EMR |= (3 << 4);        // enable MAT 3.0
+    
+    
+    NVIC_EnableIRQ(TIMER3_IRQn);    //enable timer3 interrupt
+   
 }
 
-extern "C" void TIMER0_IRQHandler (void)
+extern "C" void TIMER3_IRQHandler (void)
 {    
-    if((LPC_TIM0->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
-    {
-        LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
+    char current = 0;
+    char previous = 0;
     
-        
-        if(trameReady == true)
-        {
-            if(trameCounter <= trameSize)
-            {
-                if(trameArray[trameCounter] == 0x30)
-                {
-                    M0 = 0;    
-                }    
+    if((LPC_TIM3->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
+     {
+        LPC_TIM3->IR |= 1 << 0;         // Clear MR0 interrupt flag
+         
+        myled =!myled;
+  
+        if(trameReady == true) 
+        {        
+                      
+                if(trameCounter <= trameSize)
+                {   
+                    current = trameArray[trameCounter];
+                    previous = trameArray[trameCounter-1];
+                    
+                    if(current == previous)
+                    {
+                        LPC_TIM3->MR0 = (TIME/2);
+                    } 
+                    else
+                    {
+                        LPC_TIM3->MR0 = (TIME);
+                    }
+    
+                    // next caracter
+                    trameCounter++;
+                    
+                    // Activity light
+                    myled =!myled;
+                }
                 else
                 {
-                    M0 = 1;    
+                    // Trame send, wait until a new trame is ready to send
+                    trameReady = false;
+                    LPC_TIM3->TCR = 0;
+                    trameCounter = 1;  
+                    myled= 0;   
                 }
-                myled =!myled;
-                trameCounter++;
-                
             }
-            else
-            {
-                trameReady = false;
-                trameCounter = 0;  
-                M0 =0;
-                myled= 0;   
-            }
-        }   
-    }
+        }
 }
 
 void Sender_thread(void const *args)
@@ -79,8 +102,9 @@
                 
                 pc.printf("\n\r%s", trameArray);
                 pc.printf("\n\r%d", trameSize);
-                
-                trameReady = true;      
+                    
+                trameReady = true;  
+                LPC_TIM3->TCR = 1; 
             } 
         }
     }