| ??? 09/18/06 21:58 Read: times |
#124520 - A Module Example Responding to: ???'s previous message |
Mark,
Following is a concrete example of a module that does interrupt-based input from a UART. It would be accompanied by a header file with declarations for kbhit() and getch(), and all external access would be done through those functions. -- Russ
/* ////////////////////////////////////////////////////////////////////////////
Names for Numbers
//////////////////////////////////////////////////////////////////////////// */
#define RX_BUFF_SIZE 10 /* Must be less than 256 */
/* ////////////////////////////////////////////////////////////////////////////
Module Variables
//////////////////////////////////////////////////////////////////////////// */
char rx_buff[RX_BUFF_SIZE]; /* Circular UART receive buffer */
unsigned char rx_in; /* Next emtpy spot in rx_buff[] */
unsigned char rx_out; /* Oldest byte in rx_buff[] */
/* ////////////////////////////////////////////////////////////////////////////
serial_handler()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: This is the interrupt handler for the on-board serial port.
REVISIONS: 19 APR 98 - RAC - Lifted from the DART II Code
//////////////////////////////////////////////////////////////////////////// */
interrupt [0x23] void serial_handler() {
if (RI0) { /* It's a receive interrupt */
RI0 = 0; /* Clear the interrupt */
rx_buff[rx_in++] = SBUF0; /* Put new byte in buffer */
if (rx_in == RX_BUFF_SIZE) { /* Just reached buffer end */
rx_in = 0; /* Wrap to the beginning */
} /* End 'reached buffer end' */
} /* End 'receive interrupt' */
} /* End serial_handler() */
/* ////////////////////////////////////////////////////////////////////////////
kbhit()
getch()
///////////////////////////////////////////////////////////////////////////////
DESCRIPTION: kbhit() indicates whether or not unread data has accumulated in
the serial input buffer. getch() waits for such data and
returns the oldest character in the buffer.
REVISIONS: 19 APR 98 - RAC - Adapted from the DART II code.
//////////////////////////////////////////////////////////////////////////// */
char kbhit() { /* The buffer contains data */
return (rx_in != rx_out); /* if the in and out */
} /* indexes are different */
/* ///////////////////////////////////////////////////////////////////////// */
char getch() {
char rv; /* Put return character here */
while (!kbhit()) ; /* Wait for a character */
rv = rx_buff[rx_out++]; /* Grab byte from rx buffer */
if (rx_out == RX_BUFF_SIZE) { /* Just reached buffer end */
rx_out = 0; /* Wrap to the beginning */
} /* End 'reached buffer end' */
return rv; /* The answer */
} /* End getch() */
|



