很喜欢51黑论坛,论坛里面有很多热心的大佬,这是我见过的最有爱的论坛,没有之一。每一位回贴的网友都是优秀的老师,像我这样的小白在这里能学到很多东西。
最近想用数组跑一个流水灯,LED的引脚是随便接的,考虑到实际应用中也不可能有整组的引脚来操作,所以申明了个全局变量Y来整合这些引脚,问题好像就出现在这里。
这段代码运行的结果没有逐个点亮LED灯,而是一次全亮,显示的是y得到数组最后一个元素0X0000全亮,for循环是遍历了数组每个元素的,但为什么延时失效了?问题出在哪?好懵逼
#include<reg51.h>
sbit LED1 = P3^5;
sbit LED2 = P1^7;
sbit LED3 = P1^6;
sbit LED4 = P1^5;
sbit LED5 = P1^4;
sbit LED6 = P1^3;
sbit LED7 = P3^7;
sbit LED8 = P0^0;
sbit LED9 = P1^2;
sbit LED10 = P1^0;
sbit LED11 = P3^2;
sbit LED12 = P1^1;
//全局变量//
int y;
//定义LED数组//
int leddata[]={0x0ffe,0x0ffc,0x0ff8,0x0ff0,0x0fe0,0x0fc0,0x0f80,0x0f00,0x0e00,0x0c00,0x0800,0x0000};
//延时函数//
delay(int t)
{ int i,j;
for(i=t;i>0;i--)
{
for(j=247;j>0;j--);
}
}
//LED驱动函数//
void LED()
{
LED1=0x0001&y;
LED2=0x0002&y;
LED3=0x0004&y;
LED4=0x0008&y;
LED5=0x0010&y;
LED6=0x0020&y;
LED7=0x0040&y;
LED8=0x0080&y;
LED9=0x0100&y;
LED10=0x0200&y;
LED11=0x0400&y;
LED12=0x0800&y;
}
main()
{int a;
LED();
for (a=0;a<12;a++)
{
y=leddata[a];
delay(100);
}
}
|