#include <REGX52.H>//頭文件包含
//毫秒延時子函數
void Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}
//主函數入口
main()
{
unsigned char LEDIndex = 0;//定義變量
bit LEDDirection = 1;//定義位變量
while(1)//主死循環,讓單片機壹直工作
{
if(LEDDirection)//如果LEDDirection為真
P1 = ~(0x01<<LEDIndex);//LEDIndex的值左移1位後取反後給P1口輸出
else//如果LEDDirection為0
P1 = ~(0x80>>LEDIndex); //1000 0000右移LEDIndex位後取反後給P1口輸出
if(LEDIndex==7)//如果為LEDIndex=7則取反LEDDirection的值
LEDDirection = !LEDDirection;
LEDIndex = (LEDIndex+1)%8;//LEDIndex+1後除以8的余數再給LEDIndex
Delay1ms(100);//延時100毫秒
}
}