驱动方式: 单相励磁 1-2相励磁 2相励磁 Step Motor Drive Mode:
Mode 1: 2-2 Phase Pluse Sequence ---------------------------------------------------------- Wrie color Brown Orange Yellow Black A+ A- B+ B- Clockwise 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 CouterClockwise 1 0 1 0 0 1 1 0 0 1 0 1 1 0 0 1 ---------------------------------------------------------- Mode 2: 1 Phase Pluse Sequence Clockwise 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 1 CouterClockwise 1 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 ---------------------------------------------------------- Mode 3: 1-2 Phase Pluse Sequence
Clockwise 1 0 0 0 1 0 1 0 0 0 1 0 0 1 1 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 0 1 CounterClockwise 1 0 0 0 1 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 0 0 1 0 1 0 1 0 1 0 0 0 ***********************************************************************************/
#include "includes.h"
#define MODE1 1 #define MODE2 2 #define MODE3 3 #define Clockwise 0 #define Counterclockwise 1
sbit LED = P1^4; sbit SPM0 = P1^0; sbit I0 = P1^1; sbit SPM1 = P1^2; sbit I1 = P1^3;
/********************************************************************************/ //function Protypes void System_Initial(void); void MotorDriver_Initial(void); INT8U MotorRun(INT16U StartPPS, INT16U EndPPS, INT8U DriveMode, INT8U direction); void StepPhasing(void);
//Global variables INT8U mode; INT8U dir; INT8U phase; INT16U cnt_start; INT16U cnt_end;
void main(void) { MotorDriver_Initial(); System_Initial(); if(MotorRun(100, 110, MODE3, Counterclockwise)==0) LED = 0; P1 = 0; while(1) { }
}
/********************************************************************************/ //System Resource Inital void System_Initial(void) { //config Timer0 EA = 1; ET0 = 1; TMOD = 1; } /******************************************************************************** name: MotorDriver_Initial desc: Initial the driver pars: no rets: no **********************************************************************************/ void MotorDriver_Initial(void) { I0 = 0; I1 = 0; SPM0 = 0; SPM1 = 0; LED = 1;
}
/********************************************************************************* name: MotorRun desc: config the motor mode and its pars pars: StartPPS, EndPPS, Mode, Direction rets: state **********************************************************************************/ INT8U MotorRun(INT16U StartPPS, INT16U EndPPS, INT8U DriveMode, INT8U direction) {
if(StartPPS >1000 || StartPPS <100) return 0; if(EndPPS <100 || EndPPS > 2500) return 0;
cnt_start = 65535 - 1000000/StartPPS; cnt_end = 65535 - 1000000/EndPPS; TH0 = cnt_start/256; TL0 = cnt_start%256;
mode = DriveMode; dir = direction; if(direction==Clockwise) phase = 1; if(direction==Counterclockwise) if(mode!=3) phase = 4; else phase = 8;
TR0 = 1; return 1; }
/*************************************************************************************/ //Timer0 interrupt isr void Timer0_isr(void) interrupt 1 using 0 { TR0 = 0; TF0 = 0;
if(cnt_start!=cnt_end) { cnt_start += 1; TH0 = cnt_start/256; TL0 = cnt_start%256; } else { TH0 = cnt_end/256; TL0 = cnt_end%256; } StepPhasing(); TR0 = 1; } /************************************************************************************* name: StepPhasing desc: motor step phasing according to Run Mode pars: no rets: no **************************************************************************************/ void StepPhasing(void) { if(mode==MODE1) { I0 = 1; I1 = 1; switch (phase) { case 1: phase = 2 + (dir<<1); SPM0 = 1; SPM1 = 1; break; case 2: phase = 3 - (dir<<1); SPM0 = 1; SPM1 = 0; break; case 3: phase = 4 - (dir<<1); SPM0 = 0; SPM1 = 0; break; case 4: phase = 1 + (dir<<1); SPM0 = 0; SPM1 = 1; break; default : break; } } else if(mode==MODE2) switch (phase) { case 1: phase = 2 + (dir<<1); //disable coil B, current from A+ ---->A- I1 = 0; SPM1 = 0; SPM0 = 1; I0 = 1; break; case 2: phase = 3 - (dir<<1); //disable coil A, current from B+ ---->B- I0 = 0; SPM0 = 0; I1 = 1; SPM1 = 1; break; //disable coil B, current from A- ----->A+ case 3: phase = 4 - (dir<<1); I1 = 0; SPM1 = 0; I0 = 1; SPM0 = 0; break; //disable coil A, current from B- ----->B+ case 4: phase = 1 + (dir<<1); I0 = 0; SPM0 = 0; I1 = 1; SPM1 = 0; break; default : break; }
else if(mode==MODE3) switch (phase) { case 1: phase = 2 + dir*6; I1 = 0; SPM1 = 0; I0 = 1; SPM0 = 1; break; case 2: phase = 3 - (dir<<1); I0 = 1; SPM0 = 1; I1 = 1; SPM1 = 1; break; case 3: phase = 4 - (dir<<1); I0 = 0; SPM0 = 0; I1 = 1; SPM1 = 1; break; case 4: phase = 5 - (dir<<1); I0 = 1; SPM0 = 0; I1 = 1; SPM1 = 1; break; case 5: phase = 6 - (dir<<1); I1 = 0; SPM1 = 0; I0 = 1; SPM0 = 0; break; case 6: phase = 7 - (dir<<1); I0 = 1; SPM0 = 0; I1 = 1; SPM1 = 0; break; case 7: phase = 8 - (dir<<1); I0 = 0; SPM0 = 0; I1 = 1; SPM1 = 0; break; case 8: phase = 1 + dir*6; I0 = 1; SPM0 = 1; I1 = 1; SPM1 = 0; break; default : break; }
}
请教下,在将启动频率PPS加载到定时器时,怎样避免 cnt_start = 65535 - 1000000/StartPPS; 中的除法运算,这样的语句在51中执行的太慢还站代码空间
|