#include<STC12C5A56S2.H>
#include<intrins.h>
#define RELOAD_COUNT 0xfb //18.432MHz,12T,SMOD=0,9600bps
#define RECV_LEN 27 //接收报文长度27个字节
char recv[27];
unsigned char temp_len=0; //接收报文计数器
void UART_one_Init(void) //[email protected]
{
SCON = 0x50; //0101,0000 8位可变波特率,无奇偶校验位
// TMOD = 0x21; //0011,0001 设置顶时器1为8位自动重装计数器
// TH1 = RELOAD_COUNT; //设置定时器1自动重装数
// TL1 = RELOAD_COUNT;
// TR1 = 1; //开定时器1
BRT = RELOAD_COUNT;
// BRTR = 1, S1BRS = 1, EXTRAM = 1 ENABLE EXTRAM
AUXR = 0x11; // T0x12,T1x12,UART_M0x6,BRTR,S2SMOD,BRTx12,EXTRAM,S1BRS
ES = 1; //允许串口中断
EA = 1; //开总中断
}
void UART_two_Init(void) //[email protected]
{
/* AUXR &= 0xF7; //波特率不倍速
S2CON = 0x50; //8位数据,可变波特率
AUXR &= 0xFB; //独立波特率发生器时钟为Fosc/12,即12T
BRT = 0xFD; //设定独立波特率发生器重装值
AUXR |= 0x10; //启动独立波特率发生器
IE2 = 0x01; //允许串口中断
EA = 1; //开总中断 */
}
void uart_one_senddata(unsigned char i)
{
ES = 0; //关串口中断
TI = 0; //清零串口发送完成中断请求标志
SBUF = i;
while(TI ==0); //等待发送完成
TI = 0; //清零串口发送完成中断请求标志
ES = 1; //允许串口中断
}
void uart_one_sendstring(unsigned char *s)
{
while(*s)
{
SBUF=*s;
while(!TI);
TI=0;
s++;
}
}
void UART_one_Interrupt_Receive(void) interrupt 4
{
unsigned char k = 0;
if(RI==1)
{
RI = 0;
k = SBUF;
P21=~P21;
uart_one_senddata(k);
}
else
{
TI = 0;
}
}
void send_UART_two(unsigned char i)
{
//sfr SCON = 0x98;
//SM0,SM1,SM2,REN,TB8,RB8,TI,RI
//sfr S2CON = 0x9A;
//S2SM0,S2SM1,S2SM2,S2REN,S2TB8,S2RB8,S2TI,S2RI
//sfr S2BUF = 0x9B;
//sfr IE2 = 0xAF;
//X,X,X,X,X,X,ESPI,ES2
unsigned char temp = 0;
// ES = 0; //关串口1中断
IE2 = 0x00; //关串口2中断,es2=0
// TI = 0; //清零串口1发送完成中断请求标志
S2CON = S2CON & 0xFD; //B'11111101,清零串口2发送完成中断请求标志
// SBUF = i;
S2BUF = i;
// while(TI ==0); //等待发送完成
do
{
temp = S2CON;
temp = temp & 0x02;
}while(temp==0);
// TI = 0; //清零串口发送完成中断请求标志
S2CON = S2CON & 0xFD; //B'11111101,清零串口2发送完成中断请求标志
// ES = 1; //允许串口1中断
// ES2 = 1
IE2 = 0x01; //允许串口2中断,ES2=1
}
void uart_two_sendstring(unsigned char *s,unsigned char len)
{
unsigned char index=0;
while(index<len)
{
S2BUF=*s;
while(!S2CON & 0x02);
S2CON = S2CON & 0xFD;
s++;
index++;
}
}
void UART_two_Interrupt_Receive(void) interrupt 8
{
//sfr SCON = 0x98;
//SM0,SM1,SM2,REN,TB8,RB8,TI,RI
//sfr S2CON = 0x9A;
//S2SM0,S2SM1,S2SM2,S2REN,S2TB8,S2RB8,S2TI,S2RI
//sfr S2BUF = 0x9B;
//sfr IE2 = 0xAF;
//X,X,X,X,X,X,ESPI,ES2
unsigned char k = 0;
k = S2CON ;
k = k & 0x01;
//if(S2RI==1)
if(k==1)
{
//RI = 0;
S2CON = S2CON & 0xFE; //1111,1110
k = S2BUF;
P35=~P35;
}
else
{
//TI = 0;
S2CON = S2CON & 0xFD; //1111,1101
}
}