|
原代碼:
#include<reg52.h>
#include<intrins.h>//字符循環(huán)移動(dòng)函數(shù)庫
void delays()//延遲
{
int x,y;
for(x=0;x<110;++x)
for(y=0;y<1000;++y);
}
void delayms(unsigned int xms)//x ms延遲
{
unsigned int c,h;
for(c = 0; c<xms; ++c)
for(h = 0; h<110; ++h);
}
void main()
{
//從左到右,再從右到左的流水燈
unsigned char j;
unsigned char k;
unsigned char temp;
int i=0;
P2 = 0xff;
delays();
temp=0x7f;
for(k=0;k<7;k++)
{
P2=temp;
delays();
temp=_cror_(temp,1); //字符循環(huán)右移實(shí)現(xiàn)右移
}
for(j=0;j<8;j++)
{
P2=temp;
delays();
temp=_crol_(temp,1); //字符循環(huán)左移實(shí)現(xiàn)左移
}
/*另外一種思路,用移位運(yùn)算符實(shí)現(xiàn)
unsigned char a,i;
a=0xfe;
for(i=0;i<7;i++)
{
P2=a;
delays();
a=a<<1;//1移動(dòng),其他補(bǔ)零
a=a|0x01;//左移一位后與0x01相或(只要滿足一個(gè)條件) 0000 0001
// //或a=a|0x00;逐個(gè)點(diǎn)亮
}
a=0x7f;
for(i=0;i<7;i++)
{
P2=a;
delays();
a=a>>1;//1移動(dòng),其他補(bǔ)零
a=a|0x80;//右移一位后與0x80相或(只要滿足一個(gè)條件) 1000 0000
// //或a=a|0x00;逐個(gè)點(diǎn)亮
}*/
/*用數(shù)組實(shí)現(xiàn)從兩側(cè)到中間,再從中間到兩側(cè)流水燈
int e;
int i=0;
unsigned char d[]={0x7e,0xbd,0xdb,0xe7};//定義亮燈熄燈數(shù)組
for(e=0;e<3;e++) //兩側(cè)到中間
{
P2=d[e];
delays();
}
for(e=3;e>=0;e--) //中間到兩側(cè)
{
P2=d[e];
delays();
}*/
}
新手上路,請(qǐng)多指教,謝謝!
|
評(píng)分
-
查看全部評(píng)分
|