00001 /*This file has been prepared for Doxygen automatic documentation generation.*/ 00024 #include <ioavr.h> 00025 #include "global.h" 00026 #include "sm_driver.h" 00027 00028 // Bit position for data in step table 00029 #define BIT_A1 3 00030 #define BIT_A2 2 00031 #define BIT_B1 1 00032 #define BIT_B2 0 00033 00035 __flash unsigned char steptab[] = {((1<<BIT_A1) | (0<<BIT_A2) | (0<<BIT_B1) | (0<<BIT_B2)), 00036 ((1<<BIT_A1) | (0<<BIT_A2) | (1<<BIT_B1) | (0<<BIT_B2)), 00037 ((0<<BIT_A1) | (0<<BIT_A2) | (1<<BIT_B1) | (0<<BIT_B2)), 00038 ((0<<BIT_A1) | (1<<BIT_A2) | (1<<BIT_B1) | (0<<BIT_B2)), 00039 ((0<<BIT_A1) | (1<<BIT_A2) | (0<<BIT_B1) | (0<<BIT_B2)), 00040 ((0<<BIT_A1) | (1<<BIT_A2) | (0<<BIT_B1) | (1<<BIT_B2)), 00041 ((0<<BIT_A1) | (0<<BIT_A2) | (0<<BIT_B1) | (1<<BIT_B2)), 00042 ((1<<BIT_A1) | (0<<BIT_A2) | (0<<BIT_B1) | (1<<BIT_B2))}; 00043 00045 int stepPosition = 0; 00046 00049 void sm_driver_Init_IO(void) 00050 { 00051 // Init of IO pins 00052 SM_PORT &= ~((1<<A1) | (1<<A2) | (1<<B1) | (1<<B2)); // Set output pin registers to zero 00053 SM_DRIVE |= ((1<<A1) | (1<<A2) | (1<<B1) | (1<<B2)); // Set output pin direction registers to output 00054 } 00055 00066 unsigned char sm_driver_StepCounter(signed char inc) 00067 { 00068 // Counts 0-1-...-6-7 in halfstep, 0-2-4-6 in fullstep 00069 static unsigned char counter = 0; 00070 // Update 00071 if(inc == CCW){ 00072 stepPosition--; 00073 } 00074 else{ 00075 stepPosition++; 00076 } 00077 00078 #ifdef HALFSTEPS 00079 if(inc){ 00080 counter++; 00081 } 00082 else{ 00083 counter--; 00084 } 00085 #else 00086 if(inc){ 00087 counter += 2; 00088 } 00089 else{ 00090 counter -= 2; 00091 } 00092 #endif 00093 00094 // Stay within the steptab 00095 counter &= 0x07; 00096 sm_driver_StepOutput(counter); 00097 return(counter); 00098 } 00099 00108 void sm_driver_StepOutput(unsigned char pos) 00109 { 00110 unsigned char temp = steptab[pos]; 00111 00112 /* 00113 // Output bit by bit 00114 if(temp&(1<<BIT_A1)) 00115 SM_PORT |= (1<<A1); 00116 else 00117 SM_PORT &= ~(1<<A1); 00118 00119 if(temp&(1<<BIT_A2)) 00120 SM_PORT |= (1<<A2); 00121 else 00122 SM_PORT &= ~(1<<A2); 00123 00124 if(temp&(1<<BIT_B1)) 00125 SM_PORT |= (1<<B1); 00126 else 00127 SM_PORT &= ~(1<<B1); 00128 00129 if(temp&(1<<BIT_B2)) 00130 SM_PORT |= (1<<B2); 00131 else 00132 SM_PORT &= ~(1<<B2); 00133 */ 00134 00135 // Output the fast way 00136 SM_PORT |= ((temp<<4)&0xF0); 00137 SM_PORT &= ((temp<<4)|0x0F); 00138 }