/** * Gets the X, Y and Z values of the accelerometer in the KL25Z board and * sends the data to the UART peripheral if debug mode is active. *************************************************************************** */ void accelerometer_readXYZ(void) { uint8 registerRead = MMA8451_OUT_X_MSB; int8 xyz[3] = {0, 1, 2}; GI2C1_ReadAddress(MMA8451_I2C_ADDRESS, ®isterRead, 1, (uint8_t*)&xyz, 3); #ifdef DEBUGFLAG uart_SendString("X: "); uart_SendByte(xyz[0]); uart_SendString("; Y: "); uart_SendByte(xyz[1]); uart_SendString("; Z: "); uart_SendByte(xyz[2]); uart_SendStringLn(";"); #endif }
/*! \brief Sends out data. * * Outputs the values of the data you can control by serial interface * and the current position of the stepper motor. * * \param acceleration Accelration setting. * \param deceleration Deceleration setting. * \param speed Speed setting. * \param steps Position of the stepper motor. */ void ShowData(int position, int acceleration, int deceleration, int speed, int steps) { uart_SendString("\n\r Motor pos: "); uart_SendInt(position); uart_SendString(" a:"); uart_SendInt(acceleration); uart_SendString(" d:"); uart_SendInt(deceleration); uart_SendString(" s:"); uart_SendInt(speed); uart_SendString(" m:"); uart_SendInt(steps); uart_SendString("\n\r> "); }
int main(void) { USART_Init(MYUBBR); //initialise UART0 uart_SendString("Set Up...\n\r"); speed_cntr_Init_Timer1(); sm_driver_Init_IO(); XLocation = 0; YLocation = 0; ZLocation = 0; sei(); DDRA = 0xFF;//set port c to outputs PORTA = 0x00; DDRB = 0xFF; PORTB = 0x00; DDRC = 0xFF; PORTC = 0x00; DDRD = 0xFF; PORTD = 0x00; int Step; unsigned int accel, decel, speed; Step = 100; accel = 71; speed = 12; decel = 71; //int point = 0; uart_SendString("Ready!\n\r"); while(1) { if(COMMANDRECEIVED == TRUE) { switch(Received[0]) { case 'G': switch(Received[1]) { case '0': switch(Received[2]) { case '0': G00(); break; default: uart_SendString("Unsupported G Code"); break; } break; default: uart_SendString("Unsupported G Code"); break; } break; case 'g': //start all set up motors. StartMotors(); break; case 'X': //Set up the X motor with current settings speed_cntr_SetupX(Step, accel, decel, speed); break; case 'Y': //set up the Y motor with current settings speed_cntr_SetupY(Step, accel, decel, speed); break; case 'Z': //set up the Z motor with current settings speed_cntr_SetupZ(Step, accel, decel, speed); break; case 'S'://set the Step variable Step = atoi((char const *)Received+2); break; case 'a'://set the Accel Variable accel = atoi((char const *)Received+2); break; case 'd'://set up the deceleration variable decel = atoi((char const *)Received+2); break; case 's'://set the steps variable speed = atoi((char const *)Received+2); //speed_cntr1_Setup1(Step, accel, decel, speed); if (speed > MAXSPEED) { uart_SendString("WARNING: EXCEEDED MAXIMUM SPEED"); } break; case 'I'://show all variable information uart_SendString("\n\rSteps: "); uart_SendInt(Step); uart_SendString("\n\rAccel: "); uart_SendInt(accel); uart_SendString("\n\rSpeed: "); uart_SendInt(speed); uart_SendString("\n\rDecel: "); uart_SendInt(decel); break; case 'L'://show current locations of device USART_Transmit('X'); USART_Transmit('='); uart_SendInt(XLocation); USART_Transmit('\r'); USART_Transmit('\n'); USART_Transmit('Y'); USART_Transmit('='); uart_SendInt(YLocation); USART_Transmit('\r'); USART_Transmit('\n'); USART_Transmit('Z'); USART_Transmit('='); uart_SendInt(ZLocation); USART_Transmit('\r'); USART_Transmit('\n'); break; case '0'://Zero all Variables. XLocation = 0; YLocation = 0; ZLocation = 0; break; default://unsupported command uart_SendString("Incorrect Command"); break; } COMMANDRECEIVED = FALSE; FlushBuffer(); uart_SendString("\n\r>"); } else { PORTA = PORTA; } } }
/*! \brief Demo of linear speed controller. * * Serial interface frontend to test linear speed controller. */ void main(void) { // Number of steps to move. int steps = 1000; // Accelration to use. int acceleration = 100; // Deceleration to use. int deceleration = 100; // Speed to use. int speed = 800; // Tells if the received string was a valid command. char okCmd = FALSE; Init(); // Outputs help screen. uart_SendString("\n\r"); ShowHelp(); ShowData(stepPosition, acceleration, deceleration, speed, steps); while(1) { // If a command is received, check the command and act on it. if(status.cmd == TRUE){ if(UART_RxBuffer[0] == 'm'){ // Move with... if(UART_RxBuffer[1] == ' '){ // ...number of steps given. steps = atoi((char const *)UART_RxBuffer+2); speed_cntr_Move(steps, acceleration, deceleration, speed); okCmd = TRUE; uart_SendString("\n\r "); } else if(UART_RxBuffer[1] == 'o'){ if(UART_RxBuffer[2] == 'v'){ if(UART_RxBuffer[3] == 'e'){ // ...all parameters given if(UART_RxBuffer[4] == ' '){ int i = 6; steps = atoi((char const *)UART_RxBuffer+5); while((UART_RxBuffer[i] != ' ') && (UART_RxBuffer[i] != 13)) i++; i++; acceleration = atoi((char const *)UART_RxBuffer+i); while((UART_RxBuffer[i] != ' ') && (UART_RxBuffer[i] != 13)) i++; i++; deceleration = atoi((char const *)UART_RxBuffer+i); while((UART_RxBuffer[i] != ' ') && (UART_RxBuffer[i] != 13)) i++; i++; speed = atoi((char const *)UART_RxBuffer+i); speed_cntr_Move(steps, acceleration, deceleration, speed); okCmd = TRUE; uart_SendString("\n\r "); } } } } } else if(UART_RxBuffer[0] == 'a'){ // Set acceleration. if(UART_RxBuffer[1] == ' '){ acceleration = atoi((char const *)UART_RxBuffer+2); okCmd = TRUE; } } else if(UART_RxBuffer[0] == 'd'){ // Set deceleration. if(UART_RxBuffer[1] == ' '){ deceleration = atoi((char const *)UART_RxBuffer+2); okCmd = TRUE; } } else if(UART_RxBuffer[0] == 's'){ if(UART_RxBuffer[1] == ' '){ speed = atoi((char const *)UART_RxBuffer+2); okCmd = TRUE; } } else if(UART_RxBuffer[0] == 13){ speed_cntr_Move(steps, acceleration, deceleration, speed); okCmd = TRUE; } else if(UART_RxBuffer[0] == '?'){ ShowHelp(); okCmd = TRUE; } // Send help if invalid command is received. if(okCmd != TRUE) ShowHelp(); // Clear RXbuffer. status.cmd = FALSE; uart_FlushRxBuffer(); if(status.running == TRUE){ uart_SendString("Running..."); while(status.running == TRUE); uart_SendString("OK\n\r"); } ShowData(stepPosition, acceleration, deceleration, speed, steps); }//end if(cmd) }//end while(1) }