解決了 聯系的stc工程師 使用stc15fxxxx.h
//========================================================================
// 函數: void UART1_config(u32 brt, u8 timer, u8 io)
// 描述: UART1初始化函數。
// 參數: brt: 通信波特率.
// timer: 波特率使用的定時器, timer=2: 波特率使用定時器2, 其它值: 使用Timer1做波特率.
// io: 串口1切換到的IO, io=1: 串口1切換到P3.0 P3.1, =1: 切換到P3.6 P3.7, =2: 切換到P1.6 P1.7, =3: 切換到P4.3 P4.4.
//========================================================================
void UART1_config(u32 brt, u8 timer, u8 io) // brt: 通信波特率, timer=2: 使用定時器2做波特率發生器, 其余值: 使用定時器1做波特率發生器
// io=0: 串口切換到P3.0 P3.1, 1: 串口切換到P3.6 P3.7, 2: 串口切換到P1.6 P1.7
{
brt = 65536UL - (MAIN_Fosc / 4) / brt;
if(timer == 2) //波特率使用定時器2
{
AUXR |= 0x01; //S1 BRT Use Timer2;
SetTimer2Baudraye((u16)brt);
}
else //波特率使用定時器1
{
TR1 = 0;
AUXR &= ~0x01; //S1 BRT Use Timer1;
AUXR |= (1<<6); //Timer1 set as 1T mode
TMOD &= ~(1<<6); //Timer1 set As Timer
TMOD &= ~0x30; //Timer1_16bitAutoReload;
TH1 = (u8)(brt >> 8);
TL1 = (u8)brt;
ET1 = 0; // 禁止Timer1中斷
INT_CLKO &= ~0x02; // Timer1不輸出高速時鐘
TR1 = 1; // 運行Timer1
}
S1_8bit(); //頭文件的宏定義, 8位數據, 1位起始位, 1位停止位, 無校驗
if(io == 1) { S1_USE_P36P37(); P3n_standard(0xc0); } //UART1 使用P36 P37口
else if(io == 2) { S1_USE_P16P17(); P1n_standard(0xc0); } //UART1 使用P16 P17口
else { S1_USE_P30P31(); P3n_standard(0x03); } //UART1 使用P30 P31口
REN = 1; //允許接收
ES = 1; //允許中斷
}
|