/***************************************
*函數(shù):main.c
*描述:
* 串口通訊協(xié)議標(biāo)準(zhǔn)測(cè)試:發(fā)送01234567
*時(shí)間:
*編寫:
*版本:V1.0
****************************************/
#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
#define FSOC 11059200L //晶振
#define BAUD 9600 //波特率
uchar RevBuf; //保存串口接收到的數(shù)據(jù),數(shù)據(jù)緩沖區(qū)
void SerialInit(void) //串口初始化
{
EA = 0;
SCON = 0x50; //SCON0:mode0,8-Bit,UART,Enable RXD
PCON |= 0x80; //SMOD00 =1;
TMOD = 0x20 ; //TMOD:timer1,mode2,8-bit relaod
TH1 = -(FSOC/BAUD/12/16); //set Timer1 reload value for baudrate
TL1 = -(FSOC/BAUD/12/16); //set Timer1 reload value for baudrate
TR1 =1;//start Timer1
//ES = 1; //enable UART0 interupt
EA = 1;
}
/*
//Reeive Char "串口中斷方式接受數(shù)據(jù)"
//功能:接收到數(shù)據(jù)之后再發(fā)回去
void UART_INTR_SVC(void)interrupt 4 using 3
{
if(RI==1)
{
RI =0;
RevBuf =SBUF;
SBUF = RevBuf;
}
else
{
TI = 0;
}
}
*/
void main(void)
{
uchar m,n; //定義循環(huán)變量
SerialInit(); //初始化串口
//send('a');
for(m=0;m<10;m++)
{
SBUF = m+0x30;
while(TI!=1);
TI=0;
} //向電腦發(fā)送數(shù)據(jù)0123456789
SBUF = 0x0A; //換行符號(hào)的ASCII碼
while(TI!=1);
TI=0;
for(n=0;n<10;n++)
{
SBUF = 0x40; //@的ASCII碼
while(TI!=1);
TI=0;
}
SBUF = 0x0A;
while(TI!=1);
TI=0;
for(;;)
{
while(RI!=1);
RI=0;
RevBuf = SBUF; //查詢方法接收數(shù)據(jù)
SBUF = RevBuf; //原數(shù)據(jù)發(fā)回
while(TI!=1);
TI=0;
}
}