Znn3bq.jpeg
²é¿´: 148  |  »Ø¸´: 0
µ±Ç°Ö÷ÌâÒѾ­´æµµ¡£

sdlj8051

½ð³æ (ÖøÃûдÊÖ)


[×ÊÔ´] Ä£Äâ´®¿ÚµÄ³ÌÐò

CODE:
// UART.C
//
// Generic software uart written in C, requiring a timer set to 3 times
// the baud rate, and two software read/write pins for the receive and
// transmit functions.
//
// * Received characters are buffered
// * putchar(), getchar(), kbhit() and flush_input_buffer() are available
// * There is a facility for background processing while waiting for input
//
// Colin Gittins, Software Engineer, Halliburton Energy Services
//
// The baud rate can be configured by changing the BAUD_RATE macro as
// follows:
//
// #define BAUD_RATE 19200.0
//
// The function init_uart() must be called before any comms can take place
//
// Interface routines required:
// 1. get_rx_pin_status()
//    Returns 0 or 1 dependent on whether the receive pin is high or low.
// 2. set_tx_pin_high()
//    Sets the transmit pin to the high state.
// 3. set_tx_pin_low()
//    Sets the transmit pin to the low state.
// 4. idle()
//    Background functions to execute while waiting for input.
// 5. timer_set( BAUD_RATE )
//    Sets the timer to 3 times the baud rate.
// 6. set_timer_interrupt( timer_isr )
//    Enables the timer interrupt.
//
// Functions provided:
// 1. void flush_input_buffer( void )
//    Clears the contents of the input buffer.
// 2. char kbhit( void )
//    Tests whether an input character has been received.
// 3. char getchar( void )
//    Reads a character from the input buffer, waiting if necessary.
// 4. void turn_rx_on( void )
//    Turns on the receive function.
// 5. void turn_rx_off( void )
//    Turns off the receive function.
// 6. void putchar( char )
//    Writes a character to the serial port.

#include

#define BAUD_RATE 19200.0

#define IN_BUF_SIZE 256

#define TRUE 1
#define FALSE 0

static unsigned char inbuf[IN_BUF_SIZE];
static unsigned char qin = 0;
static unsigned char qout = 0;

static char  flag_rx_waiting_for_stop_bit;
static char  flag_rx_off;
static char  rx_mask;
static char  flag_rx_ready;
static char  flag_tx_ready;
static char  timer_rx_ctr;
static char  timer_tx_ctr;
static char  bits_left_in_rx;
static char  bits_left_in_tx;
static char  rx_num_of_bits;
static char  tx_num_of_bits;
static char internal_rx_buffer;
static char internal_tx_buffer;
static char user_tx_buffer;

void timer_isr(void)
{
char mask, start_bit, flag_in;

// Transmitter Section
if ( flag_tx_ready )
{
if ( --timer_tx_ctr<=0 )
{
mask = internal_tx_buffer&1;
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;
}
}
}
// Receiver Section
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();
// Test for Start Bit
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 // rx_busy
{
if ( --timer_rx_ctr<=0 )
{  
// rcv
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;
}
}
}
}
}
}

void init_uart( void )
{
flag_tx_ready = FALSE;
flag_rx_ready = FALSE;
flag_rx_waiting_for_stop_bit = FALSE;
flag_rx_off = FALSE;
rx_num_of_bits = 10;
tx_num_of_bits = 10;

set_tx_pin_low();

timer_set( BAUD_RATE );
set_timer_interrupt( timer_isr );  // Enable timer interrupt
}

char _getchar( void )
{
char ch;

do
{
while ( qout==qin )
{
idle();
}
ch = inbuf[qout] & 0xFF;
if ( ++qout>=IN_BUF_SIZE )
{
qout = 0;
}
}
while ( ch==0x0A || ch==0xC2 );
return( ch );
}

void _putchar( char ch )
{
while ( flag_tx_ready );
user_tx_buffer = ch;

// invoke_UART_transmit
timer_tx_ctr = 3;
bits_left_in_tx = tx_num_of_bits;
internal_tx_buffer = (user_tx_buffer<<1) | 0x200;
flag_tx_ready = TRUE;
}

void flush_input_buffer( void )
{
qin = 0;
qout = 0;
}

char kbhit( void )
{
return( qin!=qout );
}

void turn_rx_on( void )
{
flag_rx_off = FALSE;
}

void turn_rx_off( void )
{
flag_rx_off = TRUE;
}

  




¡¡

[ Last edited by sdlj8051 on 2007-1-8 at 01:50 ]
»Ø¸´´ËÂ¥

» ²ÂÄãϲ»¶

