发光二极管LED的控制
电路图
双霹雳灯循环点亮
方法一
//双霹雳灯 位移语句
#include<reg51.h>
#define LED P1
void delay(int);
int main()
{
unsigned char i;
LED = 0xfc;
while(1)
{
for(i = 0;i<3;i++) //左移
{
delay(100);
LED = (LED<<2)|0x03;
}
for(i = 0;i<3;i++) //右移
{
delay(100);
LED = (LED>>2)|0xc0;
}
}
return 0;
}
void delay(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<600;j++);
}
方法二
//双霹雳灯库函数实现
#include<reg52.h>
#include<intrins.h>
#define LED P1
void delay(int x);
int main()
{
unsigned char i;
LED = 0xfc;
while(1)
{
for(i = 0;i<3;i++) //左移
{
delay(100);
LED = _crol_(LED,2);
}
for(i = 0;i<3;i++) //右移
{
delay(100);
LED = _cror_(LED,2);
}
}
return 0;
}
void delay(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<600;j++);
}
方法三
//双霹雳灯 数组实现
#include<reg51.h>
#define LED P1
void delay(int);
unsigned char code tab[] = {0xfc,0xf3,0xcf,0x3f};
int main()
{
unsigned char i;
while(1)
{
for(i = 0;i<3;i++) //左移
{
LED = tab[i];
delay(100);
}
for(i = 3;i>0;i--) //右移
{
LED = tab[i];
delay(100);
}
}
return 0;
}
void delay(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<600;j++);
}
交替闪烁
2个灯为一组,交替闪烁4次,移到下一组继续4次,然后…
/*2个灯为一组,交替闪烁4次,移到下一组继续4次,然后...............*/
#include<reg51.h>
#define LED P1
unsigned char code tab[] = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(int);
int main()
{
unsigned char i,j;
while(1)
{
for(i=0;i<8;i+=2)
{
for(j=0;j<4;j++)
{
LED=tab[i];
delay(100);
LED = tab[i+1];
delay(100);
}
}
}
return 0;
}
void delay(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<60;j++);
}