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@21:353b0fe8fc54, 2012-06-21 (annotated)
- Committer:
- Yo_Robot
- Date:
- Thu Jun 21 03:34:46 2012 +0000
- Revision:
- 21:353b0fe8fc54
- Parent:
- 20:4b154134ab20
- Child:
- 22:d5431fff164b
Version 1.9.1 Cambiado la funcin de baudios. I2C creado no configurado;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 4:552beeda4722 | 1 | /** |
Yo_Robot | 4:552beeda4722 | 2 | * @file setup.cpp |
Yo_Robot | 4:552beeda4722 | 3 | * @author Ernesto Palacios |
Yo_Robot | 20:4b154134ab20 | 4 | * @brief Codigo Fuente de las funciones para el deslizador. |
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 | 18:cf1e07d82630 | 14 | #include "EthernetNetIf.h" |
Yo_Robot | 3:8d5a9e3cd680 | 15 | #include "mbed.h" |
Yo_Robot | 3:8d5a9e3cd680 | 16 | |
Yo_Robot | 3:8d5a9e3cd680 | 17 | // Salida Serial de mbed |
Yo_Robot | 19:c26cf8a48986 | 18 | extern Serial pc; |
Yo_Robot | 19:c26cf8a48986 | 19 | extern Serial RS_232; |
Yo_Robot | 19:c26cf8a48986 | 20 | extern DigitalIn isPC; |
Yo_Robot | 12:c02b08dacc45 | 21 | extern DigitalOut pin_son; // SON |
Yo_Robot | 12:c02b08dacc45 | 22 | extern DigitalOut pin_dir; // SIGN+ |
Yo_Robot | 12:c02b08dacc45 | 23 | extern InterruptIn pin_alm; // ALM |
Yo_Robot | 12:c02b08dacc45 | 24 | extern AnalogOut aout; // +-10V |
Yo_Robot | 3:8d5a9e3cd680 | 25 | |
Yo_Robot | 20:4b154134ab20 | 26 | |
Yo_Robot | 15:a1ffa32ce9d1 | 27 | int fq_posicion = 10000; // Variable global donde se almacenara |
Yo_Robot | 15:a1ffa32ce9d1 | 28 | // la velocidad de posicionamiento en Hz |
Yo_Robot | 15:a1ffa32ce9d1 | 29 | |
Yo_Robot | 4:552beeda4722 | 30 | void setTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 31 | { |
Yo_Robot | 3:8d5a9e3cd680 | 32 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 3:8d5a9e3cd680 | 33 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 3:8d5a9e3cd680 | 34 | |
Yo_Robot | 3:8d5a9e3cd680 | 35 | // Resetear y parar el Timer |
Yo_Robot | 3:8d5a9e3cd680 | 36 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 3:8d5a9e3cd680 | 37 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 3:8d5a9e3cd680 | 38 | |
Yo_Robot | 4:552beeda4722 | 39 | // Establecer el Preescaler en cero |
Yo_Robot | 4:552beeda4722 | 40 | // SIn Preesclaer |
Yo_Robot | 4:552beeda4722 | 41 | LPC_TIM2->PR = 0; |
Yo_Robot | 3:8d5a9e3cd680 | 42 | |
Yo_Robot | 4:552beeda4722 | 43 | // Calcular el periodo Inicial |
Yo_Robot | 4:552beeda4722 | 44 | uint32_t periodo = ( SystemCoreClock / 400 ); |
Yo_Robot | 3:8d5a9e3cd680 | 45 | |
Yo_Robot | 3:8d5a9e3cd680 | 46 | // Establecer los Registros de Coincidencia |
Yo_Robot | 3:8d5a9e3cd680 | 47 | // ( Match Register ) |
Yo_Robot | 3:8d5a9e3cd680 | 48 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 4:552beeda4722 | 49 | LPC_TIM2->MR3 = periodo; // Legacy, salidas identicas |
Yo_Robot | 3:8d5a9e3cd680 | 50 | |
Yo_Robot | 4:552beeda4722 | 51 | LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 |
Yo_Robot | 3:8d5a9e3cd680 | 52 | |
Yo_Robot | 3:8d5a9e3cd680 | 53 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 3:8d5a9e3cd680 | 54 | // y MAT2.3 |
Yo_Robot | 3:8d5a9e3cd680 | 55 | |
Yo_Robot | 3:8d5a9e3cd680 | 56 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 8:958dfe5052b9 | 57 | // y MAT2.3 como salidas |
Yo_Robot | 8:958dfe5052b9 | 58 | |
Yo_Robot | 6:b4dae934e1ea | 59 | } |
Yo_Robot | 6:b4dae934e1ea | 60 | |
Yo_Robot | 12:c02b08dacc45 | 61 | void ISR_Serial() |
Yo_Robot | 12:c02b08dacc45 | 62 | { |
Yo_Robot | 12:c02b08dacc45 | 63 | int value; // Nuevo Valor |
Yo_Robot | 12:c02b08dacc45 | 64 | char command; // Comando al que aplicar el nuevo valor |
Yo_Robot | 12:c02b08dacc45 | 65 | |
Yo_Robot | 19:c26cf8a48986 | 66 | if( isPC ) |
Yo_Robot | 19:c26cf8a48986 | 67 | pc.scanf( "%d-%c", &value, &command ) ; |
Yo_Robot | 19:c26cf8a48986 | 68 | else |
Yo_Robot | 19:c26cf8a48986 | 69 | RS_232.scanf( "%d-%c", &value, &command ) ; |
Yo_Robot | 19:c26cf8a48986 | 70 | |
Yo_Robot | 12:c02b08dacc45 | 71 | // Establecer nueva frecuencia |
Yo_Robot | 12:c02b08dacc45 | 72 | if( command == 'H') |
Yo_Robot | 12:c02b08dacc45 | 73 | setPTO( value ); |
Yo_Robot | 12:c02b08dacc45 | 74 | |
Yo_Robot | 12:c02b08dacc45 | 75 | else if( command == 'K' ) |
Yo_Robot | 12:c02b08dacc45 | 76 | setPTO( value * 1000 ); |
Yo_Robot | 12:c02b08dacc45 | 77 | |
Yo_Robot | 12:c02b08dacc45 | 78 | // Nuevo voltaje de salida |
Yo_Robot | 12:c02b08dacc45 | 79 | // Alguna formula para calcular el Vout necesario |
Yo_Robot | 12:c02b08dacc45 | 80 | // -100% a +100% |
Yo_Robot | 12:c02b08dacc45 | 81 | else if( command == 'A') |
Yo_Robot | 14:039d070732d5 | 82 | aout = (float)( value + 10000.0 ) / 20000.0; |
Yo_Robot | 12:c02b08dacc45 | 83 | |
Yo_Robot | 12:c02b08dacc45 | 84 | |
Yo_Robot | 12:c02b08dacc45 | 85 | // Cambiar la direccion |
Yo_Robot | 12:c02b08dacc45 | 86 | else if( command == 'D') |
Yo_Robot | 14:039d070732d5 | 87 | { |
Yo_Robot | 14:039d070732d5 | 88 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 89 | pin_dir = value; |
Yo_Robot | 14:039d070732d5 | 90 | wait_us( 2 ); |
Yo_Robot | 14:039d070732d5 | 91 | startTimer2(); |
Yo_Robot | 14:039d070732d5 | 92 | } |
Yo_Robot | 14:039d070732d5 | 93 | |
Yo_Robot | 12:c02b08dacc45 | 94 | |
Yo_Robot | 12:c02b08dacc45 | 95 | //Encender el Servo |
Yo_Robot | 12:c02b08dacc45 | 96 | else if( command == 'S') |
Yo_Robot | 12:c02b08dacc45 | 97 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 98 | |
Yo_Robot | 18:cf1e07d82630 | 99 | else if( command == 'V') |
Yo_Robot | 18:cf1e07d82630 | 100 | fq_posicion = value; |
Yo_Robot | 18:cf1e07d82630 | 101 | |
Yo_Robot | 18:cf1e07d82630 | 102 | else if( command == 'G' ) |
Yo_Robot | 18:cf1e07d82630 | 103 | { |
Yo_Robot | 18:cf1e07d82630 | 104 | int pulsos = value; //Numero de pulsos a generar |
Yo_Robot | 18:cf1e07d82630 | 105 | float t_alto = pulsos / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 12:c02b08dacc45 | 106 | |
Yo_Robot | 18:cf1e07d82630 | 107 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 18:cf1e07d82630 | 108 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 18:cf1e07d82630 | 109 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 18:cf1e07d82630 | 110 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 18:cf1e07d82630 | 111 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 18:cf1e07d82630 | 112 | } |
Yo_Robot | 12:c02b08dacc45 | 113 | } |
Yo_Robot | 12:c02b08dacc45 | 114 | |
Yo_Robot | 12:c02b08dacc45 | 115 | |
Yo_Robot | 12:c02b08dacc45 | 116 | void setPTO( int freq ) |
Yo_Robot | 12:c02b08dacc45 | 117 | { |
Yo_Robot | 12:c02b08dacc45 | 118 | if( freq != 0 ) |
Yo_Robot | 12:c02b08dacc45 | 119 | { |
Yo_Robot | 12:c02b08dacc45 | 120 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 121 | setMR2( getMRvalue( freq ) ); |
Yo_Robot | 12:c02b08dacc45 | 122 | startTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 123 | |
Yo_Robot | 12:c02b08dacc45 | 124 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 125 | |
Yo_Robot | 12:c02b08dacc45 | 126 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 127 | LPC_TIM2->TC = 0x00; //Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 128 | } |
Yo_Robot | 12:c02b08dacc45 | 129 | } |
Yo_Robot | 12:c02b08dacc45 | 130 | |
Yo_Robot | 12:c02b08dacc45 | 131 | void setPTO_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 132 | { |
Yo_Robot | 12:c02b08dacc45 | 133 | int freq = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 134 | |
Yo_Robot | 12:c02b08dacc45 | 135 | if( freq != 0 ){ |
Yo_Robot | 12:c02b08dacc45 | 136 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 12:c02b08dacc45 | 137 | setMR2( getMRvalue( freq ) ); // Cambiar frefuencia |
Yo_Robot | 12:c02b08dacc45 | 138 | startTimer2(); // Iniciar Timer |
Yo_Robot | 14:039d070732d5 | 139 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 140 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 141 | else |
Yo_Robot | 14:039d070732d5 | 142 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 143 | }else{ |
Yo_Robot | 12:c02b08dacc45 | 144 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 145 | LPC_TIM2->TC = 0x00; // Resetear Timer |
Yo_Robot | 14:039d070732d5 | 146 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 147 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 148 | else |
Yo_Robot | 14:039d070732d5 | 149 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 150 | } |
Yo_Robot | 12:c02b08dacc45 | 151 | } |
Yo_Robot | 12:c02b08dacc45 | 152 | |
Yo_Robot | 15:a1ffa32ce9d1 | 153 | void setANG_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 154 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 155 | long int pulsos = atol( input ); //Numero de pulsos a generar |
Yo_Robot | 15:a1ffa32ce9d1 | 156 | float t_alto = pulsos / fq_posicion; //Tiempo que debe ser generado el tren de pulsos. |
Yo_Robot | 15:a1ffa32ce9d1 | 157 | |
Yo_Robot | 15:a1ffa32ce9d1 | 158 | stopTimer2(); //Deten el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 159 | setPTO( fq_posicion ); //Nueva frecuencia de salida |
Yo_Robot | 15:a1ffa32ce9d1 | 160 | startTimer2(); //Inicia el tren de pulsos |
Yo_Robot | 15:a1ffa32ce9d1 | 161 | wait( t_alto ); //Espera hasta llegar a la posicion |
Yo_Robot | 18:cf1e07d82630 | 162 | stopTimer2(); //Posicion alcanzada ALTO. |
Yo_Robot | 15:a1ffa32ce9d1 | 163 | |
Yo_Robot | 15:a1ffa32ce9d1 | 164 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 165 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 166 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 167 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 168 | |
Yo_Robot | 15:a1ffa32ce9d1 | 169 | } |
Yo_Robot | 21:353b0fe8fc54 | 170 | |
Yo_Robot | 21:353b0fe8fc54 | 171 | |
Yo_Robot | 15:a1ffa32ce9d1 | 172 | void setSPD_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 173 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 174 | fq_posicion = atoi( input ); |
Yo_Robot | 15:a1ffa32ce9d1 | 175 | // Esta funcion cambia la velocidad con la que se |
Yo_Robot | 15:a1ffa32ce9d1 | 176 | // posicionara el eje del motor en un angulo determinado |
Yo_Robot | 15:a1ffa32ce9d1 | 177 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 178 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 179 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 180 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 181 | |
Yo_Robot | 15:a1ffa32ce9d1 | 182 | } |
Yo_Robot | 21:353b0fe8fc54 | 183 | |
Yo_Robot | 21:353b0fe8fc54 | 184 | |
Yo_Robot | 15:a1ffa32ce9d1 | 185 | void getENC_eth( char * input, char * output ) |
Yo_Robot | 15:a1ffa32ce9d1 | 186 | { |
Yo_Robot | 15:a1ffa32ce9d1 | 187 | //Leer el Encoder ******* F A L T A |
Yo_Robot | 15:a1ffa32ce9d1 | 188 | if( pin_alm == 0 ) |
Yo_Robot | 15:a1ffa32ce9d1 | 189 | sprintf( output,"Ok" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 190 | else |
Yo_Robot | 15:a1ffa32ce9d1 | 191 | sprintf( output,"AL" ); |
Yo_Robot | 15:a1ffa32ce9d1 | 192 | } |
Yo_Robot | 15:a1ffa32ce9d1 | 193 | |
Yo_Robot | 12:c02b08dacc45 | 194 | |
Yo_Robot | 12:c02b08dacc45 | 195 | void ISR_Alarm() |
Yo_Robot | 12:c02b08dacc45 | 196 | { |
Yo_Robot | 12:c02b08dacc45 | 197 | pin_son = 0 ; |
Yo_Robot | 12:c02b08dacc45 | 198 | stopTimer2(); |
Yo_Robot | 12:c02b08dacc45 | 199 | aout = 0.5 ; |
Yo_Robot | 12:c02b08dacc45 | 200 | |
Yo_Robot | 19:c26cf8a48986 | 201 | if(isPC) |
Yo_Robot | 19:c26cf8a48986 | 202 | pc.printf( "AL" ); //ALARMA! solo es AL para que |
Yo_Robot | 19:c26cf8a48986 | 203 | //sea conciso con el modo ETH y funcione |
Yo_Robot | 19:c26cf8a48986 | 204 | //bien en LabVIEW. |
Yo_Robot | 19:c26cf8a48986 | 205 | else |
Yo_Robot | 19:c26cf8a48986 | 206 | RS_232.printf( "AL" ); |
Yo_Robot | 12:c02b08dacc45 | 207 | } |
Yo_Robot | 6:b4dae934e1ea | 208 | |
Yo_Robot | 4:552beeda4722 | 209 | int getMRvalue( int fout ) |
Yo_Robot | 3:8d5a9e3cd680 | 210 | { |
Yo_Robot | 4:552beeda4722 | 211 | int toRegister; |
Yo_Robot | 4:552beeda4722 | 212 | |
Yo_Robot | 12:c02b08dacc45 | 213 | toRegister = (24000000 /(fout*2.0) ) -1; |
Yo_Robot | 4:552beeda4722 | 214 | return toRegister; |
Yo_Robot | 3:8d5a9e3cd680 | 215 | } |
Yo_Robot | 3:8d5a9e3cd680 | 216 | |
Yo_Robot | 3:8d5a9e3cd680 | 217 | |
Yo_Robot | 3:8d5a9e3cd680 | 218 | void setMR2( int newValue ) |
Yo_Robot | 3:8d5a9e3cd680 | 219 | { |
Yo_Robot | 4:552beeda4722 | 220 | LPC_TIM2->MR2 = newValue; // Las dos salidas son identicas |
Yo_Robot | 4:552beeda4722 | 221 | LPC_TIM2->MR3 = newValue; // Para testear el programa. |
Yo_Robot | 3:8d5a9e3cd680 | 222 | } |
Yo_Robot | 3:8d5a9e3cd680 | 223 | |
Yo_Robot | 3:8d5a9e3cd680 | 224 | |
Yo_Robot | 3:8d5a9e3cd680 | 225 | |
Yo_Robot | 3:8d5a9e3cd680 | 226 | void startTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 227 | { |
Yo_Robot | 8:958dfe5052b9 | 228 | // Arrancer el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 229 | LPC_TIM2->TCR = 1; |
Yo_Robot | 3:8d5a9e3cd680 | 230 | } |
Yo_Robot | 3:8d5a9e3cd680 | 231 | |
Yo_Robot | 3:8d5a9e3cd680 | 232 | void stopTimer2() |
Yo_Robot | 3:8d5a9e3cd680 | 233 | { |
Yo_Robot | 4:552beeda4722 | 234 | // Detener el Timer 2 |
Yo_Robot | 3:8d5a9e3cd680 | 235 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 4:552beeda4722 | 236 | } |
Yo_Robot | 4:552beeda4722 | 237 | |
Yo_Robot | 12:c02b08dacc45 | 238 | |
Yo_Robot | 12:c02b08dacc45 | 239 | // **** Funciones Liberia Ethernet ***** // |
Yo_Robot | 12:c02b08dacc45 | 240 | |
Yo_Robot | 12:c02b08dacc45 | 241 | void setAout_eth( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 242 | { |
Yo_Robot | 12:c02b08dacc45 | 243 | int vout = atoi( input ); |
Yo_Robot | 14:039d070732d5 | 244 | aout = (float)( vout + 10000 ) / 20000; |
Yo_Robot | 14:039d070732d5 | 245 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 246 | sprintf( output,"Ok" ); |
Yo_Robot | 14:039d070732d5 | 247 | else |
Yo_Robot | 14:039d070732d5 | 248 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 249 | } |
Yo_Robot | 12:c02b08dacc45 | 250 | |
Yo_Robot | 12:c02b08dacc45 | 251 | |
Yo_Robot | 12:c02b08dacc45 | 252 | |
Yo_Robot | 12:c02b08dacc45 | 253 | void setDir_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 254 | { |
Yo_Robot | 12:c02b08dacc45 | 255 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 256 | |
Yo_Robot | 12:c02b08dacc45 | 257 | pin_dir = value; |
Yo_Robot | 12:c02b08dacc45 | 258 | |
Yo_Robot | 14:039d070732d5 | 259 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 260 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 261 | else |
Yo_Robot | 14:039d070732d5 | 262 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 263 | } |
Yo_Robot | 12:c02b08dacc45 | 264 | |
Yo_Robot | 12:c02b08dacc45 | 265 | |
Yo_Robot | 12:c02b08dacc45 | 266 | |
Yo_Robot | 12:c02b08dacc45 | 267 | void setSON_eth ( char * input, char * output ) |
Yo_Robot | 12:c02b08dacc45 | 268 | { |
Yo_Robot | 12:c02b08dacc45 | 269 | int value = atoi( input ); |
Yo_Robot | 12:c02b08dacc45 | 270 | |
Yo_Robot | 12:c02b08dacc45 | 271 | pin_son = value; |
Yo_Robot | 12:c02b08dacc45 | 272 | |
Yo_Robot | 14:039d070732d5 | 273 | if( pin_alm == 0 ) |
Yo_Robot | 14:039d070732d5 | 274 | sprintf( output,"Ok" ); |
Yo_Robot | 12:c02b08dacc45 | 275 | else |
Yo_Robot | 14:039d070732d5 | 276 | sprintf( output,"AL" ); |
Yo_Robot | 12:c02b08dacc45 | 277 | |
Yo_Robot | 12:c02b08dacc45 | 278 | } |
Yo_Robot | 12:c02b08dacc45 | 279 | |
Yo_Robot | 12:c02b08dacc45 | 280 | |
Yo_Robot | 21:353b0fe8fc54 | 281 | int getBaud(); |
Yo_Robot | 20:4b154134ab20 | 282 | { |
Yo_Robot | 20:4b154134ab20 | 283 | int baudios = 115200; //Valor por defecto |
Yo_Robot | 20:4b154134ab20 | 284 | |
Yo_Robot | 20:4b154134ab20 | 285 | FILE *fp = fopen("/local/config.mbd", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 20:4b154134ab20 | 286 | |
Yo_Robot | 20:4b154134ab20 | 287 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 20:4b154134ab20 | 288 | { |
Yo_Robot | 20:4b154134ab20 | 289 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 21:353b0fe8fc54 | 290 | printf("Cree un archivo de texto: config.mbd dentro de la unidad Mbed\n"); |
Yo_Robot | 21:353b0fe8fc54 | 291 | printf("que contenga las líneas:\n\n"); |
Yo_Robot | 21:353b0fe8fc54 | 292 | |
Yo_Robot | 21:353b0fe8fc54 | 293 | printf(" 1\n"); |
Yo_Robot | 21:353b0fe8fc54 | 294 | printf(" 2\n"); |
Yo_Robot | 21:353b0fe8fc54 | 295 | printf(" 3\n"); |
Yo_Robot | 21:353b0fe8fc54 | 296 | printf(" 4\n"); |
Yo_Robot | 21:353b0fe8fc54 | 297 | printf(" baudios: 115200\n"); |
Yo_Robot | 21:353b0fe8fc54 | 298 | |
Yo_Robot | 21:353b0fe8fc54 | 299 | printf("Cambie el valor de 115200 por la velocidad a la que desea transmitir:\n"); |
Yo_Robot | 21:353b0fe8fc54 | 300 | printf("luego reinicie el microcontrolador\n"); |
Yo_Robot | 20:4b154134ab20 | 301 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 302 | |
Yo_Robot | 20:4b154134ab20 | 303 | } |
Yo_Robot | 20:4b154134ab20 | 304 | else |
Yo_Robot | 20:4b154134ab20 | 305 | { |
Yo_Robot | 21:353b0fe8fc54 | 306 | // Cadenas de caracteres desde el Archivo config.mbd |
Yo_Robot | 20:4b154134ab20 | 307 | char notstr [04]; // linea vacia |
Yo_Robot | 21:353b0fe8fc54 | 308 | char baud [40]; // valor en baudios |
Yo_Robot | 20:4b154134ab20 | 309 | |
Yo_Robot | 20:4b154134ab20 | 310 | // Leer linea a linea el archivo |
Yo_Robot | 20:4b154134ab20 | 311 | // cuatro primeras lineas no sirven |
Yo_Robot | 20:4b154134ab20 | 312 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 313 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 314 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 315 | fgets( notstr, 4, fp ); |
Yo_Robot | 20:4b154134ab20 | 316 | |
Yo_Robot | 20:4b154134ab20 | 317 | fgets( baud, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 318 | |
Yo_Robot | 20:4b154134ab20 | 319 | printf("Cerrando archivo...\n"); |
Yo_Robot | 20:4b154134ab20 | 320 | fclose(fp); |
Yo_Robot | 20:4b154134ab20 | 321 | |
Yo_Robot | 20:4b154134ab20 | 322 | |
Yo_Robot | 20:4b154134ab20 | 323 | // Extraer los valores numericos |
Yo_Robot | 20:4b154134ab20 | 324 | sscanf( baud,"%*s %d",&baudios ); |
Yo_Robot | 20:4b154134ab20 | 325 | |
Yo_Robot | 20:4b154134ab20 | 326 | |
Yo_Robot | 20:4b154134ab20 | 327 | } |
Yo_Robot | 20:4b154134ab20 | 328 | |
Yo_Robot | 20:4b154134ab20 | 329 | return baudios; |
Yo_Robot | 20:4b154134ab20 | 330 | } |
Yo_Robot | 12:c02b08dacc45 | 331 | |
Yo_Robot | 21:353b0fe8fc54 | 332 | |
Yo_Robot | 4:552beeda4722 | 333 | /* LEGACY FUNCTIONS |
Yo_Robot | 4:552beeda4722 | 334 | * |
Yo_Robot | 20:4b154134ab20 | 335 | * El siguiente codigo no es utilizado por el |
Yo_Robot | 20:4b154134ab20 | 336 | * programa. Sin embargo pueden servir como |
Yo_Robot | 20:4b154134ab20 | 337 | * futuras referencias. |
Yo_Robot | 4:552beeda4722 | 338 | */ |
Yo_Robot | 4:552beeda4722 | 339 | void setMR3( int newValue ) |
Yo_Robot | 4:552beeda4722 | 340 | { |
Yo_Robot | 4:552beeda4722 | 341 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 4:552beeda4722 | 342 | } |
Yo_Robot | 4:552beeda4722 | 343 | |
Yo_Robot | 4:552beeda4722 | 344 | |
Yo_Robot | 4:552beeda4722 | 345 | void setPrescaler( int newValue) |
Yo_Robot | 4:552beeda4722 | 346 | { |
Yo_Robot | 4:552beeda4722 | 347 | LPC_TIM2->PR = newValue; |
Yo_Robot | 4:552beeda4722 | 348 | } |
Yo_Robot | 20:4b154134ab20 | 349 | |
Yo_Robot | 20:4b154134ab20 | 350 | |
Yo_Robot | 20:4b154134ab20 | 351 | EthernetNetIf configurarEthernet() |
Yo_Robot | 20:4b154134ab20 | 352 | { |
Yo_Robot | 20:4b154134ab20 | 353 | //____________ *** ARCHIVO DE CONFIGURACION ***_______________________ // |
Yo_Robot | 20:4b154134ab20 | 354 | |
Yo_Robot | 20:4b154134ab20 | 355 | printf("\n *** CONFIGURACION ETHERNET DE MBED ***\n"); |
Yo_Robot | 20:4b154134ab20 | 356 | printf("Leyendo archivo de configuracion...\n\n"); |
Yo_Robot | 20:4b154134ab20 | 357 | |
Yo_Robot | 20:4b154134ab20 | 358 | FILE *fp = fopen("/local/config.txt", "r"); // Abre el archivo y lo guarda en fp |
Yo_Robot | 20:4b154134ab20 | 359 | |
Yo_Robot | 20:4b154134ab20 | 360 | if(!fp) // En caso de no encontrarse el archivo |
Yo_Robot | 20:4b154134ab20 | 361 | { |
Yo_Robot | 20:4b154134ab20 | 362 | printf("\nEl archivo /mbed/config.txt no puede ser abierto!\n"); |
Yo_Robot | 20:4b154134ab20 | 363 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 364 | |
Yo_Robot | 20:4b154134ab20 | 365 | } |
Yo_Robot | 20:4b154134ab20 | 366 | else |
Yo_Robot | 20:4b154134ab20 | 367 | { |
Yo_Robot | 20:4b154134ab20 | 368 | // Cadenas de caracteres desde el Archivo config.txt |
Yo_Robot | 20:4b154134ab20 | 369 | char isDHCP [15]; //Modo Automatico o Manual |
Yo_Robot | 20:4b154134ab20 | 370 | char empty [2]; // Linea vacia |
Yo_Robot | 20:4b154134ab20 | 371 | char ip [40]; // Direccion IP |
Yo_Robot | 20:4b154134ab20 | 372 | char mask [40]; // Mascara de Subred |
Yo_Robot | 20:4b154134ab20 | 373 | char gate [40]; // Puerta de enlace |
Yo_Robot | 20:4b154134ab20 | 374 | char dns [40]; // Direccion DNS |
Yo_Robot | 20:4b154134ab20 | 375 | |
Yo_Robot | 20:4b154134ab20 | 376 | // Valores 'int' para agregar a la configuracion manual |
Yo_Robot | 20:4b154134ab20 | 377 | int DHCP; |
Yo_Robot | 20:4b154134ab20 | 378 | int n_ip[4]; |
Yo_Robot | 20:4b154134ab20 | 379 | int n_mask[4]; |
Yo_Robot | 20:4b154134ab20 | 380 | int n_gate[4]; |
Yo_Robot | 20:4b154134ab20 | 381 | int n_dns[4]; |
Yo_Robot | 20:4b154134ab20 | 382 | |
Yo_Robot | 20:4b154134ab20 | 383 | |
Yo_Robot | 20:4b154134ab20 | 384 | // Leer linea a linea el archivo |
Yo_Robot | 20:4b154134ab20 | 385 | fgets( isDHCP, 15, fp ); |
Yo_Robot | 20:4b154134ab20 | 386 | fgets( empty, 2, fp ); |
Yo_Robot | 20:4b154134ab20 | 387 | fgets( ip, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 388 | fgets( mask, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 389 | fgets( gate, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 390 | fgets( dns, 40, fp ); |
Yo_Robot | 20:4b154134ab20 | 391 | |
Yo_Robot | 20:4b154134ab20 | 392 | printf("Cerrando archivo...\n"); |
Yo_Robot | 20:4b154134ab20 | 393 | fclose(fp); |
Yo_Robot | 20:4b154134ab20 | 394 | |
Yo_Robot | 20:4b154134ab20 | 395 | // Extraer los valores numericos |
Yo_Robot | 20:4b154134ab20 | 396 | sscanf( isDHCP,"%*s %d",&DHCP ); |
Yo_Robot | 20:4b154134ab20 | 397 | |
Yo_Robot | 20:4b154134ab20 | 398 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0],&n_ip[1],&n_ip[2],&n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 399 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 400 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 401 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0],&n_dns[1],&n_dns[2],&n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 402 | |
Yo_Robot | 20:4b154134ab20 | 403 | |
Yo_Robot | 20:4b154134ab20 | 404 | if(DHCP) //En caso de modo DHCP |
Yo_Robot | 20:4b154134ab20 | 405 | { |
Yo_Robot | 20:4b154134ab20 | 406 | printf("\n Configurar red de manera automatica\n"); |
Yo_Robot | 20:4b154134ab20 | 407 | EthernetNetIf eth; //Crea la interfaz |
Yo_Robot | 20:4b154134ab20 | 408 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 409 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 410 | { |
Yo_Robot | 20:4b154134ab20 | 411 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 412 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 413 | } |
Yo_Robot | 20:4b154134ab20 | 414 | |
Yo_Robot | 20:4b154134ab20 | 415 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 416 | return eth; |
Yo_Robot | 20:4b154134ab20 | 417 | } |
Yo_Robot | 20:4b154134ab20 | 418 | else |
Yo_Robot | 20:4b154134ab20 | 419 | { |
Yo_Robot | 20:4b154134ab20 | 420 | sscanf( ip,"%*s %d.%d.%d.%d",&n_ip[0], &n_ip[1], &n_ip[2], &n_ip[3] ); |
Yo_Robot | 20:4b154134ab20 | 421 | sscanf( mask,"%*s %d.%d.%d.%d",&n_mask[0],&n_mask[1],&n_mask[2],&n_mask[3] ); |
Yo_Robot | 20:4b154134ab20 | 422 | sscanf( gate,"%*s %d.%d.%d.%d",&n_gate[0],&n_gate[1],&n_gate[2],&n_gate[3] ); |
Yo_Robot | 20:4b154134ab20 | 423 | sscanf( dns,"%*s %d.%d.%d.%d",&n_dns[0], &n_dns[1], &n_dns[2], &n_dns[3] ); |
Yo_Robot | 20:4b154134ab20 | 424 | printf(" %s %s %s %s %s\n " , isDHCP, ip, mask, gate, dns ); |
Yo_Robot | 20:4b154134ab20 | 425 | |
Yo_Robot | 20:4b154134ab20 | 426 | printf("\n Configurar red de manera manual\n"); |
Yo_Robot | 20:4b154134ab20 | 427 | EthernetNetIf eth( |
Yo_Robot | 20:4b154134ab20 | 428 | IpAddr( n_ip[0], n_ip[1], n_ip[2], n_ip[3]), //IP Address |
Yo_Robot | 20:4b154134ab20 | 429 | IpAddr( n_mask[0],n_mask[1],n_mask[2],n_mask[3]), //Network Mask |
Yo_Robot | 20:4b154134ab20 | 430 | IpAddr( n_gate[0],n_gate[1],n_gate[2],n_gate[3]), //Gateway |
Yo_Robot | 20:4b154134ab20 | 431 | IpAddr( n_dns[0], n_dns[1], n_dns[2], n_dns[3] ) //DNS |
Yo_Robot | 20:4b154134ab20 | 432 | ); |
Yo_Robot | 20:4b154134ab20 | 433 | |
Yo_Robot | 20:4b154134ab20 | 434 | EthernetErr ethErr = eth.setup(); |
Yo_Robot | 20:4b154134ab20 | 435 | if( ethErr ) |
Yo_Robot | 20:4b154134ab20 | 436 | { |
Yo_Robot | 20:4b154134ab20 | 437 | printf( "Error %d en la configuracion\n", ethErr ); |
Yo_Robot | 20:4b154134ab20 | 438 | exit(1); |
Yo_Robot | 20:4b154134ab20 | 439 | } |
Yo_Robot | 20:4b154134ab20 | 440 | |
Yo_Robot | 20:4b154134ab20 | 441 | printf("Configuracion Correcta\n\n"); |
Yo_Robot | 20:4b154134ab20 | 442 | return eth; |
Yo_Robot | 20:4b154134ab20 | 443 | } |
Yo_Robot | 20:4b154134ab20 | 444 | |
Yo_Robot | 20:4b154134ab20 | 445 | } |
Yo_Robot | 20:4b154134ab20 | 446 | |
Yo_Robot | 20:4b154134ab20 | 447 | EthernetNetIf eth; |
Yo_Robot | 20:4b154134ab20 | 448 | return eth; |
Yo_Robot | 20:4b154134ab20 | 449 | } |