用了UART1和UART2,但只有UART2有输出,UART1没有,都是用T2作波特率发生器的,程序如下:
//#include "reg51.h"
#include "stc8.h"
#include "intrins.h"
#define FOSC 11059200UL
#define BRT (65536 - FOSC / 9600 / 4)//115200
/* sfr AUXR = 0x8e;
sfr T2H = 0xd6;
sfr T2L = 0xd7;
sfr S2CON = 0x9a;
sfr S2BUF = 0x9b;
sfr IE2 = 0xaf; */
bit busy;
bit T10mS;
char wptr;
char rptr;
char buffer[16];
void Uart2Isr() interrupt 8 using 1
{
if (S2CON & 0x02)
{
S2CON &= ~0x02;
busy = 0;
}
if (S2CON & 0x01)
{
S2CON &= ~0x01;
}
}
void Uart2Init()
{
S2CON = 0x50;
T2L = BRT;
T2H = BRT >> 8;
AUXR = 0x14;//
wptr = 0x00;
rptr = 0x00;
busy = 0;
P3M0=0x3;//P3.0,3.1准双向口
P3M1=0xfc;//P3.2-P3.7高阻输入
P1M0=0x7f;//P0.0,0.1准双向口,其它推挽输出
P1M1=0x80;//P1.7高阻输入
//ES = 1; //使能串口中断
}
void Uart2Send(char _data)
{
P_SW1 = 0x40; //RXD_2 TXD_2
while (busy);
busy = 1;
S2BUF = _data; //发送当前数据
}
void Uart2SendStr(char *p)
{
while (*p)
{
Uart2Send(*p++);
}
}
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 43;
j = 6;
k = 203;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void UART1(unsigned char _data)
{
P_SW1 = 0x00; //RXD/P3.0, TXD/P3.1
_nop_();
TI=0;
SBUF=_data; //
while(!TI);
TI=0;
}
void Timer1Init(void) //50毫秒@11.0592MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x00; //设置定时初值
TH1 = 0xdC; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
//ET1=1;
EA=1;
}
void main()
{
unsigned char i;
Uart2Init();
//Timer1Init();
IE2 = 0x01;
EA = 1;
Uart2SendStr("Uart Test !\r\n");
UART1(0x55);
while (1)
{
Delay1000ms();
//if(T10mS)
{
T10mS=0;
i++;
//if(i==20)
{
Uart2SendStr("Uart Test !\r\n");
i=0;
_nop_();_nop_();_nop_();_nop_();_nop_();
UART1(0x55);
}
}
}
}
/* void TM1_Isr() interrupt 3 using 1
{
T10mS=1;//1mS
} */
|