Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
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() */

 




List of 56 messages in thread
TopicAuthorDate
Variables within ISR            01/01/70 00:00      
   static            01/01/70 00:00      
      Static, link            01/01/70 00:00      
   beware            01/01/70 00:00      
      Modular Programming            01/01/70 00:00      
         a rebuttal            01/01/70 00:00      
            A Faq about...            01/01/70 00:00      
            Global variables            01/01/70 00:00      
               "Self Documenting"            01/01/70 00:00      
            Global Variables Compromise Modularity            01/01/70 00:00      
               Module vs Global scope            01/01/70 00:00      
               file static            01/01/70 00:00      
                  RE: file static            01/01/70 00:00      
                     extern does matter            01/01/70 00:00      
                  Undeclared Variables            01/01/70 00:00      
                     to Russ            01/01/70 00:00      
                     try this            01/01/70 00:00      
                        Re: try this            01/01/70 00:00      
               Not Evil            01/01/70 00:00      
               module variables            01/01/70 00:00      
                  yes and no all must be weighed            01/01/70 00:00      
         Hmm            01/01/70 00:00      
            the advantages of not suffering from interruptifob            01/01/70 00:00      
            That is the typical way            01/01/70 00:00      
               yes, absolutely            01/01/70 00:00      
                  Depends on the use            01/01/70 00:00      
                     every rule can (and should) be broken            01/01/70 00:00      
            Fixing Problems vs. Hiding Them            01/01/70 00:00      
               I agree, but many take this as an excuse            01/01/70 00:00      
                  Be Defensive            01/01/70 00:00      
   A Module Example            01/01/70 00:00      
      if we are to give examples            01/01/70 00:00      
         Poor example            01/01/70 00:00      
            not so poor            01/01/70 00:00      
         Reasons to Waste Machine Cycles            01/01/70 00:00      
            it is still 'hidden' and BTW why hide it            01/01/70 00:00      
               I'm Done Preaching            01/01/70 00:00      
                  I hope that as well            01/01/70 00:00      
         to asm programmers..            01/01/70 00:00      
            Poor Logic            01/01/70 00:00      
               what an utterly stupid teacher            01/01/70 00:00      
                  OO            01/01/70 00:00      
                     No            01/01/70 00:00      
                     What OOP tells you            01/01/70 00:00      
                        A long Time ago            01/01/70 00:00      
                  I disagree            01/01/70 00:00      
                     some of the worst 'spaghetti' I have seen            01/01/70 00:00      
               Rules in C            01/01/70 00:00      
                  rules imposed on C            01/01/70 00:00      
                     Language Problems            01/01/70 00:00      
            that is very relevant            01/01/70 00:00      
         what about the library?            01/01/70 00:00      
            OH NO            01/01/70 00:00      
               Experience is the best teacher            01/01/70 00:00      
                  overlay            01/01/70 00:00      
                  to Russell            01/01/70 00:00      

Back to Subject List