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

Dependencies:   EthernetNetIf HTTPServer QEI_hw RPCInterface mbed

setup.cpp

Committer:
Yo_Robot
Date:
2012-04-01
Revision:
6:b4dae934e1ea
Parent:
5:c5aea1eb10bb
Child:
7:d9aca501126f

File content as of revision 6:b4dae934e1ea:

/**
 * @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"

// Salida Serial de mbed
extern Serial pc;
extern DigitalOut pin_son;   // SON
extern DigitalOut pin_dir;   // SIGN+
extern DigitalIn  pin_alm;   // ALM 
extern AnalogOut  aout;      // +-10V



void setTimer2()
{
    // Encender Timer2 (PCONP[22])
    LPC_SC->PCONP |= 1 << 22; 

    // Resetear y parar el Timer
    LPC_TIM2->TCR  = 0x2; 
    LPC_TIM2->CTCR = 0x0; 

    // Establecer el Preescaler en cero
    // SIn Preesclaer
    LPC_TIM2->PR = 0;

    // Calcular el periodo Inicial
    uint32_t periodo = ( SystemCoreClock / 400 ); 

    // Establecer los Registros de Coincidencia
    // ( Match Register )
    LPC_TIM2->MR2 = periodo;
    LPC_TIM2->MR3 = periodo;  // Legacy, salidas identicas
    
    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
    
}


void setSON( int onOff )
{
    pin_son = onOff;
}


void setDir( int dir )
{
    pin_dir = dir;
}


void setAout( float value )
{
    aout = value;
}


void ISR_Alarm()
{
    pc.printf( "\n\n ERROR: ALARMA \n\n " );
    
    setSON( 0 );
    stopTimer2();
    setAout( 0.5 );
    
}


void ISR_Serial()
{
    int freq;        //Frecuencia del PTO
    char command;    //Comando a ejecutar
    char scale;      //Escala de la frecuencia

    pc.scanf( "%c%d%c", &command, &freq, &scale ) ;
    LPC_UART0->FCR = 0x06;
    pc.printf("\n %c%d%c \n", command, freq, scale );

    // 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( );
    
}

int getMRvalue( int fout  )
{
    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; // Las dos salidas son identicas
    LPC_TIM2->MR3 = newValue; // Para testear el programa.
}



void startTimer2()
{
    // Arrancer el Timer 2
    LPC_TIM2->TCR = 1;
}

void stopTimer2()
{
    // Detener el Timer 2
    LPC_TIM2->TCR = 0x2;
}





/*  LEGACY FUNCTIONS
 *
 *  El codigo actual no hace referencia a estas funciones
 *  sin embargo no hay problema en definirlas.
 */ 
void setMR3( int newValue )
{
    LPC_TIM2->MR3 = newValue;
}


void setPrescaler( int newValue)
{
    LPC_TIM2->PR = newValue; 
}