/*---------------------------------------------------------------------*/
/* --- STC MCU Limited ------------------------------------------------*/
/* --- STC15F4K60S4 系列 定时器2用作串口2的波特率发生器举例------------*/
/* --- Mobile: (86)13922805190 ----------------------------------------*/
/* --- Fax: 86-0513-55012956,55012947,55012969 ------------------------*/
/* --- Tel: 86-0513-55012928,55012929,55012966-------------------------*/
/* --- Web: www.STCMCU.com --------------------------------------------*/
/* --- Web: www.GXWMCU.com --------------------------------------------*/
/* 如果要在程序中使用此代码,请在程序中注明使用了STC的资料及程序 */
/* 如果要在文章中应用此代码,请在文章中注明使用了STC的资料及程序 */
/*---------------------------------------------------------------------*/
//本示例在Keil开发环境下请选择Intel的8058芯片型号进行编译
//若无特别说明,工作频率一般为11.0592MHz
#include "STC15Fxxxx.h"
#include "intrins.h"
#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
bit busy;
unsigned char dat[20];
u8 m;
void Uart2SendString(char *str);
void Uart2Send(unsigned char ch);
void uart2_init()
{
P_SW2 &= ~S2_S0; //S2_S0=0 (P1.0/RxD2, P1.1/TxD2)
S2CON = 0x50; //8???,?????
AUXR |= 0x04; //???2???Fosc,?1T
T2L = 0xE8; //??????
T2H = 0xFF; //??????
AUXR |= 0x10; //?????2
IE2 = 0x01; //使能串口2中断
}
void main()
{
uart2_init();
Uart2SendString("STC15F2K60S2\r\nUart2 Test !\r\n");
EA = 1;
while(1);
}
/*----------------------------
UART2 中断服务程序
-----------------------------*/
void Uart2() interrupt 8 using 1
{
if (S2CON & S2RI)
{
S2CON &= ~S2RI; //清除S2RI位
dat[m++]=S2BUF;
if(dat[m-1]=='\n')
{
m=0;
Uart2SendString(dat);
for(m=0;dat[m]!='\n';m++)
{
dat[m]=0;
}
m=0;
}
}
if (S2CON & S2TI)
{
S2CON &= ~S2TI; //清除S2TI位
busy = 0; //清忙标志
}
}
void Uart2Send(unsigned char ch)//发送单个字符
{
S2BUF=ch; //送入缓冲区
while(!(S2CON & S2TI)); //等待发送完毕
S2CON &= ~S2TI; //清除S2TI位
busy = 1;
}
/////////////////串口2发送字符串/////////////////////////
void Uart2SendString(char *str)
{
while (*str) //检测字符串结束标志
{
Uart2Send(*str++); //串口2发送当前字符
}
}