#include <REGX51.H> //頭文件,定義單片機型號
#define uchar unsigned char //偽指令用uchar代替unsigned char
#define uint unsigned int //偽指令用uint代替unsigned int
uchar code a[6]={0xfa,0xf5,0xeb,0xd7,0xaf,0x5f}; //定義編譯數組
uchar code b[6]={0x5f,0xaf,0xd7,0xeb,0xf5,0xfa}; //定義編譯數組
void delay(uint x) //延時函數
{
uint y;
for(;x>0;x--)
for(y=100;y>0;y--);
}
void main(void) //函數的開始,(void)表示有返回值
{
uint i;
uchar temp;
while(1) //while循環(1)循環條件
{
temp=0xfe;
for(i=0;i<8;i++)
{
P0=temp;
delay(300);
temp=(temp<<=1)|0x01; //temp值左移1位,從而實現流水燈效果,0x01表示在最右面補1
}
temp=0x7f;
for(i=0;i<8;i++)
{
P0=temp;
delay(300);
temp=(temp>>=1)|0x80; //temp值右移1位,從而實現流水燈效果,0x80表示在最左面補1
}
for(i=0;i<6;i++)
{
P0=a[i]; //遍歷數組a,將數組a中的每個元素賦給P0
delay(400);
}
for(i=0;i<6;i++)
{
P0=b[i]; //遍歷數組b,將數組b中的每個元素賦給P0
delay(400);
}
}
}
|