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

Dependencies:   EthernetNetIf HTTPServer QEI_hw RPCInterface mbed

Committer:
Yo_Robot
Date:
Wed Mar 28 18:42:32 2012 +0000
Revision:
4:552beeda4722
Parent:
3:8d5a9e3cd680
Child:
5:c5aea1eb10bb
Generador de frecuencias OK!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yo_Robot 4:552beeda4722 1 /**
Yo_Robot 4:552beeda4722 2 * @brief Tren de impulsos con Timer2
Yo_Robot 4:552beeda4722 3 * @file setup.cpp
Yo_Robot 4:552beeda4722 4 * @author Ernesto Palacios
Yo_Robot 4:552beeda4722 5 *
Yo_Robot 4:552beeda4722 6 * Created on 25 de Marzo de 2012
Yo_Robot 4:552beeda4722 7 *
Yo_Robot 4:552beeda4722 8 * Licencia GPL v3.0
Yo_Robot 4:552beeda4722 9 * http://www.gnu.org/licenses/gpl-3.0.html
Yo_Robot 4:552beeda4722 10 */
Yo_Robot 4:552beeda4722 11
Yo_Robot 3:8d5a9e3cd680 12
Yo_Robot 3:8d5a9e3cd680 13 #include "setup.h"
Yo_Robot 3:8d5a9e3cd680 14 #include "mbed.h"
Yo_Robot 3:8d5a9e3cd680 15
Yo_Robot 3:8d5a9e3cd680 16 // Salida Serial de mbed
Yo_Robot 3:8d5a9e3cd680 17 extern Serial pc;
Yo_Robot 3:8d5a9e3cd680 18
Yo_Robot 3:8d5a9e3cd680 19
Yo_Robot 4:552beeda4722 20 void setTimer2()
Yo_Robot 3:8d5a9e3cd680 21 {
Yo_Robot 3:8d5a9e3cd680 22 // Encender Timer2 (PCONP[22])
Yo_Robot 3:8d5a9e3cd680 23 LPC_SC->PCONP |= 1 << 22;
Yo_Robot 3:8d5a9e3cd680 24
Yo_Robot 3:8d5a9e3cd680 25 // Resetear y parar el Timer
Yo_Robot 3:8d5a9e3cd680 26 LPC_TIM2->TCR = 0x2;
Yo_Robot 3:8d5a9e3cd680 27 LPC_TIM2->CTCR = 0x0;
Yo_Robot 3:8d5a9e3cd680 28
Yo_Robot 4:552beeda4722 29 // Establecer el Preescaler en cero
Yo_Robot 4:552beeda4722 30 // SIn Preesclaer
Yo_Robot 4:552beeda4722 31 LPC_TIM2->PR = 0;
Yo_Robot 3:8d5a9e3cd680 32
Yo_Robot 4:552beeda4722 33 // Calcular el periodo Inicial
Yo_Robot 4:552beeda4722 34 uint32_t periodo = ( SystemCoreClock / 400 );
Yo_Robot 3:8d5a9e3cd680 35
Yo_Robot 3:8d5a9e3cd680 36 // Establecer los Registros de Coincidencia
Yo_Robot 3:8d5a9e3cd680 37 // ( Match Register )
Yo_Robot 3:8d5a9e3cd680 38 LPC_TIM2->MR2 = periodo;
Yo_Robot 4:552beeda4722 39 LPC_TIM2->MR3 = periodo; // Legacy, salidas identicas
Yo_Robot 3:8d5a9e3cd680 40
Yo_Robot 4:552beeda4722 41 LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2
Yo_Robot 3:8d5a9e3cd680 42
Yo_Robot 3:8d5a9e3cd680 43 LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2
Yo_Robot 3:8d5a9e3cd680 44 // y MAT2.3
Yo_Robot 3:8d5a9e3cd680 45
Yo_Robot 3:8d5a9e3cd680 46 LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2
Yo_Robot 3:8d5a9e3cd680 47 // y MAT2.3 como salidas
Yo_Robot 4:552beeda4722 48
Yo_Robot 3:8d5a9e3cd680 49 }
Yo_Robot 3:8d5a9e3cd680 50
Yo_Robot 3:8d5a9e3cd680 51
Yo_Robot 3:8d5a9e3cd680 52 /** @brief: Esta es la rutina que maneja las interrupciones
Yo_Robot 3:8d5a9e3cd680 53 * seriales, al recibir un caracter.
Yo_Robot 3:8d5a9e3cd680 54 */
Yo_Robot 3:8d5a9e3cd680 55 void ISR_Serial()
Yo_Robot 3:8d5a9e3cd680 56 {
Yo_Robot 4:552beeda4722 57 int freq; //Frecuencia del PTO
Yo_Robot 4:552beeda4722 58 char command; //Comando a ejecutar
Yo_Robot 4:552beeda4722 59 char scale; //Escala de la frecuencia
Yo_Robot 3:8d5a9e3cd680 60
Yo_Robot 4:552beeda4722 61 pc.scanf( "%c%d%c", &command, &freq, &scale ) ;
Yo_Robot 4:552beeda4722 62 LPC_UART0->FCR = 0x06;
Yo_Robot 4:552beeda4722 63 pc.printf("\n %c%d%c \n", command, freq, scale );
Yo_Robot 3:8d5a9e3cd680 64
Yo_Robot 4:552beeda4722 65 // Establecer nueva frecuencia
Yo_Robot 4:552beeda4722 66 if( command == 'F')
Yo_Robot 4:552beeda4722 67 {
Yo_Robot 4:552beeda4722 68 if( scale == 'H' )
Yo_Robot 4:552beeda4722 69 setMR2( getMRvalue( freq ) );
Yo_Robot 4:552beeda4722 70 else if( scale == 'K' )
Yo_Robot 4:552beeda4722 71 setMR2( getMRvalue( freq * 1000 ) );
Yo_Robot 4:552beeda4722 72 }
Yo_Robot 4:552beeda4722 73
Yo_Robot 4:552beeda4722 74 // INICIAR Timer
Yo_Robot 4:552beeda4722 75 else if( command == 'I')
Yo_Robot 4:552beeda4722 76 startTimer2( );
Yo_Robot 4:552beeda4722 77
Yo_Robot 4:552beeda4722 78 // PARAR Timer
Yo_Robot 4:552beeda4722 79 else if( command == 'P')
Yo_Robot 4:552beeda4722 80 stopTimer2( );
Yo_Robot 4:552beeda4722 81
Yo_Robot 3:8d5a9e3cd680 82 }
Yo_Robot 3:8d5a9e3cd680 83
Yo_Robot 4:552beeda4722 84 int getMRvalue( int fout )
Yo_Robot 3:8d5a9e3cd680 85 {
Yo_Robot 4:552beeda4722 86 float exact, error;
Yo_Robot 4:552beeda4722 87 int toRegister;
Yo_Robot 4:552beeda4722 88
Yo_Robot 4:552beeda4722 89 exact = (24000000 /(fout*2) ) -1;
Yo_Robot 4:552beeda4722 90 toRegister = exact; // Valor redondeado;
Yo_Robot 4:552beeda4722 91 error = exact - toRegister;
Yo_Robot 4:552beeda4722 92
Yo_Robot 4:552beeda4722 93 pc.printf( "\n\n MR value: %d\n error: %f\n" ,toRegister ,error );
Yo_Robot 4:552beeda4722 94
Yo_Robot 4:552beeda4722 95 return toRegister;
Yo_Robot 3:8d5a9e3cd680 96 }
Yo_Robot 3:8d5a9e3cd680 97
Yo_Robot 3:8d5a9e3cd680 98
Yo_Robot 3:8d5a9e3cd680 99 void setMR2( int newValue )
Yo_Robot 3:8d5a9e3cd680 100 {
Yo_Robot 4:552beeda4722 101 LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas
Yo_Robot 4:552beeda4722 102 LPC_TIM2->MR3 = newValue; // Para testear el programa.
Yo_Robot 3:8d5a9e3cd680 103 }
Yo_Robot 3:8d5a9e3cd680 104
Yo_Robot 3:8d5a9e3cd680 105
Yo_Robot 3:8d5a9e3cd680 106
Yo_Robot 3:8d5a9e3cd680 107 void startTimer2()
Yo_Robot 3:8d5a9e3cd680 108 {
Yo_Robot 3:8d5a9e3cd680 109 // Arrancer el Timer 2
Yo_Robot 3:8d5a9e3cd680 110 LPC_TIM2->TCR = 1;
Yo_Robot 3:8d5a9e3cd680 111 }
Yo_Robot 3:8d5a9e3cd680 112
Yo_Robot 3:8d5a9e3cd680 113 void stopTimer2()
Yo_Robot 3:8d5a9e3cd680 114 {
Yo_Robot 4:552beeda4722 115 // Detener el Timer 2
Yo_Robot 3:8d5a9e3cd680 116 LPC_TIM2->TCR = 0x2;
Yo_Robot 4:552beeda4722 117 }
Yo_Robot 4:552beeda4722 118
Yo_Robot 4:552beeda4722 119 /* LEGACY FUNCTIONS
Yo_Robot 4:552beeda4722 120 *
Yo_Robot 4:552beeda4722 121 * El código actual no hace referencia a estas funciones
Yo_Robot 4:552beeda4722 122 * sin embargo no hay daño en definirlas.
Yo_Robot 4:552beeda4722 123 */
Yo_Robot 4:552beeda4722 124 void setMR3( int newValue )
Yo_Robot 4:552beeda4722 125 {
Yo_Robot 4:552beeda4722 126 LPC_TIM2->MR3 = newValue;
Yo_Robot 4:552beeda4722 127 }
Yo_Robot 4:552beeda4722 128
Yo_Robot 4:552beeda4722 129
Yo_Robot 4:552beeda4722 130 void setPrescaler( int newValue)
Yo_Robot 4:552beeda4722 131 {
Yo_Robot 4:552beeda4722 132 LPC_TIM2->PR = newValue;
Yo_Robot 4:552beeda4722 133 }