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