UART2_config(2); // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
EA = 1; //允许全局中断
void SetTimer2Baudraye(u16 dat) // 选择波特率, 2: 使用Timer2做波特率, 其它值: 使用Timer1做波特率.
{
AUXR &= ~(1<<4); //Timer stop
AUXR &= ~(1<<3); //Timer2 set As Timer
AUXR |= (1<<2); //Timer2 set as 1T mode
TH2 = dat / 256;
TL2 = dat % 256;
IE2 &= ~(1<<2); //禁止中断
AUXR |= (1<<4); //Timer run enable
}
void UART2_config(u8 brt) // 选择波特率, 2: 使用Timer2做波特率, 其它值: 无效.
{
/*********** 波特率固定使用定时器2 *****************/
if(brt == 2)
{
SetTimer2Baudraye(65536UL - (MAIN_Fosc / 4) / Baudrate2);
S2CON &= ~(1<<7); // 8位数据, 1位起始位, 1位停止位, 无校验
IE2 |= 1; //允许中断
S2CON |= (1<<4); //允许接收
P_SW2 &= ~0x01;
P_SW2 |= 1; //UART2 switch to: 0: P1.0 P1.1, 1: P4.6 P4.7
B_TX2_Busy = 0;
TX2_Cnt = 0;
RX2_Cnt = 0;
}
}
void UART2_int (void) interrupt UART2_VECTOR
{
if((S2CON & 1) != 0)
{
S2CON &= ~1; //Clear Rx flag
RX2_Buffer[RX2_Cnt] = S2BUF;
if(++RX2_Cnt >= UART2_BUF_LENGTH) RX2_Cnt = 0;
}
if((S2CON & 2) != 0)
{
S2CON &= ~2; //Clear Tx flag
B_TX2_Busy = 0;
}
}
|