ÒÑÔÄ   »Ø¸´´ËÂ¥   ¹Ø×¢TA ¸øTA·¢ÏûÏ¢ ËÍTAºì»¨ TAµÄ»ØÌû
Ïà¹Ø°æ¿éÌø×ª ÎÒÒª¶©ÔÄÂ¥Ö÷ sdlj8051 µÄÖ÷Ìâ¸üÐÂ
¡î ÎÞÐǼ¶ ¡ï Ò»ÐǼ¶ ¡ï¡ï¡ï ÈýÐǼ¶ ¡ï¡ï¡ï¡ï¡ï ÎåÐǼ¶
×î¾ßÈËÆøÈÈÌûÍÆ¼ö [²é¿´È«²¿] ×÷Õß »Ø/¿´ ×îºó·¢±í
[¿¼ÑÐ] Ò»Ö¾Ô¸»ª¹¤085600 331·Ö +7 ÌìÏÂww 2026-04-09 7/350 2026-04-13 09:01 by lhj2009
[¿¼ÑÐ] 0854µ÷¼Á +10 ³¤¹­°Á 2026-04-12 12/600 2026-04-13 08:49 by Sammy2
[¿¼ÑÐ] Çóµ÷¼Á +9 ³Ô¿Ú±ù¼¤Áè 2026-04-07 9/450 2026-04-13 08:46 by ×ÏêØ×ÏÆå
[¿¼ÑÐ] ҩѧÇóµ÷¼Á +3 RussHu 2026-04-12 4/200 2026-04-12 17:49 by ³ÂƤƤ
[˶²©¼ÒÔ°] ÐÂÒ»´úµç×ÓÐÅÏ¢294Çóµ÷¼Á ²»ÌôѧУ +7 Ytyt11 2026-04-09 8/400 2026-04-12 16:57 by ajpv·çÀ×
[¿¼ÑÐ] µ÷¼Á +25 ²»·ê´º 2026-04-07 26/1300 2026-04-12 11:53 by ´óÁ¦Ë®ÊÖÁ¦´óÎÞÇ
[¿¼ÑÐ] 085410 273Çóµ÷¼Á +10 X1999 2026-04-09 10/500 2026-04-12 09:24 by ÄæË®³Ë·ç
[¿¼ÑÐ] 331Çóµ÷¼Á +5 Íõ¹ú˧ 2026-04-11 5/250 2026-04-11 22:56 by Ϫ½§Á÷Ë®
[¿¼ÑÐ] Çóµ÷¼Á +6 archer.. 2026-04-09 8/400 2026-04-11 10:55 by zhq0425
[¿¼ÑÐ] Çóµ÷¼Á +5 ²»»á·ÉµÄÓã@ 2026-04-10 5/250 2026-04-10 19:07 by chemisry
[¿¼ÑÐ] Ò»Ö¾Ô¸»ª¶«Ê¦·¶ÉúÎïѧ326·Ö£¬Çóµ÷¼Á +8 Áõīī 2026-04-09 8/400 2026-04-10 12:00 by pengliang8036
[¿¼ÑÐ] »úе»¹Óл¹ÓÐÃû¶îÂð£¿Ì«ÄÑÁË +6 ЦЦԬ 2026-04-10 6/300 2026-04-10 11:54 by ¸ßά´º
[¿¼ÑÐ] 0703»¯Ñ§Çóµ÷¼Á +21 ²»ÖªÃûµÄСئ 2026-04-08 21/1050 2026-04-09 18:55 by l_paradox
[¿¼ÑÐ] 274Çóµ÷¼Á +5 ɽ°¢Âû 2026-04-07 5/250 2026-04-09 15:28 by 18828373951
[¿¼ÑÐ] 265Çóµ÷¼Á +4 ·ç˵ËýÔçÍüÁË 2026-04-07 4/200 2026-04-09 13:59 by onlyÖÜ
[¿¼ÑÐ] ²ÄÁÏ307·ÖÇó´óÀÐ×éÊÕÁô +17 Hllºú 2026-04-07 17/850 2026-04-09 10:53 by liuhuiying09
[¿¼ÑÐ] 286Çóµ÷¼Á +19 Faune 2026-04-08 20/1000 2026-04-09 08:36 by ŶŶ123
[¿¼ÑÐ] 275 Çóµ÷¼Á +8 Lei812514 2026-04-07 8/400 2026-04-08 12:46 by chemisry
[¿¼ÑÐ] 22408 266Çóµ÷¼Á +11 masss11222 2026-04-07 14/700 2026-04-08 11:06 by yulian1987
[¿¼ÑÐ] 11408 325·Ö +3 jgtxuxgkx 2026-04-07 3/150 2026-04-07 23:10 by lbsjt
ÐÅÏ¢Ìáʾ
ÇëÌî´¦ÀíÒâ¼û