使用位定义实现LED流水灯
#include <reg52.h>
#define time 20000
sbit LED1=P2^0;
sbit LED2=P2^1;
sbit LED3=P2^2;
sbit LED4=P2^3;
sbit LED5=P2^4;
sbit LED6=P2^5;
sbit LED7=P2^6;
sbit LED8=P2^7;
void delay(unsigned int i)
{
while(i--);
}
void main()
{
while(1)
{
LED8=1;
LED1=0;
delay(time);
LED1=1;
LED2=0;
delay(time);
LED2=1;
LED3=0;
delay(time);
LED3=1;
LED4=0;
delay(time);
LED4=1;
LED5=0;
delay(time);
LED5=1;
LED6=0;
delay(time);
LED6=1;
LED7=0;
delay(time);
LED7=1;
LED8=0;
delay(time);
}
}
使用P2口实现LED流水灯
#include <reg52.h>
#define time 20000
void delay(unsigned int i)
{
while(i--);
}
void main()
{
while(1)
{
P2=0xfe;
delay(time);
P2=0xfd;
delay(time);
P2=0xfb;
delay(time);
P2=0xf7;
delay(time);
P2=0xef;
delay(time);
P2=0xdf;
delay(time);
P2=0xbf;
delay(time);
P2=0x7f;
delay(time);
}
}
使用数组实现LED流水灯
#include <reg52.h>
#define time 20000
unsigned char code led[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(unsigned int i)
{
while(i--);
}
void main()
{
int i;
while(1)
{
for(i=0;i<8;i++)
{
P2=led[i];
delay(time);
}
}
}
使用按位左移实现LED流水灯
#include <reg52.h>
#define time 20000
void delay(unsigned int i)
{
while(i--);
}
void main()
{
int i;
while(1)
{
for(i=0;i<8;i++)
{
P2=~(0x01<<i);
delay(time);
}
}
}
使用左移函数实现LED流水灯
#include <reg52.h>
#include <intrins.h>
#define time 20000
void delay(unsigned int i)
{
while(i--);
}
void main()
{
P2=0xfe;
while(1)
{
delay(time);
P2=_crol_(P2,1);
}
}
|