Proyecto de Tesis en Mecatrónica. Universidad Técnica del Norte. Ernesto Palacios <mecatronica.mid@gmail.com>

Dependencies:   EthernetNetIf HTTPServer QEI_hw RPCInterface mbed

Revision:
4:552beeda4722
Parent:
3:8d5a9e3cd680
Child:
5:c5aea1eb10bb
--- a/setup.cpp	Sat Mar 24 17:37:20 2012 +0000
+++ b/setup.cpp	Wed Mar 28 18:42:32 2012 +0000
@@ -1,23 +1,23 @@
-/// Codigo Fuente para configurar al 
+/**
+ * @brief  Tren de impulsos con Timer2
+ * @file   setup.cpp
+ * @author Ernesto Palacios
+ *
+ * Created on 25 de Marzo de 2012
+ *
+ * Licencia  GPL v3.0
+ * http://www.gnu.org/licenses/gpl-3.0.html
+ */
+
 
 #include "setup.h"
 #include "mbed.h"
 
-
-// Esta variable global determina
-// los incrementos del Preescaler
-extern uint32_t PRESCALER_STEP; 
-
 // Salida Serial de mbed
 extern Serial pc;
 
 
-/** @brief: Esta función configura al Timer2
- *  para que las salidas p5 y p6 del mbed
- *  se alternen cada vez que se iguala al
- *  registro MR2 y MR3.
- */
-void Setup_PTO_Timer2()
+void setTimer2()
 {
     // Encender Timer2 (PCONP[22])
     LPC_SC->PCONP |= 1 << 22; 
@@ -26,28 +26,26 @@
     LPC_TIM2->TCR  = 0x2; 
     LPC_TIM2->CTCR = 0x0; 
 
-    // Establecer el Preescaler inicial 0.5 seg
-    LPC_TIM2->PR = 100;
+    // Establecer el Preescaler en cero
+    // SIn Preesclaer
+    LPC_TIM2->PR = 0;
 
-    // Calcular el periodo 
-    uint32_t periodo = ( SystemCoreClock / 4000 ); 
+    // Calcular el periodo Inicial
+    uint32_t periodo = ( SystemCoreClock / 400 ); 
 
     // Establecer los Registros de Coincidencia
     // ( Match Register )
     LPC_TIM2->MR2 = periodo;
-    LPC_TIM2->MR3 = periodo * 2;
+    LPC_TIM2->MR3 = periodo;  // Legacy, salidas identicas
     
-    LPC_TIM2->MCR |= 1 << 10;    // Resetear Timer en MR3
+    LPC_TIM2->MCR |= 1 << 7;    // Resetear Timer en MR2
     
     LPC_TIM2->EMR |= 15UL << 8;  // Alternar Pin MAT2.2 
                                 //      y MAT2.3
 
     LPC_PINCON->PINSEL0 |= 15UL << 16;  //Activar  MAT2.2 
                                        // y MAT2.3 como salidas
-
-    // Arrancer el Timer 2
-    LPC_TIM2->TCR = 1;
-
+    
 }
 
 
@@ -56,46 +54,55 @@
  */
 void ISR_Serial()
 {
-    int newValue;
-    char command;
+    int freq;        //Frecuencia del PTO
+    char command;    //Comando a ejecutar
+    char scale;      //Escala de la frecuencia
 
-    pc.scanf( "%d-%c", &newValue, &command ) ;
-    pc.printf("\n %d-%c \n", newValue, command );
+    pc.scanf( "%c%d%c", &command, &freq, &scale ) ;
+    LPC_UART0->FCR = 0x06;
+    pc.printf("\n %c%d%c \n", command, freq, scale );
 
-    if( command == 'p')
-       setPrescaler( newValue );
-    else if( command == 'm')
-        setMR2( newValue );
-    else if( command == 'n')
-        setMR3( newValue );
-    else if( command == 'a')
-        startTimer2();
-    else if( command == 's')
-        stopTimer2();
-
-
+    // Establecer nueva frecuencia
+    if( command == 'F')
+    {
+       if( scale == 'H' )
+            setMR2( getMRvalue( freq ) );
+       else if( scale == 'K' )
+            setMR2( getMRvalue( freq * 1000 ) );
+    }
+    
+    // INICIAR Timer
+    else if( command == 'I')
+        startTimer2( );
+        
+    // PARAR Timer    
+    else if( command == 'P')
+        stopTimer2( );
+    
 }
 
-
-/** @brief: Esta Funcion cambia el preescaler
- *  directamente
- */
-void setPrescaler( int newValue)
+int getMRvalue( int fout  )
 {
-    LPC_TIM2->PR = newValue; 
+    float exact, error;
+    int   toRegister;
+    
+    exact = (24000000 /(fout*2) ) -1;
+    toRegister = exact;  // Valor redondeado;
+    error = exact - toRegister;
+       
+    pc.printf( "\n\n MR value: %d\n error: %f\n" ,toRegister ,error );
+    
+    return toRegister;
 }
 
 
 void setMR2( int newValue )
 {
-    LPC_TIM2->MR2 = newValue;
+    LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas
+    LPC_TIM2->MR3 = newValue; // Para testear el programa.
 }
 
 
-void setMR3( int newValue )
-{
-    LPC_TIM2->MR3 = newValue;
-}
 
 void startTimer2()
 {
@@ -105,6 +112,22 @@
 
 void stopTimer2()
 {
-    // Arrancer el Timer 2
+    // Detener el Timer 2
     LPC_TIM2->TCR = 0x2;
-}
\ No newline at end of file
+}
+
+/*  LEGACY FUNCTIONS
+ *
+ *  El código actual no hace referencia a estas funciones
+ *  sin embargo no hay daño en definirlas.
+ */ 
+void setMR3( int newValue )
+{
+    LPC_TIM2->MR3 = newValue;
+}
+
+
+void setPrescaler( int newValue)
+{
+    LPC_TIM2->PR = newValue; 
+}