为什么,我用STC12系列单片机的串口2不能进行正常通行
#include <STC12C5A60S2.h>
#include "SerialPort.h"
void SendData2(unsigned char dat);
void SendStr2(char *str);
void main(void)
{
unsigned int i;
UartInit2();
UartInit();
while(1)
{
SendStr2("dxsl");
for(i=0; i<20000; i++);
}
}
void UartIsr(void) interrupt 4
{
unsigned char receiveData;
receiveData = SBUF; //³öÈ¥½ÓÊÕµ½µÄÊý¾Ý
RI = 0; //Çå³ý½ÓÊÕÖжϱê־λ
SendData(receiveData);
}
void UartIsr2(void) interrupt 8
{
unsigned char receiveData;
S2CON &= ~S2RI;
receiveData = S2BUF;
SendData2(receiveData);
}
下面是SerialPort.c文件
#include "SerialPort.h"
void UartInit(void)
{
//¾§ÕñƵÂÊΪ12MHz£¬Ñ¡¶¨´®¿Ú²¨ÌØÂÊΪ4800bps£¬ÔòSMODÖ»ÄÜΪ1£¬×°ÔسõֵΪ0xf3
SCON = 0x50;
PCON = 0x80; //²¨ÌØÂʼӱ¶
TMOD |= 0x20;
// TH1 = TL1 = 0xf4;
TH1 = TL1 = 0xfd; //É趨ÖØÔØÖµ0xf3 //-(FOSC/12/32/BAUD);
TR1 =1; //¶¨Ê±Æ÷ 1 ´ò¿ª
ES = 1; //´®¿ÚÖжϴò¿ª
EA = 1;
}
void SendData(unsigned char dat)
{
SBUF = dat;
while(!TI);
TI = 0;
}
void SendStr(char *str)
{
while(*str)
{
SendData(*str++);
}
}
void UartInit2(void)
{
S2CON = 0x50; //°Ëλ¹¤×÷ģʽһ
BRT = 0xfd; //¶ÀÁ¢²¨ÌØÂÊ
AUXR |= 0x10; //²¨ÌØÂʼӱ¶£¬ BRT´ò¿ª
IE2 = 0x01; //ÔÊÐí´®¿Ú2ÖжÏES2=1
EA = 1;
}
void SendData2(unsigned char dat)
{
S2BUF = dat;
while(!(S2CON & S2TI));
S2CON &= ~S2TI;
}
void SendStr2(char *str)
{
while(*str)
{
SendData2(*str++);
}
}
|