|
我的程序如下:
//串口0用于与微机通信,接到微机数据包后不做改变,原封返回数据包
//晶振:11.0592MHz 波特率:2400
#include
sbit DOG = P3^2;
sbit LED = P3^3;
void InitUart0(void);
void InitTimer0(void);
void Uart0Interrupt(void);
void Timer0Interrupt(void);
void Uart0SendByte(unsigned char dat);
void delay(unsigned int times);
unsigned char i = 0;
unsigned int j,t;
unsigned char nbuff[7];
unsigned char rinbuff[20] = {0};
bit RFlag;
void main(void)
{
EA = 0;
InitTimer0();
delay(100);
InitUart0();
delay(100);
EA = 1;
while(1)
{
DOG = !DOG;
if(RFlag)
{
for(j = 0; j < 6; j++)
{
Uart0SendByte(nbuff[j]);
// delay(100);
}
RFlag = 0;
}
}
}
void InitUart0(void)
{
ES0 = 0;
S0STAT = 0x20;
PCON &= 0x1f;
TMOD |= 0x20; // timer1为自动重装模式
TH1 = 0xe8; // 设置串口1的波特率为2400
TL1 = 0xe8;
TR1 = 1;
S0CON = 0x50; // SM0_0 = 0, SM1_0 = 1(串口模式1), SM2_0 = 0, REN_0 = 1(允许收中断), TI_0 = 0, RI_0 = 0, TB8_0 = 0, RB8_0 = 0,
ES0 = 1;
}
void InitTimer0(void)
{
TMOD = 0x01;
TH0 = 0x03; // 定时10毫秒
TL0 = 0x00;
ET0 = 1;
TR0 = 1;
}
void Uart0Interrupt(void) interrupt 4
{
unsigned char dat;
if(RI_0)
{
RI_0 = 0;
dat = S0BUF;
if(i < 7)
{
nbuff = dat;
i++;
}
else
{
RFlag = 1;
i = 0;
}
}
}
void Timer0Interrupt(void) interrupt 1
{
TH0 = 0x03;
TL0 = 0x00;
t++;
if(t == 20)
{
LED = !LED;
t = 0;
}
}
void Uart0SendByte(unsigned char dat)
{
S0BUF = dat;
while(TI_0 == 0);
TI_0 = 0;
}
void delay(unsigned int times)
{
unsigned int a;
for(a = 0; a < times; a++);
}
产生问题:
我通过串口调试助手向89c669发送6个字节“aa 00 00 00 00 ff”,接收的字节为“AA 00 00 00 00 FF 00 00 00 FF AA 00 00 FF AA 00 00 00 AA 00 00 00 00 FF 00 00 00 FF AA 00 00 FF AA 00 00 00 AA 00 00 00 00 FF 00 00 00 FF AA 00 00 FF AA 00 00 00 AA 00 00 00 00 FF ”。波特率的初始值虽然和书上给的范例不一样,但是这里的初始值经过试验是没有问题的,但是为什么数据包里的数据会产生错位,我一直弄不明白,这里特向诸位大侠求救,先谢谢诸位了!!
|
|