查阅有关资料51中断函数声明变量为全局变量?不知道这个说法对不对。1、在中断函数内声明变量bit flag = 0;那么变量可以被正常赋值使用;
2、在中断函数内声明为uchar flag = 0;此时该变量不能被正常赋值使用;
3、把变量uchar flag = 0;声明到全局变量,该变量可以正常赋值使用;
单片机STC89C52;编译平台keil5代码如下:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit number0 = P1^0;
sbit number1 = P1^1;
sbit number2 = P1^2;
sbit number3 = P1^3;
sbit number4 = P1^4;
sbit number5 = P1^5;
sbit number6 = P1^6;
sbit number7 = P1^7;
sbit number8 = P3^2;
sbit number9 = P3^3;
sbit call = P3^4;
uchar temp[14];
bit flag_REC = 0;
uchar i = 0;
bit flag = 0;
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void UART_INIT()
{
SM0 = 0; SM1 = 1;
REN = 1;
EA = 1;
ES = 1;
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
TR1 = 1;
}
void main()
{
uchar b;
UART_INIT();
while(1)
{
if(flag_REC == 1)
{
flag_REC = 0;
for(b = 0; b < i; b++)
{
switch(temp[b])
{
case '0': number0 = 0; break;
case '1': number1 = 0; break;
case '2': number2 = 0; break;
case '3': number3 = 0; break;
case '4': number4 = 0; break;
case '5': number5 = 0; break;
case '6': number6 = 0; break;
case '7': number7 = 0; break;
case '8': number8 = 0; break;
case '9': number9 = 0; break;
case 'C': call = 0; break;
}
delay(50);
P1 = 0XFF;
P3 = 0Xff;
delay(50);
}
i = 0;
}
}
}
void sint() interrupt 4
{
uchar dat;
if(RI)
{
RI=0;
dat=SBUF;
if(dat=='*'&&(i==0))
{
temp[i]=dat;
flag=1;
}
else
if(flag==1)
{
i++;
temp[i]=dat;
if(dat == '#')
{
flag = 0;
flag_REC = 1;
}
}
}
}
反复查阅相关资料和分析没有结果,望各位师傅不吝赐教!在此感谢各位!
|