#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
void Delay_Ms(uint ms);
uchar Busy_Check();
void Initialize_LCD();
void Write_LCD_Command(uchar cmd);
void Write_LCD_Data(uchar dat);
void Display_String(uchar x,uchar y,uchar *str);
void Display_Clear();
sbit K1=P2^7;
sbit K2=P3^0;
sbit K3=P3^1;
sbit K4=P3^6;
sbit K5=P3^7;
sbit Forward_Flag=P2^3;
sbit Backward_Flag=P2^4;
sbit Stop_Flag=P2^5;
uchar code display_run_model_forward[]={"Forward"};
uchar code display_run_model_backward[]={"Backward"};
uchar code display_run_model_stop[]={"Stop"};
uchar display_run_number[]={" 0.00s/r "};
uchar display[]={"Dec speed: "};
uchar display1[]={"Add speed: "};
uchar setting[]={"Setting: "};
uchar kong[]={" "};
uchar code step_code_forward[]={0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09};
uchar code step_code_backward[]={0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01};
uchar m=30;
uchar N;
void Step_Motor_Forward(uchar n)
{
uchar i,j;
while(n)
{
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
if(K3==0) break;
P1=step_code_forward[j];
Delay_Ms(m);
}
display_run_number[0]=n/10+'0';
display_run_number[1]=n%10+'0';
display_run_number[5]=m*40/1000+'0';
display_run_number[7]=m*40/100%10+'0';
display_run_number[8]=m*40/10%10+'0';
Display_String(0,1,display_run_number);
}
n--;
}
}
void Step_Motor_Backward(uchar n)
{
uchar i,j;
while(n)
{
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
if(K3==0)
break;
P1=step_code_backward[j];
Delay_Ms(m);
}
display_run_number[0]=n/10+'0';
display_run_number[1]=n%10+'0';
display_run_number[5]=m*40/1000+'0';
display_run_number[7]=m*40/100%10+'0';
display_run_number[8]=m*40/10%10+'0';
Display_String(0,1,display_run_number);
}
n--;
}
}
void main()
{
EA=1;
EX0=1;
N=20;
Initialize_LCD();
while(1)
{
if(K1==0)
{
Stop_Flag=1;
Forward_Flag=0;
Display_String(0,0,display_run_model_forward);
Step_Motor_Forward(N);
Forward_Flag=1;
Display_Clear();
if(K3==0)
break;
}
else if(K2==0)
{
Stop_Flag=1;
Backward_Flag=0;
Display_String(0,0,display_run_model_backward);
Display_String(0,1,display_run_number);
Step_Motor_Backward(N);
Display_Clear();
Backward_Flag=1;
if(K3==0)
break;
}
else
{
Stop_Flag=0;
Display_String(0,0,display_run_model_stop);
Display_String(0,1,kong);
P1=0x00;
}
}
}
void inter()interrupt 0
{
Delay_Ms(100);
if(K4==0)
{
Delay_Ms(100);
m++;
if(m==99)
m=0;
Delay_Ms(100);
display[12]=m/10%10+'0';
display[13]=m%10+'0';
Delay_Ms(100);
Display_String(0,0,setting);
Display_String(0,1,display);
}
if(K5==0)
{
Delay_Ms(100);
m--;
if(m==0)
m=99;
Delay_Ms(100);
display1[12]=m/10%10+'0';
display1[13]=m%10+'0';
Delay_Ms(100);
Display_String(0,0,setting);
Display_String(0,1,display1);
}
}