單片機(jī)通用模擬串口C程序
//-------------------------------------------------------------------------------------------------------
// 函數(shù)名稱: idle
// 功能說明: 等待定時(shí)器中斷時(shí)的執(zhí)行程序,與具體應(yīng)用有關(guān)
//--------------------------------------------------------------------------------------------------------
void idle()
{
}
//-------------------------------------------------------------------------------------------------------
// 函數(shù)名稱: timer_isr
// 功能說明: 中斷處理函數(shù),與硬件無關(guān),通用函數(shù)
//--------------------------------------------------------------------------------------------------------
void timer_isr(void) /*中斷服務(wù)程序*/
{
char mask, start_bit, flag_in;
//發(fā)送程序
if ( flag_tx_ready )
{
if ( --timer_tx_ctr=0 )
{
mask = internal_tx_buffer1;
internal_tx_buffer >>= 1;
if ( mask )
{
set_tx_pin_high();
}
else
{
set_tx_pin_low();
}
timer_tx_ctr = 3;
if ( --bits_left_in_tx=0 )
{
flag_tx_ready = FALSE;
}
}
}
// 接收程序
if ( flag_rx_off==FALSE )
{
if ( flag_rx_waiting_for_stop_bit )
{
if ( --timer_rx_ctr=0 )
{
flag_rx_waiting_for_stop_bit = FALSE;
flag_rx_ready = FALSE;
internal_rx_buffer = 0xFF;
if ( internal_rx_buffer!=0xC2 )
{
inbuf[qin] = internal_rx_buffer;
if ( ++qin>=IN_BUF_SIZE )
{
qin = 0;
}
}
}
}
else // rx_test_busy接收忙
{
if ( flag_rx_ready==FALSE )
{
start_bit = get_rx_pin_status();
// 判斷起始位
if ( start_bit==0 )
{
flag_rx_ready = TRUE;
internal_rx_buffer = 0;
timer_rx_ctr = 4;
bits_left_in_rx =
rx_num_of_bits;
rx_mask = 1;
}
}
else
{
if ( --timer_rx_ctr=0 )
{
timer_rx_ctr = 3;
flag_in =
get_rx_pin_status();
if ( flag_in )
{
internal_rx_buffer |= rx_mask;
}
rx_mask = 1;
if ( --bits_left_in_rx=0 )
{
flag_rx_waiting_for_stop_bit = TRUE;
}
}
}
}
}
}
評(píng)論