/* Author Date Comment * Diego Melli 28/08/2015 * Andrea Delbuggio 28/08/2015 * Fabiana Andrade 28/08/2015 *****************************************************************************/ /** I N C L U D E S **********************************************************/ #include #include #include "system/typedefs.h" #include "system/usb/usb.h" #include "user/stepMotor.h" #include "io_cfg.h" /* I/O pin mapping */ #include "user/handlerManager.h" #include "user/usb4butia.h" #include #include /** V A R I A B L E S ********************************************************/ #pragma udata void (*callback)(void); int bitEnviados=0;//indica los bit enviados (0..4) int siguiente=0; byte pasoData0=0x01; //paso a enviar unsigned int steps=0;//cantidad steps short velocidad = 800; byte tipoPaso=SIMPLE; //tipo de paso {0-simple, 1-normal, 2-medio} byte sentido=ADELANTE; BOOL motorParado=TRUE; //motor apagado BOOL hacerFlanco=TRUE;//auxiliar usado en la funcion enviarCero BOOL envioCero=TRUE; byte* sendBufferStepMotor; /* buffer to send data*/ byte handlerDist; /** P R I V A T E P R O T O T Y P E S ***************************************/ void StepMotorInit(byte handler); void StepMotorReceived(byte*, byte, byte); void StepMotorRelease(byte handler); /* Table used by te framework to get a fixed reference point to the user module functions defined by the framework */ /** USER MODULE REFERENCE*****************************************************/ #pragma romdata user const uTab userStepperModuleTable = {&StepMotorInit, &StepMotorRelease, "stepper"}; #pragma code /** D E C L A R A T I O N S **************************************************/ #pragma code module void realizarPaso(void); void enviarBit(void){ if(hacerFlanco){ hacerFlanco=FALSE; getPortDescriptor(handlerDist)->set_data(0); if(((pasoData0 &(0x01<>bitEnviados)==1){ getPortDescriptor(handlerDist)->set_data(1); } registerT0eventInEvent(tiempoFlanco,&enviarBit); }else{ hacerFlanco=TRUE; bitEnviados++; getPortDescriptor(handlerDist)->set_data(1); realizarPaso(); } } void haceFlanco(void){ getPortDescriptor(handlerDist)->set_data(0); getPortDescriptor(handlerDist)->set_data(1); registerT0eventInEvent(tiempoFlanco,callback); } void realizarPaso(void){ //realicé todos los pasos if(steps<1){ //actualizo el estado del motor motorParado=TRUE; return; } //luego de enviar los 4 bits tengo que hacer un flanco para activar //la salida del shift register if(bitEnviados==4){ bitEnviados=0; callback=&realizarPaso; steps--; haceFlanco(); return; } else if(bitEnviados==0){ switch(tipoPaso){ case SIMPLE: case NORMAL: //siguiente= 0..3 siguiente=((siguiente+1)%4); if(sentido==ADELANTE) pasoData0=pasoArray[tipoPaso][siguiente]; else if(sentido==ATRAS) pasoData0=pasoArray[tipoPaso][3-siguiente]; else pasoData0=0x00;//STOP break; case MEDIO: //siguiente= 0..7 siguiente=(siguiente+1)%8; if(sentido==ADELANTE) pasoData0=pasoMedioArray[siguiente]; else if(sentido==ATRAS) pasoData0=pasoMedioArray[7-siguiente]; else pasoData0=0x00;//STOP break; } } registerT0eventInEvent(velocidad,&enviarBit); } /****************************************************************************** * Function: StepMotorInit(byte) * * PreCondition: None * * Input: handler * * Output: None * * Side Effects: None * * Overview: This function initialices the resources that the user module needs to work, * it is called by the framework when the module is opened * * Note: None *****************************************************************************/ void StepMotorInit(byte handler) { handlerDist=handler; /* add my receive function to the handler module, to be called automatically when the pc sends data to the user module*/ setHandlerReceiveFunction(handler, &StepMotorReceived); /* initialize the send buffer, used to send data to the PC*/ sendBufferStepMotor = getSharedBuffer(handler); getPortDescriptor(handler)->change_port_direction(OUT); /*sincronizo el shift register, ya que al iniciar queda 1 bit desfazado*/ getPortDescriptor(handler)->set_data(UNO); bitEnviados=1; motorParado=FALSE; steps=2; realizarPaso(); }/*end StepMotorInit*/ /****************************************************************************** * Function: UserButtonRelease(byte i) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function release all the resources that the user module used, it is called by the framework * when the module is close * * Note: None *****************************************************************************/ void StepMotorRelease(byte handler) { unsetHandlerReceiveBuffer(handler); unsetHandlerReceiveFunction(handler); } /****************************************************************************** * Function: StepMotorReceived(byte* recBuffPtr, byte len) * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Overview: This function manages the comunication with the pc * * Note: None *****************************************************************************/ void StepMotorReceived(byte* recBuffPtr, byte len, byte handler) { byte StepMotorCounter = 0; byte stepsLow,stepsHigh; switch (((STEPPER_DATA_PACKET*) recBuffPtr)->CMD) { case READ_VERSION: ((STEPPER_DATA_PACKET*) sendBufferStepMotor)->_byte[0] = ((STEPPER_DATA_PACKET*) recBuffPtr)->_byte[0]; ((STEPPER_DATA_PACKET*) sendBufferStepMotor)->_byte[1] = STEPPER_MINOR_VERSION ; ((STEPPER_DATA_PACKET*) sendBufferStepMotor)->_byte[2] = STEPPER_MAJOR_VERSION; StepMotorCounter = 0x03; break; case SET_STEPPER: ((STEPPER_DATA_PACKET*) sendBufferStepMotor)->_byte[0] = ((STEPPER_DATA_PACKET*) recBuffPtr)->_byte[0]; StepMotorCounter = 0x01; sentido=((STEPPER_DATA_PACKET*) recBuffPtr)->_byte[1]; //velocidad=((STEPPER_DATA_PACKET*) recBuffPtr)->_byte[2]; tipoPaso=((STEPPER_DATA_PACKET*)recBuffPtr)->_byte[3]; steps=(unsigned int) (((STEPPER_DATA_PACKET*)recBuffPtr)->_byte[4]); steps=(unsigned int) ((steps << 8) | (((STEPPER_DATA_PACKET*)recBuffPtr)->_byte[5])); //dejo libre el motor enviado 0x00 if(sentido==STOP){ //steps=2 porque puede haber un paso ya iniciado steps=2; } //si el motor esta parado inicio de nuevo la rutina //si no, la rutina ya está agendada if(motorParado){ motorParado=FALSE; realizarPaso(); } break; case RESET: Reset(); break; default: break; }/*end switch(s)*/ USBGenWrite2(handler, StepMotorCounter); }/*end StepMotorReceived*/ /** EOF stepMotor2.c ***************************************************************/