以下两个程序有什么区别
先看第一个程序:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
sbit duan=P2^6;
sbit wei=P2^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void display(uchar,uchar);
void delay(uint);
uchar shi,ge,num,num1;
void main()
{
TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
while(1)
{
display(shi,ge);
}
}
void t0()interrupt 1
{ uint a;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
a++;
if(a==4)
{
a=0;
led=~led;
}
}
void t1()interrupt 3
{
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
num1++;
shi=num1/10;
ge=num1%10;
if(num1==60)
{num1=0;}
}
}
void display(uchar shi,uchar ge)
{
duan=1;
P0=table[shi];
duan=0;
P0=0xff;
wei=1;
P0=0xfe;
wei=0;
delay(5);
duan=1;
P0=table[ge];
duan=0;
P0=0xff;
wei=1;
P0=0xfd;
wei=0;
delay(5);
}
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
像上面这样就可以实现发光二极管的流水灯闪烁和数码管60秒计时。上面定义的为全局变量。但是像下面一样就不可以了,
定义为局部变量。这是为什么呢?
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit led=P1^0;
sbit duan=P2^6;
sbit wei=P2^7;
uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
void display(uchar,uchar);
void delay(uint);
void main()
{ uchar shi,ge; TMOD=0x11;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
ET1=1;
TR1=1;
while(1)
{
display(shi,ge);
}
}
void t0()interrupt 1
{ uint a;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
a++;
if(a==4)
{
a=0;
led=~led;
}
}
void t1()interrupt 3
{
uchar shi,ge,num,num1;
TH1=(65536-45872)/256;
TL1=(65536-45872)%256;
num++;
if(num==20)
{
num=0;
num1++;
shi=num1/10;
ge=num1%10;
if(num1==60)
{num1=0;}
}
}
void display(uchar shi,uchar ge)
{
duan=1;
P0=table[shi];
duan=0;
P0=0xff;
wei=1;
P0=0xfe;
wei=0;
delay(5);
duan=1;
P0=table[ge];
duan=0;
P0=0xff;
wei=1;
P0=0xfd;
wei=0;
delay(5);
}
void delay(uint xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
} |