通过左移和右移函数实现流水灯,需添加语句#include<intrins.h>
代码如下:(STC89C52RC)
解析
在主函数的while循环中,建立两个for函数分别用于实现左移8次和右移8次,在第一个for循环中,P1赋值语句在移位语句之前,程序开始运行时,需赋给P1初值0XFE,再进行移位操作,等到8次后,temp值为0XFE,表示左移结束;开始右移操作时,将temp右移移位,并赋值给P1。
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
void delay(uint z);
uchar temp=0xfe;
uchar i,j;
void main()
{
while(1)
{
for(i=0;i<8;i++)
{
P1=temp;
temp=_crol_(temp,1);
delay(100);
}
for(j=0;j<8;j++)
{
temp=_cror_(temp,1);
P1=temp;
delay(100);
}
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}