|
先總結(jié)說明下:
第一個(gè)程序,來著開發(fā)板自帶程序,可以正常左右循環(huán);第二個(gè)程序 只是取消了 uchar LED 這個(gè)變量,直接賦值P2 端口,就不行了!
不知道為什么? 求指點(diǎn)下, 是不是哪個(gè)概念沒理解清楚?
這個(gè)程序是正常運(yùn)行的,來著普中開發(fā)板
#include <reg52.h>
void delay(unsigned int i);
char LED;
main()
{
unsigned char i;
while (1)
{ LED = 0xfe;
for (i = 0 ;i < 8 ; i++)
{
P2 = LED;
delay(500);
LED = LED << 1;
LED = LED | 0x01;
if (LED == 0x7f) break;
}
for (i = 0 ;i < 8 ; i++)
{
P2 = LED;
delay(500);
LED = LED >> 1;
LED = LED | 0x80;
}
}
}
/******延時(shí)*************/
void delay(unsigned int i)
{
unsigned char j;
for(i; i > 0; i--)
for(j = 255; j > 0; j--);
}
下面這個(gè)程序,就只能跑一下了!#include < reg52.h >
#define uchar unsigned char
#define uint unsigned int
/*****延時(shí)*****/
void delayMS ( uint ms )
{
uchar i;
while ( ms-- )
{
for ( i = 0; i < 255; i++ );
}
}
/***主函數(shù)***/
void main ()
{
uchar i;
while ( 1 )
{
for ( i = 0; i < 8; i++ )
{
P2 = 0xfe;
delayMS ( 500 );
P2 = P2 << 1;
P2 = P2 | 0x01;
if ( P2 == 0x7f )
break;
}
for ( i = 0; i < 8; i++ )
{
P2 = 0xfe;
delayMS ( 500 );
P2 = P2 >> 1;
P2 = P2 | 0x80;
}
}
}
|
|