這樣就可以啦!我已試過OK
#include <reg52.h>
sbit s2=P3^4;//停止
sbit s4=P3^6;//正
sbit s5=P3^7;//反
sbit km1=P1^3;
sbit km2=P1^5;
void delay();
void main()
{
km1=1;
km2=1;
while(1){
if (s2==0)
{
delay();
if(s2==0)
{
km1=1;
km2=1;
} }
if (s4==0)
{
delay();
if(s4==0)
{
km1=0;
km2=1;
}}
if (s5==0)
{
delay();
if(s5==0)
{
km1=1;
km2=0;
}}
}
}
void delay(void) //誤差 0us,延時50微秒
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=22;a>0;a--);
}