#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit cq2 = P1^0; //控制前后运动的电磁阀
sbit cq1 = P1^1; //控制左右运动的下面电磁阀
sbit cq3 = P1^2; //控制上下运动的电磁阀
sbit cq4 = P1^3; //控制左右运动的上面电磁阀
sbit s1 = P1^4; //第一步按钮开关
sbit s2 = P1^5; //第二步按钮开关
sbit s3 = P1^6; //第三步按钮开关
sbit err = P1^7; //错误判断位
sbit err2 = P3^0;
sbit k1 = P3^3; //左右方向下面的磁感应开关
sbit k2 = P3^2; //左右方向上面的磁感应开关
sbit k3 = P3^1; //前后方向磁感应开关
#define OneStep 1 //第一步
#define TwoStep 2 //第二步
#define ThreeStep 3 //第三步
bit EnableTwoStepFlag = 0; //第二步工作允许标志
bit flag = 0; //定时时间标志
uint count = 0; //
uchar vola
tile Status = OneStep;
void Timer0Init()
{
TMOD = 0x21;
TH0 = 0xd8;
TL0 = 0xf0;
TR0 = 1;
ET0 = 1;
}
void Timer0()interrupt 1 using 1
{
TH0 = 0xd8;
TL0 = 0xf0;
TR0 = 1;
count++;
if(count==200)
{
count = 0;
flag = 1;
}
}
void WaitTime(uchar dat)
{
uchar i = 0;
for( i = 0;i<dat;i++)
{
while(flag == 0);
flag = 0;
}
}
void OneStepWork()
{
s1 = 1;
while(s1 == 1); //等待S1 =0,按钮1按下
s1 = 1;
cq1 = 0; //打开左右向下面电磁阀,推动气缸工作
k1 = 1;
while(k1 == 1); //等待左右向下面磁感应开关到位
k1 = 1;
cq2 = 0; //打开控制前后向电磁阀,推动前后气缸工作
k3 = 1;
while(k3 == 1)
{
_nop_(); //等待前后向磁感应开关到位。
}
k3 = 1;
cq3 = 0; //打开上下向电磁阀,推动气缸向下运动
WaitTime(3); //等待1分钟
err = 1;
err2 = 1;
if( err == 1 && err2 == 1) //判断错误表示. err=1正确,err=0错误
{
EnableTwoStepFlag = 1;
cq3 = 1;
WaitTime(2);
cq2 = 1;
}
else
{
cq3 = 1;
WaitTime(2);
cq2 = 1;
WaitTime(2);
cq1 = 1;
}
}
void TwoStepWork()
{
s2 = 1;
while(s2 == 1);
s2 = 1;
cq4 = 0;
k2 = 1;
while(k2 == 1);
k2 = 1;
}
void ThreeStepWork()
{
s3 = 1;
while(s3 == 1);
s3 = 1;
cq4 = 0;
WaitTime(1);
cq1 = 0;
}
void main()
{
Timer0Init();
EA = 1;
while(1)
{
switch(Status)
{
case OneStep:
OneStepWork();
if(EnableTwoStepFlag)
{
EnableTwoStepFlag = 0;
Status = TwoStep;
}
else
{
Status = OneStep;
}
break;
case TwoStep:
TwoStepWork();
Status = ThreeStep;
break;
case ThreeStep:
ThreeStepWork();
Status = OneStep;
break;
default:
break;
}
}
}
为什么按s2不起作用啊
程序修改不判断错误,还是进不了第二个条件。
一周热门 更多>