home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / SIMTEL / HITECH-C / Z8051H83.EXE / FEATH83.C < prev    next >
C/C++ Source or Header  |  1993-05-21  |  4KB  |  139 lines

  1. #include    <intrpt.h>
  2. #include    <h83.h>
  3.  
  4. /*
  5.  *    This source file demonstrates some of the special features
  6.  *    of the HI-TECH H8/300 C Cross Compiler.  This file should
  7.  *    be compiled with H8/300 code generation selected.
  8.  *    
  9.  *    To generate the best possible code from this source file, select
  10.  *    H8/300 code generation from the "Options" menu and "Full Optimization"
  11.  *    from the "Optimization" menu.  This will demonstrate the quality of
  12.  *    code which can be obtained using HI-TECH C, for a fraction of the
  13.  *    effort required to write equivalent assember code.
  14.  *
  15.  *    HI-TECH C has a number of features designed to make programming
  16.  *    embedded processors simpler and more efficient, these include:
  17.  *
  18.  *    interrupt functions: interrupt handlers may be written entirely
  19.  *                 in C (no messy assembler code required).
  20.  *
  21.  *    absolute variables:  variables of any type (even complex
  22.  *                 structures and arrays) can be placed at
  23.  *                 any absolute address in memory - great
  24.  *                 for handling memory mapped devices.
  25.  *
  26.  *    in-line assembler:   assembler code can be place in line.  This
  27.  *                 is useful for accessing special instructions.
  28.  *
  29.  *    register arguments:  some function arguments are passed in the
  30.  *                 the R1, R2 and R3 registers.  Register based
  31.  *                 argument passing is much faster and more
  32.  *                 compact than stack based passing.
  33.  */
  34.  
  35. #ifndef    h8300
  36. #error    This program should be compiled to H8/300 code
  37. #endif
  38.  
  39. volatile unsigned char        PIA_STATUS @ 0x2080;    /* mem mapped PIA */
  40. volatile unsigned char        PIA_BUFFER @ 0x2081;    /* at 2080 - 2081 */
  41.  
  42. unsigned char    rxbuf[256];            /* 256 byte circular buffer */
  43. unsigned char    ri_ptr, ro_ptr;            /* head, tail pointers */
  44.  
  45. /* serial i/o for H8/330 */
  46.  
  47. #define    BAUD_RATE    19200            /* Serial baud rate */
  48. #define    OSC        16000000L        /* Xtal frequency */
  49. #define    DIVIDER        (OSC/(64*BAUD_RATE)-1)
  50.  
  51. /*
  52.  *    rx_intr handles receive interrupts from serial port
  53.  */
  54.  
  55. interrupt void
  56. rx_intr(void)
  57. {
  58.     if (SSR & (ORER|PER|FER)) {    /* serial port error ? */
  59.         SSR &= ~(ORER|FER|PER);
  60.     } else {
  61.         rxbuf[ri_ptr++] = RDR;
  62.         SSR &= ~RDRF;
  63.         if (ro_ptr == ri_ptr)        /* buffer overflow ? */
  64.             --ri_ptr;        /* discard last byte received */
  65.     }
  66. }
  67.  
  68. /*
  69.  *    sio_char_avail: returns 1 if a char is available, 0 otherwise
  70.  */
  71.  
  72. unsigned char
  73. sio_char_avail(void)
  74. {
  75.     return (ri_ptr != ro_ptr);
  76. }
  77.  
  78. /*
  79.  *    sio_get_char: returns one char from the tail of the circular
  80.  *                  buffer.  Assumes that a char is available.
  81.  */
  82.  
  83. unsigned char
  84. sio_get_char(void)
  85. {
  86.     unsigned char    ch;
  87.  
  88.     di();
  89.     ch = rxbuf[ro_ptr++];
  90.     ei();
  91.     return ch;
  92. }
  93.  
  94. /*
  95.  *    Transmit a character via the memory mapped PIA
  96.  *    bit 0 of STATUS register indicates it is ready to accept data
  97.  *
  98.  *    Note: register based argument passing is used with this
  99.  *    function.  "ch" will be passed in the R1 register.  If FULL
  100.  *    optimization is performed, "ch" probably won't ever be stored
  101.  *    to a stack location.
  102.  */
  103.  
  104. void
  105. pia_send(unsigned char ch)
  106. {
  107.     while (!(PIA_STATUS & 1))
  108.         continue;
  109.     PIA_BUFFER = ch;
  110. }
  111.  
  112. /*
  113.  *    Initialize the SIO and PIA
  114.  */
  115.  
  116. void
  117. device_inits(void)
  118. {
  119.     di();            /* interrupts of while programming devices */
  120.     SCR = TE|RE;        /* enable transmitter & receiver */
  121.     BRR = DIVIDER;        /* set baud rate */
  122.     SSR &= ~(ORER|FER|PER);
  123.     SSR |= RIE;        /* receive interrupts ON */
  124.     set_vector((isr *) RXI, rx_intr);
  125.     PIA_STATUS = 0x80;    /* reset PIA */
  126.     PIA_STATUS = 0x40;    /* mode 2: bidirectional, interrupts off */
  127.     ei();
  128. }
  129.  
  130. main()
  131. {
  132.     device_inits();
  133.     for (;;) {
  134.         if (sio_char_avail())
  135.             pia_send(sio_get_char());
  136.         /* other processing */
  137.     }
  138. }
  139.