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@7:d9aca501126f, 2012-04-02 (annotated)
- Committer:
- Yo_Robot
- Date:
- Mon Apr 02 19:47:49 2012 +0000
- Revision:
- 7:d9aca501126f
- Parent:
- 6:b4dae934e1ea
- Child:
- 8:958dfe5052b9
- Child:
- 10:0ebc4955cbe7
v0.2 Serial no RPC. PTO funciona bien, Aout falta revisar.
Who changed what in which revision?
User | Revision | Line number | New 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 | 7:d9aca501126f | 18 | extern DigitalOut pin_son; // SON |
Yo_Robot | 7:d9aca501126f | 19 | extern DigitalOut pin_dir; // SIGN+ |
Yo_Robot | 7:d9aca501126f | 20 | extern InterruptIn pin_alm; // ALM |
Yo_Robot | 7:d9aca501126f | 21 | extern AnalogOut aout; // +-10V |
Yo_Robot | 6:b4dae934e1ea | 22 | |
Yo_Robot | 3:8d5a9e3cd680 | 23 | |
Yo_Robot | 3:8d5a9e3cd680 | 24 | |
Yo_Robot | 4:552beeda4722 | 25 | void setTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 26 | { |
Yo_Robot | 3:8d5a9e3cd680 | 27 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 3:8d5a9e3cd680 | 28 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 3:8d5a9e3cd680 | 29 | |
Yo_Robot | 3:8d5a9e3cd680 | 30 | // Resetear y parar el Timer |
Yo_Robot | 3:8d5a9e3cd680 | 31 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 3:8d5a9e3cd680 | 32 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 3:8d5a9e3cd680 | 33 | |
Yo_Robot | 4:552beeda4722 | 34 | // Establecer el Preescaler en cero |
Yo_Robot | 4:552beeda4722 | 35 | // SIn Preesclaer |
Yo_Robot | 4:552beeda4722 | 36 | LPC_TIM2->PR = 0; |
Yo_Robot | 3:8d5a9e3cd680 | 37 | |
Yo_Robot | 4:552beeda4722 | 38 | // Calcular el periodo Inicial |
Yo_Robot | 4:552beeda4722 | 39 | uint32_t periodo = ( SystemCoreClock / 400 ); |
Yo_Robot | 3:8d5a9e3cd680 | 40 | |
Yo_Robot | 3:8d5a9e3cd680 | 41 | // Establecer los Registros de Coincidencia |
Yo_Robot | 3:8d5a9e3cd680 | 42 | // ( Match Register ) |
Yo_Robot | 3:8d5a9e3cd680 | 43 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 4:552beeda4722 | 44 | LPC_TIM2->MR3 = periodo; // Legacy, salidas identicas |
Yo_Robot | 3:8d5a9e3cd680 | 45 | |
Yo_Robot | 4:552beeda4722 | 46 | LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 |
Yo_Robot | 3:8d5a9e3cd680 | 47 | |
Yo_Robot | 3:8d5a9e3cd680 | 48 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 3:8d5a9e3cd680 | 49 | // y MAT2.3 |
Yo_Robot | 3:8d5a9e3cd680 | 50 | |
Yo_Robot | 3:8d5a9e3cd680 | 51 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 6:b4dae934e1ea | 52 | // y MAT2.3 como salidas |
Yo_Robot | 6:b4dae934e1ea | 53 | } |
Yo_Robot | 6:b4dae934e1ea | 54 | |
Yo_Robot | 6:b4dae934e1ea | 55 | |
Yo_Robot | 6:b4dae934e1ea | 56 | void ISR_Serial() |
Yo_Robot | 6:b4dae934e1ea | 57 | { |
Yo_Robot | 7:d9aca501126f | 58 | int value; // Nuevo Valor |
Yo_Robot | 7:d9aca501126f | 59 | char command; // Comando al que aplicar el nuevo valor |
Yo_Robot | 6:b4dae934e1ea | 60 | |
Yo_Robot | 7:d9aca501126f | 61 | pc.scanf( "%d-%c", &value, &command ) ; |
Yo_Robot | 7:d9aca501126f | 62 | pc.printf("\n %d-%c \n", value, command ); |
Yo_Robot | 6:b4dae934e1ea | 63 | |
Yo_Robot | 6:b4dae934e1ea | 64 | // Establecer nueva frecuencia |
Yo_Robot | 7:d9aca501126f | 65 | if( command == 'H') |
Yo_Robot | 7:d9aca501126f | 66 | setPTO( value ); |
Yo_Robot | 7:d9aca501126f | 67 | |
Yo_Robot | 7:d9aca501126f | 68 | else if( command == 'K' ) |
Yo_Robot | 7:d9aca501126f | 69 | setPTO( value * 1000 ); |
Yo_Robot | 7:d9aca501126f | 70 | |
Yo_Robot | 7:d9aca501126f | 71 | |
Yo_Robot | 7:d9aca501126f | 72 | |
Yo_Robot | 7:d9aca501126f | 73 | // Nuevo voltaje de salida |
Yo_Robot | 7:d9aca501126f | 74 | // Alguna formula para calcular el Vout necesario |
Yo_Robot | 7:d9aca501126f | 75 | // -100% a +100% |
Yo_Robot | 7:d9aca501126f | 76 | else if( command == 'A') |
Yo_Robot | 7:d9aca501126f | 77 | aout = ( value + 100 ) / 200; |
Yo_Robot | 7:d9aca501126f | 78 | |
Yo_Robot | 7:d9aca501126f | 79 | |
Yo_Robot | 7:d9aca501126f | 80 | // Cambiar la direccion |
Yo_Robot | 7:d9aca501126f | 81 | else if( command == 'D') |
Yo_Robot | 7:d9aca501126f | 82 | pin_dir = value; |
Yo_Robot | 7:d9aca501126f | 83 | |
Yo_Robot | 7:d9aca501126f | 84 | |
Yo_Robot | 7:d9aca501126f | 85 | //Encender el Servo |
Yo_Robot | 7:d9aca501126f | 86 | else if( command == 'S') |
Yo_Robot | 7:d9aca501126f | 87 | pin_son = value; |
Yo_Robot | 7:d9aca501126f | 88 | |
Yo_Robot | 7:d9aca501126f | 89 | //else if( command == 'E') |
Yo_Robot | 7:d9aca501126f | 90 | // setDir( value ); |
Yo_Robot | 6:b4dae934e1ea | 91 | |
Yo_Robot | 7:d9aca501126f | 92 | } |
Yo_Robot | 7:d9aca501126f | 93 | |
Yo_Robot | 7:d9aca501126f | 94 | |
Yo_Robot | 7:d9aca501126f | 95 | void setPTO( int freq ) |
Yo_Robot | 7:d9aca501126f | 96 | { |
Yo_Robot | 7:d9aca501126f | 97 | if( freq != 0 ) |
Yo_Robot | 7:d9aca501126f | 98 | { |
Yo_Robot | 7:d9aca501126f | 99 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 7:d9aca501126f | 100 | setMR2( getMRvalue( freq ) ); |
Yo_Robot | 7:d9aca501126f | 101 | startTimer2(); |
Yo_Robot | 7:d9aca501126f | 102 | |
Yo_Robot | 7:d9aca501126f | 103 | }else{ |
Yo_Robot | 6:b4dae934e1ea | 104 | |
Yo_Robot | 7:d9aca501126f | 105 | stopTimer2(); |
Yo_Robot | 7:d9aca501126f | 106 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 7:d9aca501126f | 107 | } |
Yo_Robot | 7:d9aca501126f | 108 | } |
Yo_Robot | 7:d9aca501126f | 109 | |
Yo_Robot | 7:d9aca501126f | 110 | void ISR_Alarm() |
Yo_Robot | 7:d9aca501126f | 111 | { |
Yo_Robot | 7:d9aca501126f | 112 | pin_son = 0 ; |
Yo_Robot | 7:d9aca501126f | 113 | stopTimer2(); |
Yo_Robot | 7:d9aca501126f | 114 | aout = 0.5 ; |
Yo_Robot | 7:d9aca501126f | 115 | |
Yo_Robot | 7:d9aca501126f | 116 | pc.printf( "\n\n ERROR: ALARMA \n\n " ); |
Yo_Robot | 7:d9aca501126f | 117 | |
Yo_Robot | 6:b4dae934e1ea | 118 | } |
Yo_Robot | 6:b4dae934e1ea | 119 | |
Yo_Robot | 4:552beeda4722 | 120 | int getMRvalue( int fout ) |
Yo_Robot | 3:8d5a9e3cd680 | 121 | { |
Yo_Robot | 4:552beeda4722 | 122 | float exact, error; |
Yo_Robot | 4:552beeda4722 | 123 | int toRegister; |
Yo_Robot | 4:552beeda4722 | 124 | |
Yo_Robot | 4:552beeda4722 | 125 | exact = (24000000 /(fout*2) ) -1; |
Yo_Robot | 4:552beeda4722 | 126 | toRegister = exact; // Valor redondeado; |
Yo_Robot | 7:d9aca501126f | 127 | //error = exact - toRegister; |
Yo_Robot | 4:552beeda4722 | 128 | |
Yo_Robot | 7:d9aca501126f | 129 | //pc.printf( "\n\n MR value: %d\n error: %f\n" ,toRegister ,error ); |
Yo_Robot | 4:552beeda4722 | 130 | |
Yo_Robot | 4:552beeda4722 | 131 | return toRegister; |
Yo_Robot | 3:8d5a9e3cd680 | 132 | } |
Yo_Robot | 3:8d5a9e3cd680 | 133 | |
Yo_Robot | 3:8d5a9e3cd680 | 134 | |
Yo_Robot | 3:8d5a9e3cd680 | 135 | void setMR2( int newValue ) |
Yo_Robot | 3:8d5a9e3cd680 | 136 | { |
Yo_Robot | 4:552beeda4722 | 137 | LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas |
Yo_Robot | 4:552beeda4722 | 138 | LPC_TIM2->MR3 = newValue; // Para testear el programa. |
Yo_Robot | 3:8d5a9e3cd680 | 139 | } |
Yo_Robot | 3:8d5a9e3cd680 | 140 | |
Yo_Robot | 3:8d5a9e3cd680 | 141 | |
Yo_Robot | 3:8d5a9e3cd680 | 142 | |
Yo_Robot | 3:8d5a9e3cd680 | 143 | void startTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 144 | { |
Yo_Robot | 7:d9aca501126f | 145 | // Arrancar el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 146 | LPC_TIM2->TCR = 1; |
Yo_Robot | 3:8d5a9e3cd680 | 147 | } |
Yo_Robot | 3:8d5a9e3cd680 | 148 | |
Yo_Robot | 3:8d5a9e3cd680 | 149 | void stopTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 150 | { |
Yo_Robot | 4:552beeda4722 | 151 | // Detener el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 152 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 4:552beeda4722 | 153 | } |
Yo_Robot | 4:552beeda4722 | 154 | |
Yo_Robot | 6:b4dae934e1ea | 155 | |
Yo_Robot | 6:b4dae934e1ea | 156 | |
Yo_Robot | 6:b4dae934e1ea | 157 | |
Yo_Robot | 6:b4dae934e1ea | 158 | |
Yo_Robot | 4:552beeda4722 | 159 | /* LEGACY FUNCTIONS |
Yo_Robot | 4:552beeda4722 | 160 | * |
Yo_Robot | 5:c5aea1eb10bb | 161 | * El codigo actual no hace referencia a estas funciones |
Yo_Robot | 5:c5aea1eb10bb | 162 | * sin embargo no hay problema en definirlas. |
Yo_Robot | 4:552beeda4722 | 163 | */ |
Yo_Robot | 4:552beeda4722 | 164 | void setMR3( int newValue ) |
Yo_Robot | 4:552beeda4722 | 165 | { |
Yo_Robot | 4:552beeda4722 | 166 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 4:552beeda4722 | 167 | } |
Yo_Robot | 4:552beeda4722 | 168 | |
Yo_Robot | 4:552beeda4722 | 169 | |
Yo_Robot | 4:552beeda4722 | 170 | void setPrescaler( int newValue) |
Yo_Robot | 4:552beeda4722 | 171 | { |
Yo_Robot | 4:552beeda4722 | 172 | LPC_TIM2->PR = newValue; |
Yo_Robot | 4:552beeda4722 | 173 | } |