|
(高分求救)我用T2作中断发生器,串口接收正常,发送不对
[复制链接]
我用T2作中断发生器,串口接收正常,发送不对,不知道哪个设定不比好心人帮帮忙.程序如下#include
#include
uint08 com1data[15];
uint08 point1=0;
uint08 com2data[6];
uint08 point2=0;
uint08 sbuf[15];
void initUart(void)
{ IE=0x90; //允许总中断和串口0的中断
TMOD=0x20; //定时器1工作在模式1
//T1 用于com1波特率 9600
switch (4)/*串口1波特率选择*/
{
case 1:
TH1=0xE8; //1200
TL1=0xE8;
break;
case 2:
TH1=0xF4; //2400
TL1=0xF4;
break;
case 3:
TH1=0xFA; //4800
TL1=0xFA;
break;
case 4:
TH1=0xFD; //9600
TL1=0xFD;
break;
}
SCON1=0x58; //工作在模式1,允许接收
PCON=0x00; // SM0=0 SM1=1 在10位异步收发模式 SMOD=0 溢出速率/32
ES1=1; //串口1中断允许
SCON0=0x58; //工作在模式1,接收允许
T2CON=0x30; //用定时器2做串口0的波特率发生器
//TCLK=1;
//T2 用于com0波特率 9600
switch (4)/*串口0波特率选择*/
{
case 1:
RCAP2H=0xFE; //1200
RCAP2L=0xE0;
break;
case 2:
RCAP2H=0xFF; //2400
RCAP2L=0x70;
break;
case 3:
RCAP2H=0xFF; //4800
RCAP2L=0xB8;
break;
case 4:
RCAP2H=0xFF; //9600
RCAP2L=0xDC;
break;
case 5:
RCAP2H=0xFF; //19200
RCAP2L=0xEE;
break;
}
TR2=1;
TR1=1;
}
void Int_serial1() interrupt 7
{
uint16 uctimes=5000;
while((RI_1==0)&&(uctimes>0))
{
uctimes--;
}
if(uctimes!=0)
{ com2data[point2]=SBUF1;
point2++;
}
RI_1=0;
}
void Intcom ()interrupt 4 //串口1接收
{
uint16 uctimes=5000;
while((RI==0)&&(uctimes>0))
{
uctimes--;
}
if(uctimes!=0)
{
if(point1==0)
{
if(SBUF==0x7e)
{
com1data[point1]=SBUF;
point1++;
}
}
else
{
com1data[point1]=SBUF;
point1++;
}
}
RI=0;
}
void DelayMs(uint08 usTime)
{
uint16 i;
uint16 j;
for(j=0;j
for(i=0;i<227;i++);
}
void putchar(uint08 xdata *ucBuff) //串口1发送
{ uint08 ucNum;
for(ucNum = 0; ucNum <15; ucNum++)
{
SBUF = ucBuff[ucNum];
while (0 == TI);
TI = 0;
}
}
void main()
{ sbuf[0] = 0x7e;
sbuf[1] = 0x01;
sbuf[2] = 0x00;
sbuf[3] = 0x10;
sbuf[4] = 0x01;
sbuf[5] = 0x00;
sbuf[6] = 0x12;
sbuf[7] = 0x0d;
sbuf[8] = 0x08;
sbuf[9] = 0x09;
sbuf[10] = 0x0a;
sbuf[11] = 0x0b;
sbuf[12] = 0x0c;
sbuf[13] = 0x0d;
sbuf[14] = 0x0e;// 获取实时信息
initUart();
while(1)
{
SBUF1=1;
if(point1==15)
{
ES1=0;
putchar(sbuf);
ES1=1;
point1=0;
}
if(point2==6)
point2=0;
}
}
void putchar(uint08 xdata *ucBuff) //串口1发送
{ uint08 ucNum;
for(ucNum = 0; ucNum <15; ucNum++)
{
SBUF = ucBuff[ucNum];
while (0 == TI);
TI = 0;
}
}
数据不对
|
|