#include "STC15F2K60S2.H"
#define Lenth 64
sbit k1=P1^0;
typedef unsigned char u8;
u8 xdata RX1_Buffer[Lenth];
u8 xdata TX1_Buffer[Lenth];
u8 TX1_c;
u8 RX1_c;
u8 TX1_write;
u8 TX1_read;
bit TX1_Busy;
void TX1_write2buff(u8 dat) //写入发送缓冲,指针+1
{
TX1_Buffer[TX1_write] = dat; //装发送缓冲
if(++TX1_write >=Lenth) TX1_write = 0;
if(TX1_Busy == 0) //空闲
{
TX1_Busy = 1; //标志忙
TI = 1; //触发中断
}
}
void PrintString1(u8 *puts)
{
for (; *puts != 0; puts++)
TX1_write2buff(*puts); //遇到停止符0结束
}
void main()
{
TX1_Busy=0;
RX1_c=0;
TX1_c=0;
TX1_write=0;
TX1_read=0;
SCON = 0x50; //
AUXR |= 0x01; /
AUXR |= 0x04; //
T2L = 0xE0; //
T2H = 0xFE; //
AUXR |= 0x10; //
REN = 1; //允许接收
ES = 1; //允许中断
EA = 1; //允许全局中断
while(1)
{
if(k1==0)
{
PrintString1("你好世界\n\r");
}
}
}
void UART_1() interrupt 4
{
if(RI)
{
RI=0;
RX1_Buffer[RX1_c]=SBUF;
if(++RX1_c>=Lenth) RX1_c=0;
}
if(TI)
{
TI=0;
if(TX1_read!=TX1_write)
{
SBUF=TX1_Buffer[TX1_read];
if(++TX1_read>=Lenth) TX1_read=0;
}
TX1_Busy=0;
}
}
Ps:波特率没选错,9600,
|