#include <STC15F2K60S2.H>
#include "intrins.h"
#define FOSC 11059200L //系统频率
#define BAUD 9600 //串口波特率
#define S2RI 0x01 //S2CON.0 不能进行位寻址
#define S2TI 0x02 //S2CON.1
#define S2RB8 0x04 //S2CON.2
#define S2TB8 0x08 //S2CON.3
#define S2_S0 0x01 //P_SW2.0
typedef unsigned char uchar;
typedef unsigned int uint;
bit busy;
bit Flag;
uchar GetC;
unsigned char a,i=0;
unsigned char String[32];
bit busy;
void delay(uint x) [url=]//@11.0592MHz[/url] x ms
{
unsigned char i, j;
while(x--)
{
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
}
while (--i);
}
}
/*************系统初始化****************/
void sys_init(void)
{
P20=1;
P32=1;
P33=1;
//P_SW2 &= ~S2_S0; //S2_S0=0 (P1.0/RxD2, P1.1/TxD2)
//P_SW2 |= S2_S0; //S2_S0=1 (P4.6/RxD2_2, P4.7/TxD2_2)
}
/*************串口2初始化************************/
void Uart2_Init(void)
{
S2CON = 0x50; //8位可变波特率
T2L = (65536 - (FOSC/4/BAUD)); //设置波特率重装值
T2H = (65536 - (FOSC/4/BAUD))>>8;
AUXR = 0x14; //T2为1T模式, 并启动定时器2
IE2 = 0x01; //使能串口2中断
}
/*----------------------------
UART1初始化
-----------------------------*/
void InitUART(void)
{
SCON = 0x50; //8位可变波特率
AUXR1= AUXR1 & 0x3F;
AUXR = 0x40; //定时器1为1T模式
TMOD = 0x20; //定时器1为模式2(8位自动重载)
TL1 = 0xDC; //设置波特率重装值
TH1 = 0xDC;
TR1 = 1; //定时器1开始工作
ES = 1; //使能串口中断
}
/*************发送发送字符************************/
void SendData(unsigned char dat)
{
while (busy); //等待前面的数据发送完成
busy = 1;
S2BUF =dat; //写数据到UART2数据寄存器
}
/************发送字符串************************/
void SendString(char *s)
{
while (*s) //检测字符串结束标志
{
SendData(*s++); //发送当前字符
}
}
/*----------------------------
发送串口数据
----------------------------*/
void SendData1(uchar dat)
{
while (busy); //等待前面的数据发送完成
busy = 1;
SBUF = dat; //写数据到UART数据寄存器
}
/*----------------------------
发送字符串
----------------------------*/
void SendString1(uchar *s)
{
while (*s) //检测字符串结束标志
{
SendData1(*s++); //发送当前字符
}
}
void main()
{
sys_init();
InitUART();
Uart2_Init();
EA = 1; //quanjuzhongduan
SendString("guowende ceshichengxu\r\n");
while(1)
{
if(Flag)
{
delay(200);
SendData1(GetC);
SendData(GetC);
Flag=0;
}
}
}
/************UART2 中断服务程序*******************/
void Uart2() interrupt 8 using 2
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //清除S2RI位
GetC= S2BUF; //P0显示串口数据
Flag=1;
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //清除S2TI位
busy = 0; //清忙标志
}
}
/*----------------------------
UART 中断服务程序
-----------------------------*/
void Uart() interrupt 4 using 1
{
if (RI)
{
RI = 0; //清除RI位
GetC = SBUF; //P0显示串口数据
Flag=1;
}
if (TI)
{
TI = 0; //清除TI位
busy = 0; //清忙标志
}
}
|