00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "..\inc\includes.h"
00011 #include "..\os_core\os_includes.h"
00012
00013 #define cEventMotorCcw 0
00014 #define cEventMotorCw 1
00015 #define cEventMotorStop 2
00016
00017 #define MOTOR_COMMAND_MAX 3
00018 INT8U code cw[3]={'c','w',0};
00019 INT8U code ccw[4]={'c','c','w',0};
00020 INT8U code stop[5]={'s','t','o','p',0};
00021 INT8U code *MotorCommandList[MOTOR_COMMAND_MAX]={cw,ccw,stop};
00022
00023 struct STEPMOTOR_CONTROL StepMotorCtrl;
00024 struct MOTOR_EVENT MotorEvent;
00025 sbit MotorPhase_B= P1^0;
00026 sbit MotorPhase_BX=P1^1;
00027 sbit MotorPhase_AX=P1^2;
00028 sbit MotorPhase_A= P1^3;
00029
00030 void F_MotorPowerOnInit(void) LargeReentrant;
00031 void F_MotorSvc(void) LargeReentrant;
00032
00033 void F_MotorPowerOnInit(void) LargeReentrant{
00034 MotorPhase_A =High;
00035 MotorPhase_AX=High;
00036 MotorPhase_B =High;
00037 MotorPhase_BX=High;
00038 MotorEvent.HasEvent=0;
00039 MotorEvent.EventType=0;
00040 MotorEvent.RwFlag=0;
00041 MotorEvent.Data=0;
00042 StepMotorCtrl.State=StepMotorCw0State;
00043 StepMotorCtrl.Timer=MotorStepTime;
00044 }void F_MotorSvc(void) LargeReentrant{
00045 if(MotorEvent.HasEvent==1){
00046 if(MotorEvent.EventType==cEventMotorCw){
00047 switch(StepMotorCtrl.State){
00048 case StepMotorRcw0State:
00049 StepMotorCtrl.State=StepMotorCw1State;
00050 break;
00051 case StepMotorRcw1State:
00052 StepMotorCtrl.State=StepMotorCw0State;
00053 break;
00054 case StepMotorRcw2State:
00055 StepMotorCtrl.State=StepMotorCw3State;
00056 break;
00057 case StepMotorRcw3State:
00058 StepMotorCtrl.State=StepMotorCw2State;
00059 break;
00060 }
00061 }
00062 else if(MotorEvent.EventType==cEventMotorCcw){
00063 switch(StepMotorCtrl.State){
00064 case StepMotorCw0State:
00065 StepMotorCtrl.State=StepMotorRcw1State;
00066 break;
00067 case StepMotorCw1State:
00068 StepMotorCtrl.State=StepMotorRcw0State;
00069 break;
00070 case StepMotorCw2State:
00071 StepMotorCtrl.State=StepMotorRcw3State;
00072 break;
00073 case StepMotorCw3State:
00074 StepMotorCtrl.State=StepMotorRcw2State;
00075 break;
00076 }
00077 }
00078 else{
00079 StepMotorCtrl.State=StepMotorIdleState;
00080 }
00081 MotorEvent.HasEvent=0;
00082 }
00083 switch(StepMotorCtrl.State){
00084 case StepMotorIdleState:
00085 break;
00086 case StepMotorCw0State:
00087 MotorPhase_BX=Low;
00088 MotorPhase_AX=Low;
00089 MotorPhase_B =High;
00090 MotorPhase_A =High;
00091 StepMotorCtrl.State=StepMotorCw1State;
00092 break;
00093 case StepMotorCw1State:
00094 MotorPhase_BX=Low;
00095 MotorPhase_AX=High;
00096 MotorPhase_B =High;
00097 MotorPhase_A =Low;
00098 StepMotorCtrl.State=StepMotorCw2State;
00099 break;
00100 case StepMotorCw2State:
00101 MotorPhase_BX=High;
00102 MotorPhase_AX=High;
00103 MotorPhase_B =Low;
00104 MotorPhase_A =Low;
00105 StepMotorCtrl.State=StepMotorCw3State;
00106 break;
00107 case StepMotorCw3State:
00108 MotorPhase_BX=High;
00109 MotorPhase_AX=Low;
00110 MotorPhase_B =Low;
00111 MotorPhase_A =High;
00112 StepMotorCtrl.State=StepMotorCw0State;
00113 break;
00114 case StepMotorRcw0State:
00115 MotorPhase_BX=High;
00116 MotorPhase_AX=Low;
00117 MotorPhase_B =Low;
00118 MotorPhase_A =High;
00119 StepMotorCtrl.State=StepMotorRcw1State;
00120 break;
00121 case StepMotorRcw1State:
00122 MotorPhase_BX=High;
00123 MotorPhase_AX=High;
00124 MotorPhase_B =Low;
00125 MotorPhase_A =Low;
00126 StepMotorCtrl.State=StepMotorRcw2State;
00127 break;
00128 case StepMotorRcw2State:
00129 MotorPhase_BX=Low;
00130 MotorPhase_AX=High;
00131 MotorPhase_B =High;
00132 MotorPhase_A =Low;
00133 StepMotorCtrl.State=StepMotorRcw3State;
00134 break;
00135 case StepMotorRcw3State:
00136 MotorPhase_BX=Low;
00137 MotorPhase_AX=Low;
00138 MotorPhase_B =High;
00139 MotorPhase_A =High;
00140 StepMotorCtrl.State=StepMotorRcw0State;
00141 break;
00142 }
00143 }
00144
00145 INT8U F_MotorEventCheck(void) LargeReentrant{
00146 if(MotorEvent.HasEvent==1) return 1;
00147 else return 0;
00148 }
00149 void F_MotorDirectionSet(INT8U Direction)LargeReentrant {
00150 if(Direction==0){
00151 OS_ENTER_CRITICAL();
00152 MotorEvent.EventType=cEventMotorCw;
00153 MotorEvent.RwFlag=0;
00154 MotorEvent.Data=0;
00155 MotorEvent.HasEvent=1;
00156 OS_EXIT_CRITICAL();
00157 LcmChar[27]=' ';
00158 LcmChar[28]='C';
00159 LcmChar[29]='W';
00160 LcmChar[30]=' ';
00161 }
00162 else if(Direction==1){
00163 OS_ENTER_CRITICAL();
00164 MotorEvent.EventType=cEventMotorCcw;
00165 MotorEvent.RwFlag=0;
00166 MotorEvent.Data=0;
00167 MotorEvent.HasEvent=1;
00168 OS_EXIT_CRITICAL();
00169 LcmChar[27]='C';
00170 LcmChar[28]='C';
00171 LcmChar[29]='W';
00172 LcmChar[30]=' ';
00173 }
00174 else if(Direction==2){
00175 OS_ENTER_CRITICAL();
00176 MotorEvent.EventType=cEventMotorStop;
00177 MotorEvent.RwFlag=0;
00178 MotorEvent.Data=0;
00179 MotorEvent.HasEvent=1;
00180 OS_EXIT_CRITICAL();
00181 LcmChar[27]='S';
00182 LcmChar[28]='T';
00183 LcmChar[29]='O';
00184 LcmChar[30]='P';
00185 }
00186 }
00187 void F_MotorCommandSet(INT8U **pArgv,INT8U Argc) LargeReentrant{
00188 INT8U i;
00189
00190 if(Argc==0){
00191 printf("\n no command param");
00192 return;
00193 }
00194 for(i=0;i<MOTOR_COMMAND_MAX;i++){
00195 if(strcmp(pArgv[0],MotorCommandList[i])==0) break;
00196 }
00197 if(i==MOTOR_COMMAND_MAX) printf("\n motor command param invalid");
00198 else{
00199 printf("\n motor command param %s ",MotorCommandList[i]);
00200 F_MotorDirectionSet(i);
00201 }
00202 }