VOID uart1_init()
{
UINT8 SFRPAGE_save = SFRPAGE;
SFRPAGE = ACTIVE2_PAGE;
SCON1 = 0x10; // SCON1: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI1 and TI1 bits
P3 &= 0xDF;
if (SYSCLK / BAUDRATE / 2 / 256 < 1)
{
TH1 = -(SYSCLK / BAUDRATE / 2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = 11
CKCON |= 0x08;
}
else if (SYSCLK / BAUDRATE / 2 / 256 < 4)
{
TH1 = -(SYSCLK / BAUDRATE / 2 / 4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x01;
}
else if (SYSCLK / BAUDRATE / 2 / 256 < 12)
{
TH1 = -(SYSCLK / BAUDRATE / 2 / 12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
}
else
{
TH1 = -(SYSCLK / BAUDRATE / 2 / 48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1; // Init Timer1
TMOD &= ~0xF0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
// TI1 = 1; // Indicate TX1 ready (SCON1)
// EA = 1;
EIE2 |= 0x08;
SFRPAGE = SFRPAGE_save;
}
INTERRUPT(UART1_ISR, INTERRUPT_UART1)
{
//
// EIE2 &= 0x00;
if((SCON1 & 0x01)==0x01) //如果接收中断标志RI1=1
{
// SCON1 &= ~0x01; /*清接收标志位*/
P3 &= 0xDF; //P3.5=0,低--接收
S_Uart1_Buffer[S_Uart1_Rx] = SBUF1&0xff;
if( S_Uart1_Buffer[0] == 0x01)
{
S_Uart1_Rx = S_Uart1_Rx+1;
}
else
{
S_Uart1_Rx = 0;
}
if(S_Uart1_Rx == 8)
{
S_Uart1_Sta = 1; //
}
}
SCON1 &= ~0x01;
// SCON1 &= ~0x02;
// EIE2 |= 0x08; /*清发送标志位*/
}
VOID c8051_port_init(VOID)
{
UINT8 SFRPAGE_save = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
P0MDIN = 0xF2;
P1MDIN = 0xF9;
P2MDIN = 0xFC;
P0MDOUT = 0xF2;
P1MDOUT = 0xC9;
P2MDOUT = 0xF8;
P3MDOUT = 0xDF;
P0SKIP = 0xFF;
P1SKIP = 0xFF;
P2SKIP = 0xF3;
XBR2 = 0x42;
SFRPAGE = SFRPAGE_save;
} |