STC15F2K60S2串口2程序
#include "reg51.h"
sfr P4 = 0xc0;
sfr P5 = 0xc8;
sfr AUXR = 0x8e ;
sfr S2CON = 0x9a ;
sfr S2BUF = 0x9b ;
sfr T2H = 0xd6 ;
sfr T2L = 0xd7 ;
sfr IE2 = 0xaf ;
sfr IP2 = 0xb5 ;
#define S2RI 0x01 // S2CON.0
#define S2TI 0x02 // S2CON.1
unsigned char temp ;
bit flag1 = 0 , flag2 = 0 , busy = 0 ;
unsigned char xdata Uart1TxBuffer [20] = "杜興杰你必須加油個(gè)"; //
void Uart2Init(void) //9600bps@11.0592MHz
{
S2CON = 0x50; //8位數(shù)據(jù),可變波特率
AUXR |= 0x04; //定時(shí)器2時(shí)鐘為Fosc,即1T
T2L = 0xE0; //設(shè)定定時(shí)初值
T2H = 0xFE; //設(shè)定定時(shí)初值
AUXR |= 0x14; //啟動(dòng)定時(shí)器2 4 注意這里
EA = 1 ;
IE2 = 0x01 ; //開(kāi)啟串口中斷ES2
IP2 |= 0x00 ; //串口2優(yōu)先級(jí)0
}
void main( void )
char i ;
P0 = 0 ;
Uart2Init() ;
SendData (C) ;
SendData (C) ;
for ( i = 0 ; i < 20 ; i++ )
}
for ( i = 0 ; i < 20 ; i++ )
}
SendData (C) ;
SendData (C) ;
while(1)
}
}
}
void Uart2( void ) interrupt 8
if( S2CON & S2RI )
}
}
}
// while ( !( S2CON & 0x02 ) ) ;
下一步是把串口1和串口2 加在一起,還有就是加入循環(huán)隊(duì)列實(shí)現(xiàn)流控等
評(píng)論