Serial Port Complete - Latest Microcontroller projects

(lily) #1

Chapter 11


&    5    


5
  &5  &50
The baud rate generator registers (SPBRG and optionally SPBRGH) contain a
value used in setting the bit rate. If the BAUDCON register’s BRG16 = 0, the
value is 8 bits and SPBRG contains the entire value. If BRG16 = 1, the value is
16 bits, SPBRG contains the low byte, and SPBRGH contains the high byte.

           & 5
To set the register values for a desired bit rate, do the following:


  1. Obtain the value of FOSC. Begin by determining the clock source from the
    OSCCON register (SCS1..SCS0). If using the primary clock, determine FOSC
    from the frequency of the clock source and the value of FOSC3..FOSC0 in the
    CONFIG1H register. If using the internal oscillator block, determine FOSC
    from the settings in the OSCCON and OSCTUNE registers. If using the
    Timer1 oscillator, FOSC is the frequency of the timing source connected to
    T1OSI and T1OSO.

  2. Determine the values to store in the registers. Two ways to do so are by con-
    sulting the tables provided in the data sheet and by calculating the values.
    For many applications, you can find a match in the tables in the chip’s data
    sheet. For common FOSC values, the tables show four options depending on
    the values of BRGH in the TXSTA register and BRG16 in the BAUDCON
    register. Select the value with the smallest error. For example, if FOSC = 20
    MHz and you want a bit rate of 115,200 bps, the smallest error (0.94%) is with
    BRGH = 1, BRG16 = 1, SPBRG = 42 and SPBGRH = 0.
    If your bit rate or FOSC value isn’t in the tables, you’ll need to calculate the best
    value. Computer assistance is useful. See http://www.Lvr.com for a link to an applica-
    tion that selects values for a desired bit rate and oscillator frequency
    To find a value to store in SPBRG (and SPBRGH if used) for a desired bit rate
    and FOSC, BRGH, and BRG16 settings, use this formula:
    spbrg = Math.Round((fosc / (multiplier * bitRate)) - 1)
    spbrg is the 8-bit value in SPBRG or the 16-bit value in SPBRGH and SPBRG.
    If BRG16 = 0, SPBRG must be 255 or less. If the calculated spbrg value is
    slightly higher than 255, setting SPBRG to 255 might result in a usable bit rate.
    fosc is the FOSC value in Hz.

Free download pdf