标签:
#include "reg51.h"
#define FOSC 12000000L //晶振12M
#define BAUD 9600
#define NONE_PARITY 0 //无校验位
#define ODD_PARITY 1 //奇校验
#define EVEN_PARITY 2 //偶校验
#define MARK_PARITY 3 //标记校验
#define SPACE_PARITY 4 //空校验
#define PARITYBIT EVEN_PARITY
#define S2RI 0x01
#define S2TI 0x02
#define S2RB8 0x04
#define S2TB8 0x08
sfr AUXR = 0x8e;
sfr S2CON = 0x9a;
sfr S2BUF = 0x9b;
sfr BRT = 0x9c;
sfr IE2 = 0xaf;
bit busy;
void SendData(char dat);
void SendString(char *s);
unsigned char string[]={"hello world!\r\n"};
void main()
{
#if (PARITYBIT == NONE_PARITY)
S2CON = 0x5a; //8位可变波特率 (无校验位)
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
S2CON = 0xda; //9位可变波特率,校验位初始为1
#elif (PARITYBIT == SPACE_PARITY)
S2CON = 0xd5; //9位可变波特率,校验位初始为0
#endif
//----串口1初始化------------------------------
SCON=0x40; //设置串口方式1,REN=0;
PCON=0x80; //波特率加倍
TMOD=0x20; //定时器工作于方式2
TH1=0xf3; //波特率4800
TR1=1; //开启定时器1
//---------------串口2初始化-------------
BRT = -(FOSC/32/BAUD); //设置独立波特率发生器的重载初值
AUXR = 0x14; //独立波特率发生器工作在1T模式
IE2 = 0x01; //使能串口2中断
EA = 1; //开总中断
SendString("STC12C5A60S2\r\nUart2 Test !\r\n"); //串口2发送
while(1)
{
unsigned char i=0;
while(string[i]) //等待字符串发完 //串口1发送
{
SBUF= string[i++]; //数据装入缓冲区
while(TI==0); //等待发送完
TI=0; //清除发送标志位
}
}
while(1);
}
void Uart2() interrupt 8 using 1
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //清除接收完成标志
P0 = S2BUF; //P0显示串口数据
P2 = (S2CON & S2RB8); //P2.2显示校验位
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //清除发送完成标志
busy = 0;
}
}
void SendData(char dat)
{
while (busy); //等待上个数据发送完成
ACC = dat; //取得偶校验位P
if (P) //根据P来设置串口数据的校验位
{富婆找男人 www.suhe0.com/
#if (PARITYBIT == ODD_PARITY)
S2CON &= ~S2TB8; //置校验位为0
#elif (PARITYBIT == EVEN_PARITY)
S2CON |= S2TB8; //置校验位为1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
S2CON |= S2TB8; //置校验位为1
#elif (PARITYBIT == EVEN_PARITY)
S2CON &= ~S2TB8; //置校验位为0
#endif
}
busy = 1;
S2BUF = ACC; //发送数据
}
void SendString(char *s)
{
while (*s) //判断字符串结束标志
{
SendData(*s++); //发送字符
}
}
标签:
原文地址:http://my.oschina.net/pwd/blog/412077