實現1:位移+取反實現
點選檢視程式碼
#include <stc89c5xrc.h>
#define u16 unsigned int
#define u8 unsigned char
void delay(u16 c)
{
u16 a,b;
for(a=c;a>0;a--)
for(b=115;b>0;b--);
}
void main()
{
u8 i=0;
while(1)
{
for(i=0;i<8;i++)
{
P1=~(0X01<<i);
delay(100);
}
}
}
實現2:呼叫庫函式_crol_和_cror_
點選檢視程式碼
#include <stc89c5xrc.h>
#include <intrins.h>
#define u16 unsigned int
#define u8 unsigned char
void delay(u16 c)
{
u16 a,b;
for(a=c;a>0;a--)
for(b=115;b>0;b--);
}
void main()
{
u8 i=0;
P1=0XFE;
while(1)
{
for(i=0;i<7;i++)
{
P1=_crol_(P1,1);//left move one bit
delay(300);
}
for(i=0;i<7;i++)
{
P1=_cror_(P1,1);//right move one bit
delay(300);
}
}
}
實現3:透過陣列實現
點選檢視程式碼
#include <stc89c5xrc.h>
#include <intrins.h>
#define u16 unsigned int
#define u8 unsigned char
u8 code led[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(u16 c)
{
u16 a,b;
for(a=c;a>0;a--)
for(b=115;b>0;b--);
}
void main()
{
u8 i=0;
P1=0XFE;
while(1)
{
for(i=0;i<8;i++)
{
P1=led[i];
delay(300);
}
}
}