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:
Tue Apr 03 00:55:48 2012 +0000
Revision:
9:6976ac1a430e
Parent:
8:958dfe5052b9
Child:
12:c02b08dacc45
v0.2 Eth RPC. Compilo falta probar y algunas funciones mas

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 8:958dfe5052b9 47 // y MAT2.3 como salidas
Yo_Robot 8:958dfe5052b9 48
Yo_Robot 6:b4dae934e1ea 49 }
Yo_Robot 6:b4dae934e1ea 50
Yo_Robot 6:b4dae934e1ea 51
Yo_Robot 4:552beeda4722 52 int getMRvalue( int fout )
Yo_Robot 3:8d5a9e3cd680 53 {
Yo_Robot 4:552beeda4722 54 float exact, error;
Yo_Robot 4:552beeda4722 55 int toRegister;
Yo_Robot 4:552beeda4722 56
Yo_Robot 4:552beeda4722 57 exact = (24000000 /(fout*2) ) -1;
Yo_Robot 4:552beeda4722 58 toRegister = exact; // Valor redondeado;
Yo_Robot 8:958dfe5052b9 59 error = exact - toRegister;
Yo_Robot 9:6976ac1a430e 60 // printf( "\n\n MR value: %d\n error: %f\n" ,toRegister ,error );
Yo_Robot 4:552beeda4722 61 return toRegister;
Yo_Robot 3:8d5a9e3cd680 62 }
Yo_Robot 3:8d5a9e3cd680 63
Yo_Robot 3:8d5a9e3cd680 64
Yo_Robot 3:8d5a9e3cd680 65 void setMR2( int newValue )
Yo_Robot 3:8d5a9e3cd680 66 {
Yo_Robot 4:552beeda4722 67 LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas
Yo_Robot 4:552beeda4722 68 LPC_TIM2->MR3 = newValue; // Para testear el programa.
Yo_Robot 3:8d5a9e3cd680 69 }
Yo_Robot 3:8d5a9e3cd680 70
Yo_Robot 3:8d5a9e3cd680 71
Yo_Robot 3:8d5a9e3cd680 72
Yo_Robot 3:8d5a9e3cd680 73 void startTimer2()
Yo_Robot 3:8d5a9e3cd680 74 {
Yo_Robot 8:958dfe5052b9 75 // Arrancer el Timer 2
Yo_Robot 3:8d5a9e3cd680 76 LPC_TIM2->TCR = 1;
Yo_Robot 3:8d5a9e3cd680 77 }
Yo_Robot 3:8d5a9e3cd680 78
Yo_Robot 3:8d5a9e3cd680 79 void stopTimer2()
Yo_Robot 3:8d5a9e3cd680 80 {
Yo_Robot 4:552beeda4722 81 // Detener el Timer 2
Yo_Robot 3:8d5a9e3cd680 82 LPC_TIM2->TCR = 0x2;
Yo_Robot 4:552beeda4722 83 }
Yo_Robot 4:552beeda4722 84
Yo_Robot 4:552beeda4722 85 /* LEGACY FUNCTIONS
Yo_Robot 4:552beeda4722 86 *
Yo_Robot 5:c5aea1eb10bb 87 * El codigo actual no hace referencia a estas funciones
Yo_Robot 5:c5aea1eb10bb 88 * sin embargo no hay problema en definirlas.
Yo_Robot 4:552beeda4722 89 */
Yo_Robot 4:552beeda4722 90 void setMR3( int newValue )
Yo_Robot 4:552beeda4722 91 {
Yo_Robot 4:552beeda4722 92 LPC_TIM2->MR3 = newValue;
Yo_Robot 4:552beeda4722 93 }
Yo_Robot 4:552beeda4722 94
Yo_Robot 4:552beeda4722 95
Yo_Robot 4:552beeda4722 96 void setPrescaler( int newValue)
Yo_Robot 4:552beeda4722 97 {
Yo_Robot 4:552beeda4722 98 LPC_TIM2->PR = newValue;
Yo_Robot 4:552beeda4722 99 }
Yo_Robot 8:958dfe5052b9 100
Yo_Robot 8:958dfe5052b9 101