1 //----------------------------------------------------------------------------
2 //Written by Dmitry S. Obukhov, 1996
4 //----------------------------------------------------------------------------
5 //This module implements serial interrupt handler and IO routinwes using
6 //two 256 byte cyclic buffers. Bit variables can be used as flags for
7 //real-time kernel tasks
8 //Last modified 6 Apr 97
9 //----------------------------------------------------------------------------
11 //This module contains definition of I8051 registers
15 static unsigned char __xdata stx_index_in, srx_index_in, stx_index_out, srx_index_out;
16 static unsigned char __xdata stx_buffer[0x100];
17 static unsigned char __xdata srx_buffer[0x100];
19 static __bit work_flag_byte_arrived;
20 static __bit work_flag_buffer_transfered;
21 static __bit tx_serial_buffer_empty;
22 static __bit rx_serial_buffer_empty;
25 void serial_init(void)
37 stx_index_in = srx_index_in = stx_index_out = srx_index_out = 0;
38 rx_serial_buffer_empty = tx_serial_buffer_empty = 1;
39 work_flag_buffer_transfered = 0;
40 work_flag_byte_arrived = 0;
44 void serial_interrupt_handler(void) __interrupt 4 __using 1
50 srx_buffer[srx_index_in++]=SBUF;
51 work_flag_byte_arrived = 1;
52 rx_serial_buffer_empty = 0;
57 if (stx_index_out == stx_index_in )
59 tx_serial_buffer_empty = 1;
60 work_flag_buffer_transfered = 1;
62 else SBUF = stx_buffer[stx_index_out++];
67 //Next two functions are interface
69 void serial_putc(unsigned char c)
71 stx_buffer[stx_index_in++]=c;
73 if ( tx_serial_buffer_empty )
75 tx_serial_buffer_empty = 0;
81 unsigned char serial_getc(void)
83 unsigned char tmp = srx_buffer[srx_index_out++];
85 if ( srx_index_out == srx_index_in) rx_serial_buffer_empty = 1;