用C语言给单片机编程时,主函数中的变量为什么都得定义成全局变量

来源:百度知道 编辑:UC知道 时间:2024/06/17 21:31:11

变量是定义成全局变量还是局部变量得看变量的在程序中所起的作用,比如如下程序:
#include <REGX52.h>
unsigned int int_count=0;
unsigned char count=0;
unsigned char second=0;
unsigned char seg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x40};
unsigned char seg1[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x98};
Delay_1s() //500m秒延时子程序
{unsigned char l,i,n;
for(l=50;l>0;l--)
for(i=20;i>0;i--)
for(n=248;n>0;n--);
}

void t0(void) interrupt 1 using 0
{int_count++;
if(int_count==4000)
{ int_count=0;
second=1;
}
}
void main (void)
{TMOD=0x02;
TH0=0x06;
TL0=0x06;
TR0=1;
ET0=1;
EA=1;
while(1)
{
if(second)
{
second=0;
P0=seg1[count];
count++;
if(count>=10)
count=0;
}
}
}
定时器输出0到9,每一秒变化一次,其中int_count、second是全局变量这是因为,int_count是中断次数统计,他需要在完成一次次的中断