今天學習了一個很簡單的功能實現~利用單片機實現流水燈效果。
函數:_cror_( a,b ); 將char型變量a循環向右移動指定位數b後返回。b=1/2/…
_crol_(a , b); 將char型變量a循環向左移動指定位數b後返回。
註釋:這裏的左移指全部位數向高位移動,最高位補到最低位。
注意:使用時需包含頭文件。
需要寫代碼 #include <instrins.h>
代碼實現:
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit DIOLA=P2^5;
uchar temp;
void delay(uint z);
void main()
{
DIOLA=1;
temp=0xfe;//二進制11111110,即最右端LED亮,其餘燈滅
P1=temp;
while(1)
{
delay(1000);
temp=_cror_(temp,2);//_cror_&_crol_
P1=temp;//實現LED輪流點亮
}
}
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
仿真截圖: