发送一个字节到底进几次串口中断?
用的芯片为:STC10F08XE
1、发现问题的代码:
void isrSerial(void) interrupt 4
{
if(RI)
{
RI = 0;
RcvBuffer[RcvNumber++] = SBUF;
if (RcvNumber == RCVBUFLEN) RcvNumber = 0;
}
else
{
SentNumber++; //写在这里会发现,发送一个字节SentNumber值加2
}
}
void SerialSend(u8 xdata *buffer,u8 number)
{
pSendBuffer = buffer;
SentNumber = 0;
REN = 0;
while(SentNumber<number)
{
SBUF = pSendBuffer[SentNumber];
while(TI==0);
TI = 0;
// SentNumber++; 写在这里正常
}
REN = 1;
}
2、测试发送一个字节到底进几次中断
上面代码不变;
void main(void)
{
InitSerial();
SentNumber = 0;
SBUF = 0xaa;
while(TI == 0);
TI = 0;
while(1)
{
if(SentNumber == 1) Led0 = 0;
else if(SentNumber == 2) Led1 = 0; //结果显示Led1亮
}
}
3、修改串口中断服务程序
更改为:
void isrSerial(void) interrupt 4
{
if(RI)
{
RI = 0;
RcvBuffer[RcvNumber++] = SBUF;
if (RcvNumber == RCVBUFLEN) RcvNumber = 0;
}
else if(TI) //就多了这一点代码,结果就一切正常
{
SentNumber++; //写在这里会发现,发送一个字节SentNumber值加2
}
}
4、最后的测试代码
void isrSerial(void) interrupt 4
{
if(RI)
{
RI = 0;
RcvBuffer[RcvNumber++] = SBUF;
if (RcvNumber == RCVBUFLEN) RcvNumber = 0;
}
else if(TI)
{
SentNumber++;
}
else Led0 = 0; //灯竟然会亮!!!神马原因??
}
|