Proyecto de Tesis en Mecatrónica. Universidad Técnica del Norte. Ernesto Palacios <mecatronica.mid@gmail.com>
Dependencies: EthernetNetIf HTTPServer QEI_hw RPCInterface mbed
Revision 2:a1b556d78a7f, committed 2012-03-24
- Comitter:
- Yo_Robot
- Date:
- Sat Mar 24 04:40:21 2012 +0000
- Parent:
- 1:186187f69295
- Child:
- 3:8d5a9e3cd680
- Commit message:
- Works! Still needs to figure out how the period and match register works, but for a Stepper motor the base code is there!
Changed in this revision
--- a/main.cpp Fri Mar 23 03:16:06 2012 +0000 +++ b/main.cpp Sat Mar 24 04:40:21 2012 +0000 @@ -1,67 +1,33 @@ -// Example to set up an PTO based on the LPC TIMER0 external match register, ernestop - -#include "mbed.h" - -Serial pc( USBTX, USBRX ); -DigitalOut myled( LED1 ); - -void Setup_PTO_Timer0(); - -int main() { - - int six = 0; - six |= 15UL << 8; // New Value 16 - pc.printf( "\n 1 << 8 = %d ", six ); - - Setup_PTO_Timer0(); - - while(1) { - - int test = ( (uint32_t)LPC_TIM0->EMR & 4 ) >> 2 ; - if( test ) - myled = 1; - else - myled = 0; - - wait(0.05); - - } -} - -void Setup_PTO_Timer0(){ - - // power up TIMER0 (PCONP[1]) - LPC_SC->PCONP |= 1 << 1; - - // reset and set TIMER0 to timer mode - LPC_TIM0->TCR = 0x2; - LPC_TIM0->CTCR = 0x0; - - // set prescaler - LPC_TIM0->PR = 1000; - - // calculate period (1 interrupt every second) - uint32_t period = ( SystemCoreClock / 4000 ); - - // set Extyernat match register - LPC_TIM0->MR2 = period; - LPC_TIM0->MR3 = period * 2; - - LPC_TIM0->MCR |= 1 << 10; // reset on MR3 - - LPC_TIM0->EMR |= 15UL << 8; // Toogle Pin MAT2.2 - // and MAT2.3 - - LPC_PINCON->PINSEL0 |= 15UL << 16; //Set Funtcion to MAT2.2 - // and MAT2.3 - - //Start the Timer 0 - LPC_TIM0->TCR = 1; - - //OUTPUT ON PIN_5 AND PIN_6 - // OF MBED WILL TOGGLE EVERY - // SECOND... I hope! -} - - - +// El siguiente código sirve de ejemplo para +// establecer un tren de impulsos y controlar +// su frecuencia. + +#include "mbed.h" +#include "setup.h" + +Serial pc( USBTX, USBRX ); +DigitalOut myled( LED1 ); + +void Setup_PTO_Timer2(); +void ISR_Serial(); + +uint32_t PRESCALER_STEP; //Steps for the preescaler + +int main() { + + + pc.printf( "Teclee 'w' para aumentar, 's' para disminuir, 'z' cambia los pasos " ); + PRESCALER_STEP = 50UL; + //The default prescaler will be 0.05 sec + + Setup_PTO_Timer2(); + pc.attach( &ISR_Serial ); + + uint32_t period = ( uint32_t LPC_TIM2->PR ); + pc.printf( "\Period = %d", period ); + + while(1) { + + + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.cpp Sat Mar 24 04:40:21 2012 +0000 @@ -0,0 +1,108 @@ +/// Codigo Fuente para configurar al + +#include "setup.h" +#include "mbed.h" + + +// Esta variable global determina +// los incrementos del Preescaler +extern uint32_t PRESCALER_STEP; + +// Salida Serial de mbed +extern Serial pc; + + +/** @brief: Esta función configura al Timer2 + * para que las salidas p5 y p6 del mbed + * se alternen cada vez que se iguala al + * registro MR2 y MR3. + */ +void Setup_PTO_Timer2() +{ + // Encender Timer2 (PCONP[22]) + LPC_SC->PCONP |= 1 << 22; + + // Resetear y parar el Timer + LPC_TIM2->TCR = 0x2; + LPC_TIM2->CTCR = 0x0; + + // Establecer el Preescaler inicial 0.5 seg + LPC_TIM2->PR = 100; + + // Calcular el periodo + uint32_t periodo = ( SystemCoreClock / 4000 ); + + // Establecer los Registros de Coincidencia + // ( Match Register ) + LPC_TIM2->MR2 = periodo; + LPC_TIM2->MR3 = periodo * 2; + + LPC_TIM2->MCR |= 1 << 10; // Resetear Timer en MR3 + + LPC_TIM2->EMR |= 15UL << 8; // Alternar Pin MAT2.2 + // y MAT2.3 + + LPC_PINCON->PINSEL0 |= 15UL << 16; //Activar MAT2.2 + // y MAT2.3 como salidas + + // Arrancer el Timer 2 + LPC_TIM2->TCR = 1; + +} + + +/** @brief: Esta es la rutina que maneja las interrupciones + * seriales, al recibir un caracter. + */ +void ISR_Serial() +{ + char c = pc.getc(); + + if( c == 'w') + setPeriodUp(); + else if( c == 's') + setPeriodDown(); + + else if( c == 'z') + { + float newStep; + pc.printf("\nIntroducir nuevo paso: "); + pc.scanf( "%d", &newStep ); + setPeriodStep( newStep ); + } + +} + + +/** @brief: Esta Funcion incrementará el preescaler + * del timer en un paso. + */ +void setPeriodUp() +{ + // Es muy dícil llegar a un límite superior + // de 4294967295 para los 32bit del preescaler + // Pero quizás haga falta chequear el + + LPC_TIM2->PR += PRESCALER_STEP; +} + + +/** @brief: Esta Funcion decrementará el preescaler + * del timer en un paso. + */ +void setPeriodDown() +{ + uint32_t prescaler = (uint32_t ) LPC_TIM2->PR ; + + if( prescaler > PRESCALER_STEP ) + LPC_TIM2->PR -= PRESCALER_STEP; +} + + +/** @brief: Esta Funcion Cambiara los pasos en los que + * se aumenta o disminuye el Preescaler. + */ +void setPeriodStep( uint32_t newStep ) +{ + PRESCALER_STEP = newStep; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/setup.h Sat Mar 24 04:40:21 2012 +0000 @@ -0,0 +1,15 @@ + +#ifndef SETUP_H +#define SETUP_H + +#include "mbed.h" + +void Setup_PTO_Timer2(); +void ISR_Serial(); + +void setPeriodUp(); +void setPeriodDown(); +void setPeriodStep( uint32_t newStep ); + + +#endif \ No newline at end of file