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@2:a1b556d78a7f, 2012-03-24 (annotated)
- Committer:
- Yo_Robot
- Date:
- Sat Mar 24 04:40:21 2012 +0000
- Revision:
- 2:a1b556d78a7f
- Child:
- 3:8d5a9e3cd680
Works! Still needs to figure out how the period and match register works, but for a Stepper motor the base code is there!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 2:a1b556d78a7f | 1 | /// Codigo Fuente para configurar al |
Yo_Robot | 2:a1b556d78a7f | 2 | |
Yo_Robot | 2:a1b556d78a7f | 3 | #include "setup.h" |
Yo_Robot | 2:a1b556d78a7f | 4 | #include "mbed.h" |
Yo_Robot | 2:a1b556d78a7f | 5 | |
Yo_Robot | 2:a1b556d78a7f | 6 | |
Yo_Robot | 2:a1b556d78a7f | 7 | // Esta variable global determina |
Yo_Robot | 2:a1b556d78a7f | 8 | // los incrementos del Preescaler |
Yo_Robot | 2:a1b556d78a7f | 9 | extern uint32_t PRESCALER_STEP; |
Yo_Robot | 2:a1b556d78a7f | 10 | |
Yo_Robot | 2:a1b556d78a7f | 11 | // Salida Serial de mbed |
Yo_Robot | 2:a1b556d78a7f | 12 | extern Serial pc; |
Yo_Robot | 2:a1b556d78a7f | 13 | |
Yo_Robot | 2:a1b556d78a7f | 14 | |
Yo_Robot | 2:a1b556d78a7f | 15 | /** @brief: Esta función configura al Timer2 |
Yo_Robot | 2:a1b556d78a7f | 16 | * para que las salidas p5 y p6 del mbed |
Yo_Robot | 2:a1b556d78a7f | 17 | * se alternen cada vez que se iguala al |
Yo_Robot | 2:a1b556d78a7f | 18 | * registro MR2 y MR3. |
Yo_Robot | 2:a1b556d78a7f | 19 | */ |
Yo_Robot | 2:a1b556d78a7f | 20 | void Setup_PTO_Timer2() |
Yo_Robot | 2:a1b556d78a7f | 21 | { |
Yo_Robot | 2:a1b556d78a7f | 22 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 2:a1b556d78a7f | 23 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 2:a1b556d78a7f | 24 | |
Yo_Robot | 2:a1b556d78a7f | 25 | // Resetear y parar el Timer |
Yo_Robot | 2:a1b556d78a7f | 26 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 2:a1b556d78a7f | 27 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 2:a1b556d78a7f | 28 | |
Yo_Robot | 2:a1b556d78a7f | 29 | // Establecer el Preescaler inicial 0.5 seg |
Yo_Robot | 2:a1b556d78a7f | 30 | LPC_TIM2->PR = 100; |
Yo_Robot | 2:a1b556d78a7f | 31 | |
Yo_Robot | 2:a1b556d78a7f | 32 | // Calcular el periodo |
Yo_Robot | 2:a1b556d78a7f | 33 | uint32_t periodo = ( SystemCoreClock / 4000 ); |
Yo_Robot | 2:a1b556d78a7f | 34 | |
Yo_Robot | 2:a1b556d78a7f | 35 | // Establecer los Registros de Coincidencia |
Yo_Robot | 2:a1b556d78a7f | 36 | // ( Match Register ) |
Yo_Robot | 2:a1b556d78a7f | 37 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 2:a1b556d78a7f | 38 | LPC_TIM2->MR3 = periodo * 2; |
Yo_Robot | 2:a1b556d78a7f | 39 | |
Yo_Robot | 2:a1b556d78a7f | 40 | LPC_TIM2->MCR |= 1 << 10; // Resetear Timer en MR3 |
Yo_Robot | 2:a1b556d78a7f | 41 | |
Yo_Robot | 2:a1b556d78a7f | 42 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 2:a1b556d78a7f | 43 | // y MAT2.3 |
Yo_Robot | 2:a1b556d78a7f | 44 | |
Yo_Robot | 2:a1b556d78a7f | 45 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 2:a1b556d78a7f | 46 | // y MAT2.3 como salidas |
Yo_Robot | 2:a1b556d78a7f | 47 | |
Yo_Robot | 2:a1b556d78a7f | 48 | // Arrancer el Timer 2 |
Yo_Robot | 2:a1b556d78a7f | 49 | LPC_TIM2->TCR = 1; |
Yo_Robot | 2:a1b556d78a7f | 50 | |
Yo_Robot | 2:a1b556d78a7f | 51 | } |
Yo_Robot | 2:a1b556d78a7f | 52 | |
Yo_Robot | 2:a1b556d78a7f | 53 | |
Yo_Robot | 2:a1b556d78a7f | 54 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 2:a1b556d78a7f | 55 | * seriales, al recibir un caracter. |
Yo_Robot | 2:a1b556d78a7f | 56 | */ |
Yo_Robot | 2:a1b556d78a7f | 57 | void ISR_Serial() |
Yo_Robot | 2:a1b556d78a7f | 58 | { |
Yo_Robot | 2:a1b556d78a7f | 59 | char c = pc.getc(); |
Yo_Robot | 2:a1b556d78a7f | 60 | |
Yo_Robot | 2:a1b556d78a7f | 61 | if( c == 'w') |
Yo_Robot | 2:a1b556d78a7f | 62 | setPeriodUp(); |
Yo_Robot | 2:a1b556d78a7f | 63 | else if( c == 's') |
Yo_Robot | 2:a1b556d78a7f | 64 | setPeriodDown(); |
Yo_Robot | 2:a1b556d78a7f | 65 | |
Yo_Robot | 2:a1b556d78a7f | 66 | else if( c == 'z') |
Yo_Robot | 2:a1b556d78a7f | 67 | { |
Yo_Robot | 2:a1b556d78a7f | 68 | float newStep; |
Yo_Robot | 2:a1b556d78a7f | 69 | pc.printf("\nIntroducir nuevo paso: "); |
Yo_Robot | 2:a1b556d78a7f | 70 | pc.scanf( "%d", &newStep ); |
Yo_Robot | 2:a1b556d78a7f | 71 | setPeriodStep( newStep ); |
Yo_Robot | 2:a1b556d78a7f | 72 | } |
Yo_Robot | 2:a1b556d78a7f | 73 | |
Yo_Robot | 2:a1b556d78a7f | 74 | } |
Yo_Robot | 2:a1b556d78a7f | 75 | |
Yo_Robot | 2:a1b556d78a7f | 76 | |
Yo_Robot | 2:a1b556d78a7f | 77 | /** @brief: Esta Funcion incrementará el preescaler |
Yo_Robot | 2:a1b556d78a7f | 78 | * del timer en un paso. |
Yo_Robot | 2:a1b556d78a7f | 79 | */ |
Yo_Robot | 2:a1b556d78a7f | 80 | void setPeriodUp() |
Yo_Robot | 2:a1b556d78a7f | 81 | { |
Yo_Robot | 2:a1b556d78a7f | 82 | // Es muy dícil llegar a un límite superior |
Yo_Robot | 2:a1b556d78a7f | 83 | // de 4294967295 para los 32bit del preescaler |
Yo_Robot | 2:a1b556d78a7f | 84 | // Pero quizás haga falta chequear el |
Yo_Robot | 2:a1b556d78a7f | 85 | |
Yo_Robot | 2:a1b556d78a7f | 86 | LPC_TIM2->PR += PRESCALER_STEP; |
Yo_Robot | 2:a1b556d78a7f | 87 | } |
Yo_Robot | 2:a1b556d78a7f | 88 | |
Yo_Robot | 2:a1b556d78a7f | 89 | |
Yo_Robot | 2:a1b556d78a7f | 90 | /** @brief: Esta Funcion decrementará el preescaler |
Yo_Robot | 2:a1b556d78a7f | 91 | * del timer en un paso. |
Yo_Robot | 2:a1b556d78a7f | 92 | */ |
Yo_Robot | 2:a1b556d78a7f | 93 | void setPeriodDown() |
Yo_Robot | 2:a1b556d78a7f | 94 | { |
Yo_Robot | 2:a1b556d78a7f | 95 | uint32_t prescaler = (uint32_t ) LPC_TIM2->PR ; |
Yo_Robot | 2:a1b556d78a7f | 96 | |
Yo_Robot | 2:a1b556d78a7f | 97 | if( prescaler > PRESCALER_STEP ) |
Yo_Robot | 2:a1b556d78a7f | 98 | LPC_TIM2->PR -= PRESCALER_STEP; |
Yo_Robot | 2:a1b556d78a7f | 99 | } |
Yo_Robot | 2:a1b556d78a7f | 100 | |
Yo_Robot | 2:a1b556d78a7f | 101 | |
Yo_Robot | 2:a1b556d78a7f | 102 | /** @brief: Esta Funcion Cambiara los pasos en los que |
Yo_Robot | 2:a1b556d78a7f | 103 | * se aumenta o disminuye el Preescaler. |
Yo_Robot | 2:a1b556d78a7f | 104 | */ |
Yo_Robot | 2:a1b556d78a7f | 105 | void setPeriodStep( uint32_t newStep ) |
Yo_Robot | 2:a1b556d78a7f | 106 | { |
Yo_Robot | 2:a1b556d78a7f | 107 | PRESCALER_STEP = newStep; |
Yo_Robot | 2:a1b556d78a7f | 108 | } |