📄 motor.c
字号:
//------12M Hz,AT89C51-----
#include <at89x51.h>
#include <motor.h>
void init_ports()
{
P0=0x07;
P1=0x00;
P2=0x00;
P3=0x00;
}
void init_297()
{
DR1=CCW;
DR2=CCW;
RST1=1;
RST2=1;
RST1=0;
RST2=0;
Clk_Delay();
RST1=1;
RST2=1;
EN1=DISABLE;
EN2=DISABLE;
HF1=1;
HF2=1;
M1_BUSY=0;
M2_BUSY=0;
}
void init_device()
{
EA=0;
init_ports();
init_297();
TMOD=0x22;//timer,auto-reload mode.
TL0=0x9B;
TH0=0x9B;
TL1=0x9B;
TH1=0x9B;
ET0=1;
ET1=1;
TR0=1;
TR1=1;
EA=1;
}
void int_t0(void) interrupt TF0_VECTOR using 1
{
if(step_count1==0)
{
M1_BUSY=0;
motor1_stop();
return;
}
else
{
M1_BUSY=1;
}
speed_count1++;
if(speed_count1>=speed_set1)
{
speed_count1=0;
CLK1_CLR;
Clk_Delay();
CLK1_SET;
if(speed_set1!=FOREVER_RUN)
{
step_count1--;
}
}
}
void int_t1(void) interrupt TF1_VECTOR using 2
{
if(step_count2==0)
{
M2_BUSY=0;
motor2_stop();
return;
}
else
{
M2_BUSY=1;
}
speed_count2++;
if(speed_count2>=speed_set2)
{
speed_count2=0;
CLK2_CLR;
Clk_Delay();
CLK2_SET;
if(speed_set2!=FOREVER_RUN)
step_count2--;
}
}
void motor_run(bit Motor_Select,bit Direction,uint speed,uint step)
{
if(!Motor_Select)
{
if(M1_BUSY){return;}
EN1=ENABLE;
DR1=Direction;
speed_set1=speed;
step_count1=step;
//return TURE;
}
else
{
if(M2_BUSY){return;}
EN2=ENABLE;
DR2=Direction;
speed_set2=speed;
step_count2=step;
//return TURE;
}
}
void motor1_stop()
{
M1_BUSY=0;
EN1=DISABLE;
step_count1=0;
}
void motor2_stop()
{
M2_BUSY=0;
EN2=DISABLE;
step_count2=0;
}
void motor_status()
{
if(M1_BUSY)
LED_M1=0;
else
LED_M1=1;
if(M2_BUSY)
LED_M2=0;
else
LED_M2=1;
}
main()
{
init_device();
motor_run(M1,CW,5000,2000);
motor_run(M2,CCW,500,2000);
while(1)
{
motor_status();
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -