設定8051串列中斷,並開啟Time0計時中斷
//設定中斷模式
void init_rs232(void)
{
IE=0x9A; //開啟time0計時中斷 time1串列中斷 關閉外部中斷
// ES=1; EA=1; ET0=1; ET1=1;
IP=0x02; //設定time0計時中斷 權限 高於time1串列中斷
TMOD = 0x21; //time0 mode1 time1 mode2
SCON = 0x50; //設定模式 串列中斷mode1 鮑率可變 REN=1 RI = 0; TI = 0;
PCON = 0x00; //清除SMOD
TH1 = 253; //設定鮑率為9600 11.059MHz
TR1 = 1; //啟動time1產生鮑率
}
//RX接收副程式
char UART_RxChar()
{
while(RI==0); // Wait till the data is received
RI=0; // Clear Receive Interrupt Flag for next cycle
return SBUF; // return the received char
}
//TX傳輸副程式
void UART_TxChar(char ch)
{
SBUF=ch; // Load the data to be transmitted
while(TI==0); // Wait till the data is trasmitted
TI=0; //Clear the flag for next cycle.
}