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 #include "reg51.h"//This module contains definition of I8051 registers
14 static unsigned char xdata stx_index_in, srx_index_in, stx_index_out, srx_index_out;
15 static unsigned char xdata stx_buffer[0x100];
16 static unsigned char xdata srx_buffer[0x100];
18 static bit work_flag_byte_arrived;
19 static bit work_flag_buffer_transfered;
20 static bit tx_serial_buffer_empty;
21 static bit rx_serial_buffer_empty;
24 void serial_init(void)
36 stx_index_in = srx_index_in = stx_index_out = srx_index_out = 0;
37 rx_serial_buffer_empty = tx_serial_buffer_empty = 1;
38 work_flag_buffer_transfered = 0;
39 work_flag_byte_arrived = 0;
43 void serial_interrupt_handler(void) interrupt 4 using 1
49 if (stx_index_out == stx_index_in )
51 tx_serial_buffer_empty = 1;
52 work_flag_buffer_transfered = 1;
54 else SBUF = stx_buffer[stx_index_out++];
59 srx_buffer[srx_index_in++]=SBUF;
60 work_flag_byte_arrived = 1;
61 rx_serial_buffer_empty = 0;
66 //Next two functions are interface
68 void serial_putc(unsigned char c)
70 stx_buffer[stx_index_in++]=c;
72 if ( tx_serial_buffer_empty )
74 tx_serial_buffer_empty = 0;
80 unsigned char serial_getc(void)
82 unsigned char tmp = srx_buffer[srx_index_out++];
84 if ( srx_index_out == srx_index_in) rx_serial_buffer_empty = 1;