help with buad rate caclulation and setting


+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    808

    Default help with buad rate caclulation and setting

    Hi guys

    changed chip internal osc to an external tcxo at 16.368mhz with 4ppl to give 65.472mhz active for a 18f47k40 and the terminal port 2 is not as happy , all other bits on chip working fine

    done the same tcxo change for k22 and it not an issue for the terminal at same speed as 19.2k baud rate ,and using just the base setup for the terminal
    but the 47k40 is not output all the right chrs when connected to terminal at 19.2k with errors showing on a number of chrs

    tried calc1 and calc2 code , but with the same result
    also tried setting 16bit/ async SP2BRGH=0 which is the same as calc2 - but that results in total unreadable terminal display at 19.2k

    so some help be good to solve this

    cheers

    sheldon



    normally just the basic setting are needed

    Code:
        DEFINE HSER_PORT 2          ' Hser port 2 use 
        DEFINE HSER2_RXREG PORTB    ' USART 2 RX - Set PPS Register port B
        DEFINE HSER2_RXBIT 7        ' 
        DEFINE HSER2_TXREG PORTB
        DEFINE HSER2_TXBIT 6
        DEFINE HSER2_RCSTA 90h      ' Hser2 receive status init  - async 
        DEFINE HSER2_TXSTA 24h      ' Hser2 transmit status init - async 
        DEFINE HSER2_BAUD 19200     ' Hser2 baud rate
    Have tried a 2 baud rate calculations changes to allow for the osc change but result is the same
    so must be missing something , but not sure what

    calc1 ....
    use 8bit brgh= 0 async = baud rate = fosc / (64 x SPxBRG) +1
    osc 16.368mhz x 4 ppl = 65,472,000 hz
    buad rate req 19.2k

    solve SP2BRG
    SP2BRG = 65472000 /( 64 X 19200) = 53.28125
    SP2BRG = SP2BRG -1 = 52.28125
    SP2BRG = 52

    BAUD RATE = 65472000 / 64 X (52+1) = 19301.8867

    ERROR RATE = 19301.8867 - 19200 / 19200 = 0.0053 %

    CODE RESULT

    Code:
        DEFINE HSER_PORT 2          ' Hser port 2 use 
        DEFINE HSER2_RXREG PORTB    ' USART 2 RX - Set PPS Register port B
        DEFINE HSER2_RXBIT 7        ' 
        DEFINE HSER2_TXREG PORTB
        DEFINE HSER2_TXBIT 6
        DEFINE HSER2_RCSTA 90h      ' Hser2 receive status init  - async 
        DEFINE HSER2_TXSTA 24h      ' Hser2 transmit status init - async 
        dEFINE HSER2_CLROERR 1      ' Clear overflow automatically
        DEFINE HSER2_BAUD 19200     ' Hser2 baud rate
        DEFINE HSER2_SPBRGH 0       ' set F/osc/[64(n+1] formular for baud rate gen
        DEFINE HSER2_SPBRGL 52      ' 19200 Baud @ SPBRGL = 52 0.0053% error
        BAUDCON2.3 = 0                 ' set BRG16 = 0  THEN  8BIT/ASYNC

    calc2 ....
    use 8bit brgh= 1 async = baud rate = fosc / (16 x SPxBRG) +1
    osc 16.368mhz x 4 ppl = 65,472,000 hz
    buad rate req 19.2k

    solve SP2BRG
    SP2BRG = 65472000 /( 16 X 19200) = 213.125
    SP2BRG = SP2BRG -1 = 212.125
    SP2BRG = 212

    BAUD RATE = 65472000 / 16 X (212+1) = 19211.267

    ERROR RATE = 19211.267 - 19200 / 19200 = 0.00058 %

    calc2 give a value closer to the 19.2k than calc 1 - so should be the better ?

    Resulting code

    Code:
        DEFINE HSER_PORT 2          ' Hser port 2 use 
        DEFINE HSER2_RXREG PORTB    ' USART 2 RX - Set PPS Register port B
        DEFINE HSER2_RXBIT 7        ' 
        DEFINE HSER2_TXREG PORTB
        DEFINE HSER2_TXBIT 6
        DEFINE HSER2_RCSTA 90h      ' Hser2 receive status init  - async 
        DEFINE HSER2_TXSTA 24h      ' Hser2 transmit status init - async 
        dEFINE HSER2_CLROERR 1       ' Clear overflow automatically
        DEFINE HSER2_BAUD 19200     ' Hser2 baud rate
        DEFINE HSER2_SPBRGH 1        ' set F/osc/[16(n+1] formular for baud rate gen
        DEFINE HSER2_SPBRGL 212     ' 19200 Baud @ SPBRGL = 212   0.0058% error
        BAUDCON2.3 = 0                   ' set BRG16 = 0 - 8BIT/ASYNC

  2. #2
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    808

    Default Re: help with buad rate caclulation and setting

    changed incorrrect command
    baudcon2.3 =0 to baud2con.3 =0 but no change in result

  3. #3
    Join Date
    Feb 2012
    Location
    PERTH AUSTRALIA
    Posts
    808

    Default Re: help with buad rate caclulation and setting

    FIXED and now i know why i stuffed up

    for those that do not know how to caclulate the baud rates for osc speeds not common
    at least is shows how its done for the k40 series

    reason for the error is that in the data sheet the parameter BRGH and SPxBRGH are not the same and i was setting it incorrectly when changing for calculations

    all baud rate calcs refer to setting BRGH found in TXxSTA bit3

    also so by setting DEFINE HSER2_SPBRGH 0 and DEFINE HSER2_SPBRGL 52 - did not work , dEFINE HSER2_SPBRG 52 works

    by using calc2 gives the lowest error rate for 19.2k baud
    so requires 8bit/async BRGH = 1 , BRG16 =0

    Code:
      DEFINE HSER_PORT 2          ' Hser port 2 use 
        DEFINE HSER2_RXREG PORTB    ' USART 2 RX - Set PPS Register port B
        DEFINE HSER2_RXBIT 7        ' 
        DEFINE HSER2_TXREG PORTB
        DEFINE HSER2_TXBIT 6
        DEFINE HSER2_RCSTA 90h      ' Hser2 receive status init  - async 
        DEFINE HSER2_TXSTA 24h      ' Hser2 transmit status init - async BRGH = 1 ( BIT3)
        dEFINE HSER2_CLROERR 1      ' Clear overflow automatically
        DEFINE HSER2_BAUD 19200     ' Hser2 baud rate
        DEFINE HSER2_SPBRG 212      ' 19200 Baud @ SPBRG = 212 0.0016% error
        BAUD2CON.3 = 0                   ' set BRG16 = 0 - 8BIT/ASYNC

Similar Threads

  1. HSEROUT at Quarter Rate!
    By Gitchie in forum Serial
    Replies: 6
    Last Post: - 11th April 2012, 20:48
  2. Rf module baud rate
    By tazntex in forum Serial
    Replies: 4
    Last Post: - 5th August 2008, 18:47
  3. Replies: 1
    Last Post: - 27th March 2007, 08:28
  4. Baud rate calculation
    By Demon in forum Serial
    Replies: 12
    Last Post: - 21st July 2006, 22:02
  5. SHIFTOUT Clock rate
    By Brock in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 8th July 2006, 23:42

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts