Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

main.c

Go to the documentation of this file.
00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00024 #include <ioavr.h> 00025 #include <inavr.h> 00026 #include <stdlib.h> 00027 #include "global.h" 00028 #include "uart.h" 00029 #include "sm_driver.h" 00030 #include "speed_cntr.h" 00031 00033 struct GLOBAL_FLAGS status = {FALSE, FALSE, 0}; 00034 00035 void ShowHelp(void); 00036 void ShowData(int position, int acceleration, int deceleration, int speed, int steps); 00037 00042 void Init(void) 00043 { 00044 // Init of IO pins 00045 sm_driver_Init_IO(); 00046 // Init of uart 00047 InitUART(); 00048 00049 // Set stepper motor driver output 00050 sm_driver_StepOutput(0); 00051 00052 // Init of Timer/Counter1 00053 speed_cntr_Init_Timer1(); 00054 00055 __enable_interrupt(); 00056 } 00057 00062 void main(void) 00063 { 00064 // Number of steps to move. 00065 int steps = 1000; 00066 // Accelration to use. 00067 int acceleration = 100; 00068 // Deceleration to use. 00069 int deceleration = 100; 00070 // Speed to use. 00071 int speed = 800; 00072 // Tells if the received string was a valid command. 00073 char okCmd = FALSE; 00074 00075 Init(); 00076 00077 // Outputs help screen. 00078 uart_SendString("\n\r"); 00079 ShowHelp(); 00080 ShowData(stepPosition, acceleration, deceleration, speed, steps); 00081 00082 while(1) { 00083 // If a command is received, check the command and act on it. 00084 if(status.cmd == TRUE){ 00085 if(UART_RxBuffer[0] == 'm'){ 00086 // Move with... 00087 if(UART_RxBuffer[1] == ' '){ 00088 // ...number of steps given. 00089 steps = atoi((char const *)UART_RxBuffer+2); 00090 speed_cntr_Move(steps, acceleration, deceleration, speed); 00091 okCmd = TRUE; 00092 uart_SendString("\n\r "); 00093 } 00094 else if(UART_RxBuffer[1] == 'o'){ 00095 if(UART_RxBuffer[2] == 'v'){ 00096 if(UART_RxBuffer[3] == 'e'){ 00097 // ...all parameters given 00098 if(UART_RxBuffer[4] == ' '){ 00099 int i = 6; 00100 steps = atoi((char const *)UART_RxBuffer+5); 00101 while((UART_RxBuffer[i] != ' ') && (UART_RxBuffer[i] != 13)) i++; 00102 i++; 00103 acceleration = atoi((char const *)UART_RxBuffer+i); 00104 while((UART_RxBuffer[i] != ' ') && (UART_RxBuffer[i] != 13)) i++; 00105 i++; 00106 deceleration = atoi((char const *)UART_RxBuffer+i); 00107 while((UART_RxBuffer[i] != ' ') && (UART_RxBuffer[i] != 13)) i++; 00108 i++; 00109 speed = atoi((char const *)UART_RxBuffer+i); 00110 speed_cntr_Move(steps, acceleration, deceleration, speed); 00111 okCmd = TRUE; 00112 uart_SendString("\n\r "); 00113 } 00114 } 00115 } 00116 } 00117 } 00118 else if(UART_RxBuffer[0] == 'a'){ 00119 // Set acceleration. 00120 if(UART_RxBuffer[1] == ' '){ 00121 acceleration = atoi((char const *)UART_RxBuffer+2); 00122 okCmd = TRUE; 00123 } 00124 } 00125 else if(UART_RxBuffer[0] == 'd'){ 00126 // Set deceleration. 00127 if(UART_RxBuffer[1] == ' '){ 00128 deceleration = atoi((char const *)UART_RxBuffer+2); 00129 okCmd = TRUE; 00130 } 00131 } 00132 else if(UART_RxBuffer[0] == 's'){ 00133 if(UART_RxBuffer[1] == ' '){ 00134 speed = atoi((char const *)UART_RxBuffer+2); 00135 okCmd = TRUE; 00136 } 00137 } 00138 else if(UART_RxBuffer[0] == 13){ 00139 speed_cntr_Move(steps, acceleration, deceleration, speed); 00140 okCmd = TRUE; 00141 } 00142 else if(UART_RxBuffer[0] == '?'){ 00143 ShowHelp(); 00144 okCmd = TRUE; 00145 } 00146 00147 // Send help if invalid command is received. 00148 if(okCmd != TRUE) 00149 ShowHelp(); 00150 00151 // Clear RXbuffer. 00152 status.cmd = FALSE; 00153 uart_FlushRxBuffer(); 00154 00155 if(status.running == TRUE){ 00156 uart_SendString("Running..."); 00157 while(status.running == TRUE); 00158 uart_SendString("OK\n\r"); 00159 } 00160 00161 ShowData(stepPosition, acceleration, deceleration, speed, steps); 00162 }//end if(cmd) 00163 }//end while(1) 00164 } 00165 00167 __flash char Help[] = {"\n\r--------------------------------------------------------------\n\rAtmel AVR446 - Linear speed control of stepper motor\n\r\n\r? - Show help\n\ra [data] - Set acceleration (range: 71 - 32000)\n\rd [data] - Set deceleration (range: 71 - 32000)\n\rs [data] - Set speed (range: 12 - motor limit)\n\rm [data] - Move [data] steps (range: -64000 - 64000)\n\rmove [steps] [accel] [decel] [speed]\n\r - Move with all parameters given\n\r<enter> - Repeat last move\n\r\n\r acc/dec data given in 0.01*rad/sec^2 (100 = 1 rad/sec^2)\n\r speed data given in 0.01*rad/sec (100 = 1 rad/sec)\n\r--------------------------------------------------------------\n\r"}; 00168 00173 void ShowHelp(void) 00174 { 00175 unsigned int i = 0; 00176 while(Help[i] != 0) 00177 uart_SendByte(Help[i++]); 00178 } 00179 00190 void ShowData(int position, int acceleration, int deceleration, int speed, int steps) 00191 { 00192 uart_SendString("\n\r Motor pos: "); 00193 uart_SendInt(position); 00194 uart_SendString(" a:"); 00195 uart_SendInt(acceleration); 00196 uart_SendString(" d:"); 00197 uart_SendInt(deceleration); 00198 uart_SendString(" s:"); 00199 uart_SendInt(speed); 00200 uart_SendString(" m:"); 00201 uart_SendInt(steps); 00202 uart_SendString("\n\r> "); 00203 } 00204

Generated on Mon May 8 15:05:03 2006 for AVR446 - Linear speed control of stepper motor by doxygen 1.3.7