很喜歡51黑論壇,論壇里面有很多熱心的大佬,這是我見過的最有愛的論壇,沒有之一。每一位回貼的網(wǎng)友都是優(yōu)秀的老師,像我這樣的小白在這里能學到很多東西。
最近想用數(shù)組跑一個流水燈,LED的引腳是隨便接的,考慮到實際應用中也不可能有整組的引腳來操作,所以申明了個全局變量Y來整合這些引腳,問題好像就出現(xiàn)在這里。
這段代碼運行的結(jié)果沒有逐個點亮LED燈,而是一次全亮,顯示的是y得到數(shù)組最后一個元素0X0000全亮,for循環(huán)是遍歷了數(shù)組每個元素的,但為什么延時失效了?問題出在哪?好懵逼
#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數(shù)組//
int leddata[]={0x0ffe,0x0ffc,0x0ff8,0x0ff0,0x0fe0,0x0fc0,0x0f80,0x0f00,0x0e00,0x0c00,0x0800,0x0000};
//延時函數(shù)//
delay(int t)
{ int i,j;
for(i=t;i>0;i--)
{
for(j=247;j>0;j--);
}
}
//LED驅(qū)動函數(shù)//
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);
}
}
|