给个例子你自己看看。
void sys_Init ( void )
{
SCON = 0x50; /* mode 1:8bit UART;enable receiver */
PCON |= 0x80; /* SMOD = 1; BR = Timer1 OV */
TMOD |= 0x20; /* timer 1 mode 2: 8-Bit reload */
TMOD &= 0x2f; /* timer0 used by OS in Mode 0 */
// TH1 = 0xfd; /* F = 11.0592MHz; TH1 = 0xfd; BR =9600 */
TH1 = 0xf3; /* F = 24MHz; TH1 =0xf3; BR = 9600 */
IP = 0x1f;
TR1 = 1; /* timer 1 run */
ES = 1; /* enable serial port interrupt */
IT0 = 1;
IE0 = 0; /* Clear flag */
IT1 = 1;
IE1 = 0; /* Clear flag */
EX1 = 1;
EX0 = 1;
// T2MOD = 0x00;
RCLK = 0;
TCLK = 0;
EXEN2 = 0;
C_T2 = 0;
CP_RL2 = 0;
TF2 = 0;
EXF2 = 0;
RCAP2L = 0x00;
RCAP2H = 0xC8;
TL2 = RCAP2L;
TH2 = RCAP2H;
ET2 = 1; /* Enable Timer2 in Mode 1 */
WDTD = 0xe0;
WDTC = 0x0f;
TR2 = 1;
return;
}
void sp_tx_sem_take ( void )
{
sp_tx_sem0:
EA = 0;
if ( sp_tx_sem == SEM_FULL )
{
EA = 1;
os_wait ( K_TMO, 1, 0 );
goto sp_tx_sem0;
}
sp_tx_sem = SEM_FULL;
EA = 1;
return;
}
void sp_tx_sem_give ( void )
{
EA = 0;
sp_tx_sem = SEM_EMPTY;
EA = 1;
return; // OK
}
void sp_tx_start ( void )
{
SBUF = SP_STX;
return;
}
void sp_rx_sem_give ( void )
{
EA = 0;
sp_rx_sem = SEM_EMPTY;
sp_rx_len = 0;
EA = 1;
return; // OK
}
void serial_int ( void ) interrupt 4 using 2
{
static unsigned char rx_cnt = 0; /* State: idle, initial state */
static unsigned char tx_cnt = 0;
if ( TI )
{
if ( sp_tx_sem == SEM_FULL )
{
switch ( tx_cnt )
{
case 0x00:
tx_cnt ++;
SBUF = SP_ADDR_DVR;
break;
case 0x01:
tx_cnt ++;
SBUF = sp_tx_addr;
break;
case 0x02:
tx_cnt ++;
SBUF = sp_tx_cmd;
break;
case 0x03:
if ( sp_tx_len > 0 )
{
sp_tx_len --;
SBUF = sp_tx_buffer [ sp_tx_len ];
}
if ( sp_tx_len == 0 )
tx_cnt ++;
break;
case 0x04:
tx_cnt ++;
SBUF = SP_ETX;
break;
default:
tx_cnt = 0x00;
sp_tx_len = 0x00;
sp_tx_sem_give (); /* Transmission Complete, Release Semaphore */
break;
}
TI = 0; /* Clear Interrupt Flag */
}
}
if ( RI ) /* Receive interrupt, clear flag RI, handled */
{
if ( sp_rx_sem == SEM_EMPTY )
{
switch ( rx_cnt )
{
case 0x00 :
if ( SBUF == SP_ADDR_KB )
rx_cnt = 0x01;
else
rx_cnt = 0x00;
break;
case 0x01 :
if ( SBUF == SP_ADDR_DVR )
rx_cnt = 0x02;
else
rx_cnt = 0x00;
break;
case 0x02 :
if ( SBUF == SP_STX )
{
rx_cnt = 0x00;
break;
}
sp_rx_cmd = SBUF;
rx_cnt = 0x03;
break;
case 0x03 :
if ( SBUF == SP_STX )
{
rx_cnt = 0x00;
sp_rx_len = 0x00;
break;
}
if ( SBUF == SP_ETX )
{
rx_cnt = 0x00;
sp_rx_sem = SEM_FULL;
isr_send_signal ( SP_RX );
}
else
{
sp_rx_buffer [ sp_rx_len ] = SBUF;
sp_rx_len ++;
}
break;
default : /* Machine state return idle state */
rx_cnt = 0x00;
sp_rx_len = 0x00;
break;
}
}
RI = 0; /* Clear Interrupt Flag */
}
return;
} |