有没有遇到采用中断方式每隔几秒种发送一组数据出去之后,调试助手收到多个同样的数据,而在串口发送后加1秒的延迟程序,收到的就只有一组数据.
#include "stc15.h"
#include "USART.h"
#include "intrins.H"
unsigned char TxBusy = 0; //发送忙检测
unsigned char rxBuff[rxLength]={0}; //接收缓冲区,长度50
unsigned char txBuff[txLength]={0}; //发送缓冲区,长度50
unsigned char rxcnt = 0; //接收计数
/* void Uart_Init(void) //115200bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0xE8; //设定定时初值
T2H = 0xFF; //设定定时初值
AUXR |= 0x10; //启动定时器2
ES = 1; //使能串口中断
EA = 1; //开总中断
}*/
void Uart_Init(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x01; //串口1选择定时器2为波特率发生器
AUXR |= 0x04; //定时器2时钟为Fosc,即1T
T2L = 0xE0; //设定定时初值
T2H = 0xFE; //设定定时初值
AUXR |= 0x10; //启动定时器2
ES = 1; //使能串口中断
EA = 1; //开总中断
}
/*
void Uart1_SendByte(unsigned char dat)
{
//unsigned int OutTime = 10000;
//TI = 0;
//while((TxBusy)&&(OutTime--));
while(TxBusy);
SBUF = dat;
TxBusy = 1;
}*/
void Uart1_SendByte(unsigned char dat)
{
while(TxBusy);//等待数据发送完成,进入中断,将TI、TxBusy清0
TxBusy = 1;
SBUF = dat;
}
void Uart1_SendByte_N(unsigned char *dat,unsigned char N)
{
while(N--)
{
Uart1_SendByte(*dat) ;
dat++;
}
}
void Uart1_IT_Handle(void) interrupt 4 using 1
{
unsigned char temp;
if(RI)
{
RI = 0; //清RI
if(rxcnt<rxLength)
{
rxBuff[rxcnt++]= SBUF;
}
else
{
temp =SBUF; //读BUFF
}
}
if(TI)
{
TI = 0 ; //清TI
TxBusy = 0 ; //清BUSY
}
}
/*void Uart_SendStr(const char* str)
{
while(*str)
{
Uart1_SendByte(*str);
str++;
}
} */ |