#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; //清忙標志
}
}
|