1.通过数组实现led流水灯
#include
int m;
unsigned char code leds[8] = {0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe};
void delay(unsigned int i)
{
unsigned int j;
unsigned char k;
for(j=i; j>0; j--)
for(k=125; k>0 ;k--);
}
int main()
{
while(1)
{
for(m = 0; m < 8; m++)
{
P1 = leds[m];
delay(500);
}
}
return 0;
}
使用
if语句实现led灯的控制,i类似于标志位控制进行循环转移
i = 0;
while(1)
{
P1 = leds[i++];
delay(5000);
if(i == 8) i = 0;
}
2.通过独立按键实现LED灯的闪烁流动
运用独立按键需I/O端口,预先置高,表示作为输入端
#include
sbit k1 = P3^4;
unsigned int m;
unsigned char code leds[8] = {0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe};
void delay(unsigned int i)
{
unsigned int j;
unsigned char k;
for(j=i; j>0; j--)
for(k=125; k>0; k--);
}
int main()
{
k1 = 1;
while(1)
{
for(m = 0; m < 8; )
{
P1 = leds[m];
delay(500);
if(k1 == 0)
{
delay(80);
if(k1 == 0)
m++;
}
}
}
return 0;
}
3.外部中断1实现按键闪烁
1).实现部分在中断函数里
#include
sbit p3_4 = P3^4;
unsigned int m;
unsigned char code leds[8] = {0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe};
void delay(unsigned int i)
{
unsigned int j;
unsigned char k;
for(j=i; j>0; j--)
for(k=125; k>0; k--);
}
int main()
{
EA = 1;
EX1 = 1;
IT1 = 1;
p3_4 = 0;
P1 = leds[0];
while(1);
return 0;
}
void led_key() interrupt 2
{
if(m <= 7)
{
P1 = leds[m++];
delay(500);
//m++;
}
else
P1 = leds[0];
}
2)实现部分在主函数中
bit flag; //标志位
while(1)
{
for(m = 0; m < 8; )
{
P1 = leds[m];
delay(500);
if(flag == 1)
{
delay(80); //去抖 -----中断
if(flag == 1)
m++;
}
}
}
void led() interrupt 0
{
flag = 1;
}