简要说明:P1.4连接到一个按键,低电平有效;P0.0,P0.1,P0.2,P0.3通过74HC14后再通过ULN2003A驱动伺服电机
问题:为什么程序一运行到P0 = 0X00后就跑不动了,为什么按键标志位作为判断标准却没有反映,请大虾指教下:
程序一:
#include <reg51.h>
#define uchar unsigned char
sbit key_input = P1^4;
/*************************************************/
void delay(uchar i)
{
uchar j;
for( i; i>0; i-- )
for ( j=255; j>0; j-- )
;
}//延时子程序
/*************************************************/
char key_scan(void)
{
char key_flag = 0;
while(1)
{
if (key_input == 0)
{
delay(100);
if (key_input == 0)
{
key_flag = 1;
}
while(!key_input);
delay(100);
while(!key_input);
}
}
return key_flag;
}
/*************************************************/
void main()
{
P0 = 0x00;
key_input = 1;
while(1)
{
if( key_scan() )
{
P0 = 0xfc;
delay(255);
P0 = 0xf9;
delay(255);
P0 = 0xf3;
delay(255);
P0 = 0xf6;
delay(255);
}
else
{
P0 = 0xfc;
delay(255);
P0 = 0xf6;
delay(255);
P0 = 0xf3;
delay(255);
P0 = 0xf9;
delay(255);
}
}
}
程序二:
#include <reg51.h>
#define uchar unsigned char
sbit key_input = P1^4;
/*************************************************/
void delay(uchar i)
{
uchar j;
for( i; i>0; i-- )
for ( j=255; j>0; j-- )
;
}//延时子程序
/*************************************************/
void main()
{
P0 = 0x00;
key_input = 1;
while(1)
{
if( key_input==1 )
{
P0 = 0xfc;
delay(255);
P0 = 0xf9;
delay(255);
P0 = 0xf3;
delay(255);
P0 = 0xf6;
delay(255);
}
else
{
P0 = 0xfc;
delay(255);
P0 = 0xf6;
delay(255);
P0 = 0xf3;
delay(255);
P0 = 0xf9;
delay(255);
}
}
} |