最近在用STC15L204EA单片机,单片机不带串口,自己写了段程序,单片机往上位机发送数据没有问题,可是上位机往单片机发送数据总是有问题,急死了,还请懂得大神帮忙,谢谢。看看是哪里出错了。
/*24MHz 9600bps*/
#include "STC15L204EA.h"
sbit RXD = P3^2; //define UART TX/RX port
sbit TXD = P3^1;
sbit led = P1^6;
bit sflag=0;
unsigned char buf=0x00;
void WaitTF0(void);
unsigned char RByte();
void WByte(unsigned char input);
void Delay100ms();
void DelayS(unsigned char t);
void main()
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x40;
TH0 = 0xF6;
TF0 = 0;
TR0 = 1;
INT_CLKO |= 0x40;
EA = 1;
while(1)
{
DelayS(1);
WByte(buf);
}
}
/*void tm0_isr() interrupt 1 using 1
{
TF0=1;
}*/
void exint4() interrupt 16
{
INT_CLKO &= 0xBF;
buf=RByte();
INT_CLKO |= 0x40;
}
unsigned char RByte() //½ÓÊÕÒ»¸ö×Ö·û
{
unsigned char Output=0;
int i=8;
TR0=1; //Æô¶¯Timer0
WaitTF0(); //µÈ¹ýÆðʼλ
while(i--) //½ÓÊÕ8λÊý¾Ýλ
{
Output >>=1;
if(RXD) Output |=0x80; //ÏÈÊÕµÍλ
WaitTF0(); //λ¼äÑÓʱ
}
//while(!TF0) if(RXD) break;
TR0=0; //Í£Ö¹Timer0
//Output = 0x50;
return Output;
}
void WByte(unsigned char input)
{
int i=8; //·¢ËÍÆôʼλ
TR0=1;
TXD=(bit)0;
WaitTF0();
while(i--) //·¢ËÍ8λÊý¾Ýλ
{
TXD=(bit)(input&0x01);//ÏÈ´«µÍλ
WaitTF0();
input=input>>1;
}
TXD=(bit)1; //·¢ËͽáÊøλ
WaitTF0();
TR0=0;
}
void WaitTF0(void) //²éѯ´«Êä±ê־λ
{
TF0=0; //Çå±ê־λ
while(!TF0) ;
TF0=0; //Çå±ê־λ
}
void Delay100ms() //@24.000MHz
{
unsigned char i, j, k;
i = 10;
j = 31;
k = 147;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void DelayS(unsigned char t)
{
int a=0;
while(t--)
{
for(a=0;a<=10;a++)
{
Delay100ms();
}
}
}
|