此程序為設(shè)置Led流水燈與數(shù)碼管第0一位的0-16數(shù)字循環(huán)亮。不能將數(shù)碼管程序放進(jìn)主函數(shù)里,因為延遲函數(shù)delay,可能會在add自加到40時剛好在dalay里面,這時不會有數(shù)碼管的閃爍。
#include
#include
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7; //位選
sbit dula=P2^6; //段選
int num,add,temp=0xfe;
uchar code table[]={0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uchar n);
void main()
{
TMOD=0x01; //工作方式
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //總中斷打開
ET0=1; //定時器中斷打開
TR0=1; //定時器打開
wela=1;
P0=0xfe;
wela=0;
dula=1;
P0=table[0];
dula=0; //數(shù)碼管初始化
P1=temp; //Led燈初始化
while(1)
{
delay(100);
delay(500);
temp=_crol_(temp,1);
P1=temp;
}
}
void add_1() interrupt 1 //計時器中斷函數(shù)
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
add++;
if(add==40)
{
num++;
add=0;
dula=1;
P0=table[num];
if(num==16)
num=-1;
}
}
//流水燈延遲函數(shù)
void delay(uchar n)
{
int i=0, j=0;
for(i=0; i for(j=0; j<125; j++) ; }