1. You can now buy finished microcontroller project from us, Check out the Store for the complete list of projects.
  2. Need a custom project, Send us some details about your project. So that we can quote the price for it.

A rewrite of the serial example to use interrupts in C

A rewrite of the serial example to use interrupts in C for 8051 microcontroller.

  1. Binu
    Code:
    Code (Text):
    1. /*************************************************************************
    2. * int.c - A demonstration of how to write interrupt-driven code for an
    3. *  8051 using the Keil C compiler.  The same techniques may work in other
    4. *  8051 C compilers with little or no modifcation.  This program will
    5. *  combine the functionality of both basic.c and serial.c to allow serial
    6. *  communications to be entirely in the background, driven by the serial
    7. *  interrupt.  This allows the main() function to count on Port 0 without
    8. *  being aware of any ongoing serial communication.
    9. */
    10.  
    11. /* included headers */
    12.  
    13. #include
    14.  
    15. /* function declarations */
    16.  
    17. char getCharacter (void);        /* read a character from the serial port */
    18. void sendCharacter (char);        /* write a character to the serial port */
    19.  
    20. /*
    21. * Interrupt handlers:
    22. *  Here the code for the interrupt handler will be placed.  In this
    23. *  example, a handler for the serial interrupt will be written.
    24. *  Examination of the 8051 specs will show that the serial interrupt is
    25. *  interrupt 4.  A single interrupt is generated for both transmit and
    26. *  receive interrupts, so determination of the exact cause (and proper
    27. *  response) must be made within the handler itself.
    28. *  To write an interrupt handler in Keil, the function must be declared
    29. *  void, with no parameters.  In addition, the function specification
    30. *  must be followed by a specification of the interrupt source it is
    31. *  attached to.  The "using" attribute specifies which register bank
    32. *  to use for the interrupt handler.
    33. */
    34.  
    35. void serial_int (void) interrupt 4
    36. {
    37.       static char        chr = '\0';        /* character buffer */
    38.  
    39.       /*
    40.         * The interrupt was generated, but it is still unknown why.  First,
    41.         * check the RI flag to see if it was because a new character was
    42.         * received.
    43.         */
    44.  
    45.       if (RI == 1)        /* it was a receive interrupt */
    46.       {
    47.               chr = SBUF;        /* read the character into our local buffer */
    48.               RI = 0;        /* clear the received interrupt flag */
    49.               TI = 1;        /* signal that there's a new character to send */
    50.       }
    51.       else if (TI == 1)        /* otherwise, assume it was a transmit interrupt */
    52.       {
    53.               TI = 0;        /* clear the transmit interrupt flag */
    54.               if (chr != '\0')        /* if there's something in the local buffer... */
    55.               {
    56.                       if (chr == '\r') chr = '\n';        /* convert  to  */
    57.                       SBUF = chr;        /* put the character into the transmit buffer */
    58.                       chr = '\0';
    59.               }
    60.       }
    61. }
    62.  
    63. /* functions */
    64.  
    65. /*************************************************************************
    66. * main - Program entry point.  This program sets up the timers and
    67. *  interrupts, then simply receives characters from the serial port and
    68. *  sends them back.  Notice that nowhere in the main function is Port 0
    69. *  incremented, nor does it call any other function that may do so.
    70. *  main() is free to do solely serial communications.  Port 0 is handled
    71. *  entirely by the interrupt handler (aside from initialization).
    72. *
    73. * INPUT: N/A
    74. * RETURNS: N/A
    75. */
    76.  
    77. main()
    78. {
    79.       /* Before the serial port may be used, it must be configured. */
    80.  
    81.       /* Set up Timer 0 for the serial port */
    82.  
    83.       SCON = 0x50;        /* mode 1, 8-bit uart, enable receiver */
    84.       TMOD = 0x20;        /* timer 1, mode 2, 8-bit reload */
    85.       TH1  = 0xFE;        /* reload value for 2400 baud */
    86.       ET0  = 0;                /* we don't want this timer to make interrupts */
    87.       TR1  = 1;                /* start the timer */
    88.       TI  = 1;                /* clear the buffer */
    89.  
    90.       /*
    91.         * The compiler automatically installs the interrupt handler, so
    92.         * all that needs to be done to use it is enable interrupts.  First,
    93.         * speficially enable the serial interrupt, then enable interrupts.
    94.         */
    95.      
    96.       ES  = 1;                /* allow serial interrupts */
    97.       EA  = 1;                /* enable interrupts */
    98.  
    99.       /* initialize Port 0 to 0, as in basic.c */
    100.  
    101.     P0 = 0;
    102.  
    103.       /*
    104.         * Loop forever, increasing Port 0.  Again, note nothing is done
    105.         * with the serial port in this loop.  Yet simulations will show
    106.         * that the software is perfectly capable of maintaining serial
    107.         * communications while this counting proceeds.
    108.         */
    109.  
    110.       while (1==1)
    111.     {
    112.               unsigned int        i;
    113.               for (i = 0; i < 60000; i++) {;}        /* delay */
    114.               P0 = P0 + 1;        /* increment Port 0 */
    115.       }
    116. }
Loading...