/*
把P0.0定义K1,P0.1定义K2, P0.3定义K4,P1.0定义Y0,P1.1定义Y1,单片机低电平有效。
K1光电开关(为触发开关)检测到物体时输出低电平,K2(检测开关)检测到物体时高电平,K4(检测开关)检测到物体时是低电平,Y0外接电机,Y1外接继电器。
1、 在开机状态下,K1没有检测到物体(没有触发),K2没有检测到物体,K4没有检测到物体,Y0为高电平,Y1为高电平。
2、 K1没有检测到物体为高电平,K2没有检测到物体为低电平,K4检测到物体为低电平。
Y0为高电平,Y1为低电平
3、 K1检测到物体(触发)为低电平,K2没有检测到物体为低电平,K4检测到物体为低电平,Y0为输出低电平,Y1输出高电平。
4、 K1没有检测到物体(没有触发)为高电平,K4检测到物体为低电平,当K2检测到物体为高电平,当K2从高电平变成低电平时,Y0输出为高电平,Y1输出为低电平。
5、 当K4没有检测到物体时,Y0延时3秒变为高电平,Y1为高电平。
*/
#include
sbit K1 = P0^0; // 0
sbit K2 = P0^1; // 1
sbit K4 = P0^3; // O
sbit Y0 = P1^0; // MOTOR
sbit Y1 = P1^1; // SWITH
bit flag_have_K4 = 0;
bit falg_have_K2 = 0;
void delayms(int tms)
{
char i;
while(tms--)
{
i = 110;
while(i--);
}
}
void main (void){
unsigned char Y_status = 0;
Y0 = 1;
Y1 = 1;
while(1){
if(!K4) // 测到
{
flag_have_K4 = 1;
if(K1) ////4 011 --> 2 001
{
//Y0 = 1;
//Y1 = 0;
if(K2)
{
falg_have_K2 = 1;
}
if(!K2&&falg_have_K2)
{
falg_have_K2 = 0;
Y_status = 2;
}
}
if(!K1&&!K2) //3 101
{
//Y0 = 0;
//Y1 = 1;
Y_status = 3;
}
}
else
{
if(K1&&!K2) //1 000
{
Y_status = 1;
}
if(flag_have_K4)
{
flag_have_K4 = 0;
Y_status =4;
}
}
if(1 == Y_status )
{
Y0 = 1;
Y1 = 1;
Y_status = 0;
}
else if(2 == Y_status)
{
Y0 = 1;
Y1 = 0;
Y_status = 0;
}
else if(3 == Y_status)
{
Y0 = 0;
Y1 = 1;
Y_status = 0;
}
else if(4 == Y_status)
{
delayms(3000);
Y0 = 1;
Y1 = 1;
Y_status = 0;
}
else
{
Y_status = 0;
}
}
}
你这个连张电路图也没,什么单片机也不知道,人家怎么可能给你写个程序。
这个程序是要照你的要求初始化端口,用KEIL3检测通过,你参照一下:
#include<单片机头文件.h>
sbit k1=P0^0;
sbit k2=P0^1;
sbit k4=P0^3;
sbit y0=P1^0;
sbit y1=P1^1;
void delay()
{
unsigned char m,n;
for(m=255;m>1;m--)
for(n=255;n>1;n--)
;
}
void main()
{
k1=1;
k2=0;
k4=1;
y0=1;
y1=1;
delay();
}