枕头aa 55 结束55 aa 数据。返回来枕头 结束 中间数据的和
不知道问题出在那。就是反不回来数据
#include <c8051f330.h> // SFR declarations
#include &quot;string.h&quot;
#include <stdio.h>
#include &quot;intrins.h&quot;
unsigned char RX_dat[20];
unsigned char RX_data[20];
unsigned char RXflage;
unsigned long Rx_Freq;
double temp;
unsigned long temp_ulong;
unsigned long cfgdat;
unsigned char UART_RX_idx = 0;
void delay(unsigned long delaytime)
{
for(;delaytime>0;delaytime--)
_nop_();
}
void delay10000(unsigned long delaytime)
{
unsigned long i;
for(;delaytime>0;delaytime--)
for(i = 0;i <10000;i++)
_nop_();
}
void UART_Init()
{
EA = 0;
SCON0 = 0x50;
TCON = 0x40;
TMOD = 0x20;
TH1 = 0x96;
CKCON = 0x00;
TR1 = 1;
ET0= 1;
ES0 =1;
TI0 = 0;
RI0 = 0;
EA = 1;
UART_RX_idx = 0;
RXflage = 0;
}
void Send_data(unsigned char dat)
{
SBUF0 = dat;
TI0 = 1;
delay(10);
while(!TI0);
}
void uart() interrupt 4
{
if(RI0)
{
EA = 0;
RI0 = 0;
RX_dat[UART_RX_idx] = SBUF0;
Send_data(SBUF0);
UART_RX_idx++;
if((UART_RX_idx ==2))
{
if((RX_dat[0]==0XAA)&&(RX_dat[1]==0x55))
{
}
else { UART_RX_idx=0;}
}
if((UART_RX_idx>=6))
{
if((RX_dat[5]==0XAA)&&(RX_dat[4]==0x55))
{
UART_RX_idx |=0x8000;
}
else
{
UART_RX_idx=0;
}
}
EA = 1;
}
}
void main (void)
{
unsigned char time=0;
unsigned char len;
unsigned char t;
unsigned int aa = 0;
PCA0MD &= ~0x40;
OSCICN |= 0x03;
XBR0 = 0x01;
XBR1 = 0x40;
P0MDIN |= 0x00;
P1MDIN |= 0x00;
P0MDOUT|= 0xff;
P1MDOUT|= 0xff;
UART_Init();
delay10000(20);
while(1)
{
delay(100);
if( UART_RX_idx&0x8000 )
{
RX_data[0]=0xaa;
RX_data[1]=0x55;
RX_data[2]=RX_dat[2]+RX_dat[3];
RX_data[3]=0x55;
RX_data[4]=0xaa;
len=UART_RX_idx&0x3fff;
for(t=0;t<(len-1);t++)
{
SBUF0=RX_data[UART_RX_idx];
// Send_data((RX_data[UART_RX_idx]));//向串口1发送数据
while(!TI0)
TI0 = 0;
}
RX_data[UART_RX_idx]=0;
}
}
} |