This is a program to drive a stepper servomotor from SerialUSB without any other interrution but the serial one.
config.cpp@0:da3eb35a2787, 2012-04-09 (annotated)
- Committer:
- Yo_Robot
- Date:
- Mon Apr 09 03:10:30 2012 +0000
- Revision:
- 0:da3eb35a2787
version0.2 PTO using Timer2
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Yo_Robot | 0:da3eb35a2787 | 1 | /// Codigo Fuente para configurar al |
Yo_Robot | 0:da3eb35a2787 | 2 | |
Yo_Robot | 0:da3eb35a2787 | 3 | #include "config.h" |
Yo_Robot | 0:da3eb35a2787 | 4 | #include "mbed.h" |
Yo_Robot | 0:da3eb35a2787 | 5 | |
Yo_Robot | 0:da3eb35a2787 | 6 | // Salida Serial de mbed |
Yo_Robot | 0:da3eb35a2787 | 7 | extern Serial pc; |
Yo_Robot | 0:da3eb35a2787 | 8 | |
Yo_Robot | 0:da3eb35a2787 | 9 | |
Yo_Robot | 0:da3eb35a2787 | 10 | /** @brief: Esta funcion configura al Timer2 |
Yo_Robot | 0:da3eb35a2787 | 11 | * para que las salidas p5 y p6 del mbed |
Yo_Robot | 0:da3eb35a2787 | 12 | * se alternen cada vez que se iguala al |
Yo_Robot | 0:da3eb35a2787 | 13 | * registro MR2 y MR3. |
Yo_Robot | 0:da3eb35a2787 | 14 | */ |
Yo_Robot | 0:da3eb35a2787 | 15 | void Setup_PTO_Timer2() |
Yo_Robot | 0:da3eb35a2787 | 16 | { |
Yo_Robot | 0:da3eb35a2787 | 17 | // Encender Timer2 (PCONP[22]) |
Yo_Robot | 0:da3eb35a2787 | 18 | LPC_SC->PCONP |= 1 << 22; |
Yo_Robot | 0:da3eb35a2787 | 19 | |
Yo_Robot | 0:da3eb35a2787 | 20 | // Resetear y parar el Timer |
Yo_Robot | 0:da3eb35a2787 | 21 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 0:da3eb35a2787 | 22 | LPC_TIM2->CTCR = 0x0; |
Yo_Robot | 0:da3eb35a2787 | 23 | |
Yo_Robot | 0:da3eb35a2787 | 24 | // Establecer el Preescaler inicial 0.5 seg |
Yo_Robot | 0:da3eb35a2787 | 25 | LPC_TIM2->PR = 100; |
Yo_Robot | 0:da3eb35a2787 | 26 | |
Yo_Robot | 0:da3eb35a2787 | 27 | // Calcular el periodo |
Yo_Robot | 0:da3eb35a2787 | 28 | uint32_t periodo = 240; |
Yo_Robot | 0:da3eb35a2787 | 29 | |
Yo_Robot | 0:da3eb35a2787 | 30 | // Establecer los Registros de Coincidencia |
Yo_Robot | 0:da3eb35a2787 | 31 | // ( Match Register ) |
Yo_Robot | 0:da3eb35a2787 | 32 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 0:da3eb35a2787 | 33 | LPC_TIM2->MR3 = periodo * 2; |
Yo_Robot | 0:da3eb35a2787 | 34 | |
Yo_Robot | 0:da3eb35a2787 | 35 | LPC_TIM2->MCR |= 1 << 10; // Resetear Timer en MR3 |
Yo_Robot | 0:da3eb35a2787 | 36 | |
Yo_Robot | 0:da3eb35a2787 | 37 | LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 |
Yo_Robot | 0:da3eb35a2787 | 38 | // y MAT2.3 |
Yo_Robot | 0:da3eb35a2787 | 39 | |
Yo_Robot | 0:da3eb35a2787 | 40 | LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 |
Yo_Robot | 0:da3eb35a2787 | 41 | // y MAT2.3 como salidas |
Yo_Robot | 0:da3eb35a2787 | 42 | |
Yo_Robot | 0:da3eb35a2787 | 43 | } |
Yo_Robot | 0:da3eb35a2787 | 44 | |
Yo_Robot | 0:da3eb35a2787 | 45 | |
Yo_Robot | 0:da3eb35a2787 | 46 | /** @brief: Esta es la rutina que maneja las interrupciones |
Yo_Robot | 0:da3eb35a2787 | 47 | * seriales, al recibir un caracter. |
Yo_Robot | 0:da3eb35a2787 | 48 | */ |
Yo_Robot | 0:da3eb35a2787 | 49 | void ISR_Serial() |
Yo_Robot | 0:da3eb35a2787 | 50 | { |
Yo_Robot | 0:da3eb35a2787 | 51 | int newValue; |
Yo_Robot | 0:da3eb35a2787 | 52 | char command; |
Yo_Robot | 0:da3eb35a2787 | 53 | |
Yo_Robot | 0:da3eb35a2787 | 54 | pc.scanf( "%d-%c", &newValue, &command ) ; |
Yo_Robot | 0:da3eb35a2787 | 55 | pc.printf("\n %d-%c \n", newValue, command ); |
Yo_Robot | 0:da3eb35a2787 | 56 | |
Yo_Robot | 0:da3eb35a2787 | 57 | if( command == 'p') |
Yo_Robot | 0:da3eb35a2787 | 58 | setPrescaler( newValue ); |
Yo_Robot | 0:da3eb35a2787 | 59 | else if( command == 'm') |
Yo_Robot | 0:da3eb35a2787 | 60 | setMR2( newValue ); |
Yo_Robot | 0:da3eb35a2787 | 61 | else if( command == 'n') |
Yo_Robot | 0:da3eb35a2787 | 62 | setMR3( newValue ); |
Yo_Robot | 0:da3eb35a2787 | 63 | else if( command == 'a') |
Yo_Robot | 0:da3eb35a2787 | 64 | startTimer2(); |
Yo_Robot | 0:da3eb35a2787 | 65 | else if( command == 's') |
Yo_Robot | 0:da3eb35a2787 | 66 | stopTimer2(); |
Yo_Robot | 0:da3eb35a2787 | 67 | else if( command == 'l' ) |
Yo_Robot | 0:da3eb35a2787 | 68 | setPhaseAB(); |
Yo_Robot | 0:da3eb35a2787 | 69 | else if( command == 'r' ) |
Yo_Robot | 0:da3eb35a2787 | 70 | setPhaseBA(); |
Yo_Robot | 0:da3eb35a2787 | 71 | } |
Yo_Robot | 0:da3eb35a2787 | 72 | |
Yo_Robot | 0:da3eb35a2787 | 73 | |
Yo_Robot | 0:da3eb35a2787 | 74 | /** @brief: Esta Funcion cambia el preescaler |
Yo_Robot | 0:da3eb35a2787 | 75 | * directamente |
Yo_Robot | 0:da3eb35a2787 | 76 | */ |
Yo_Robot | 0:da3eb35a2787 | 77 | void setPrescaler( int newValue) |
Yo_Robot | 0:da3eb35a2787 | 78 | { |
Yo_Robot | 0:da3eb35a2787 | 79 | LPC_TIM2->PR = newValue; |
Yo_Robot | 0:da3eb35a2787 | 80 | } |
Yo_Robot | 0:da3eb35a2787 | 81 | |
Yo_Robot | 0:da3eb35a2787 | 82 | |
Yo_Robot | 0:da3eb35a2787 | 83 | void setMR2( int newValue ) |
Yo_Robot | 0:da3eb35a2787 | 84 | { |
Yo_Robot | 0:da3eb35a2787 | 85 | LPC_TIM2->MR2 = newValue; |
Yo_Robot | 0:da3eb35a2787 | 86 | } |
Yo_Robot | 0:da3eb35a2787 | 87 | |
Yo_Robot | 0:da3eb35a2787 | 88 | |
Yo_Robot | 0:da3eb35a2787 | 89 | void setMR3( int newValue ) |
Yo_Robot | 0:da3eb35a2787 | 90 | { |
Yo_Robot | 0:da3eb35a2787 | 91 | LPC_TIM2->MR3 = newValue; |
Yo_Robot | 0:da3eb35a2787 | 92 | } |
Yo_Robot | 0:da3eb35a2787 | 93 | |
Yo_Robot | 0:da3eb35a2787 | 94 | void startTimer2() |
Yo_Robot | 0:da3eb35a2787 | 95 | { |
Yo_Robot | 0:da3eb35a2787 | 96 | // Arrancer el Timer 2 |
Yo_Robot | 0:da3eb35a2787 | 97 | LPC_TIM2->TCR = 1; |
Yo_Robot | 0:da3eb35a2787 | 98 | } |
Yo_Robot | 0:da3eb35a2787 | 99 | |
Yo_Robot | 0:da3eb35a2787 | 100 | void stopTimer2() |
Yo_Robot | 0:da3eb35a2787 | 101 | { |
Yo_Robot | 0:da3eb35a2787 | 102 | // Arrancer el Timer 2 |
Yo_Robot | 0:da3eb35a2787 | 103 | LPC_TIM2->TCR = 0x2; |
Yo_Robot | 0:da3eb35a2787 | 104 | } |
Yo_Robot | 0:da3eb35a2787 | 105 | |
Yo_Robot | 0:da3eb35a2787 | 106 | void setPhaseAB() |
Yo_Robot | 0:da3eb35a2787 | 107 | { |
Yo_Robot | 0:da3eb35a2787 | 108 | // Calcular el periodo |
Yo_Robot | 0:da3eb35a2787 | 109 | uint32_t periodo = 240; |
Yo_Robot | 0:da3eb35a2787 | 110 | |
Yo_Robot | 0:da3eb35a2787 | 111 | // Establecer los Registros de Coincidencia |
Yo_Robot | 0:da3eb35a2787 | 112 | // ( Match Register ) |
Yo_Robot | 0:da3eb35a2787 | 113 | LPC_TIM2->MR2 = periodo; |
Yo_Robot | 0:da3eb35a2787 | 114 | LPC_TIM2->MR3 = periodo * 2; |
Yo_Robot | 0:da3eb35a2787 | 115 | |
Yo_Robot | 0:da3eb35a2787 | 116 | LPC_TIM2->MCR |= 1 << 10; // Resetear Timer en MR3 |
Yo_Robot | 0:da3eb35a2787 | 117 | LPC_TIM2->MCR &= ~( 1 << 7 ); // Resetear Timer en MR3 |
Yo_Robot | 0:da3eb35a2787 | 118 | } |
Yo_Robot | 0:da3eb35a2787 | 119 | |
Yo_Robot | 0:da3eb35a2787 | 120 | |
Yo_Robot | 0:da3eb35a2787 | 121 | void setPhaseBA() |
Yo_Robot | 0:da3eb35a2787 | 122 | { |
Yo_Robot | 0:da3eb35a2787 | 123 | // Calcular el periodo |
Yo_Robot | 0:da3eb35a2787 | 124 | uint32_t periodo = 240; |
Yo_Robot | 0:da3eb35a2787 | 125 | |
Yo_Robot | 0:da3eb35a2787 | 126 | // Establecer los Registros de Coincidencia |
Yo_Robot | 0:da3eb35a2787 | 127 | // ( Match Register ) |
Yo_Robot | 0:da3eb35a2787 | 128 | LPC_TIM2->MR2 = periodo * 2; |
Yo_Robot | 0:da3eb35a2787 | 129 | LPC_TIM2->MR3 = periodo; |
Yo_Robot | 0:da3eb35a2787 | 130 | |
Yo_Robot | 0:da3eb35a2787 | 131 | LPC_TIM2->MCR |= 1 << 7; // Resetear Timer en MR2 |
Yo_Robot | 0:da3eb35a2787 | 132 | LPC_TIM2->MCR &= ~( 1 << 10 ); // Resetear Timer en MR3 |
Yo_Robot | 0:da3eb35a2787 | 133 | } |