Electronic Circuits and Projects Forum



Cannot get 57600 b/s on 18f452

12 Last »
  1. #1
    BOBKA BOBKA is offline

    Cannot get 57600 b/s on 18f452

    Hi, I am trying to get as much as possible from my serial connection. I am using 10Mhz xtal in HS+PLL mode (total of 40MHz).

    at the top of my program I define

    #define BAUD d'57600'
    #define XTAL d'10'
    #define X ((XTAL*d'1000000')/(d'64'*BAUD))-1

    and then in the program I clear the BRGH bit (making the speed slow - and what is the limit here?).

    Instead of the desired chars, I am getting some garbage. If I reduce the speed to 38400 b/s (or lower), everything works just fine.

    I also tried
    #define X ((XTAL*d'1000000')/(d'16'*BAUD))-1
    and then set BRGH bit high, so I started to get some new garbage. I also tried to change the XTAL to 40, but no success.

    Two questions:
    1. what is the bitrates for BRGH=1 and BRGH=0?
    2. is this a limitation of a pic/max/pc?

    PS: I am using COM2 on pc which is set to 115200 in devices, and then set to appropriate BAUD in terra term. Also, fifo buffers are off.


    Thank You.

  2. #2
    cadstarsucks cadstarsucks is offline
    Quote Originally Posted by BOBKA
    Hi, I am trying to get as much as possible from my serial connection. I am using 10Mhz xtal in HS+PLL mode (total of 40MHz).

    at the top of my program I define

    #define BAUD d'57600'
    #define XTAL d'10'
    #define X ((XTAL*d'1000000')/(d'64'*BAUD))-1

    and then in the program I clear the BRGH bit (making the speed slow - and what is the limit here?).

    Instead of the desired chars, I am getting some garbage. If I reduce the speed to 38400 b/s (or lower), everything works just fine.

    I also tried
    #define X ((XTAL*d'1000000')/(d'16'*BAUD))-1
    and then set BRGH bit high, so I started to get some new garbage. I also tried to change the XTAL to 40, but no success.

    Two questions:
    1. what is the bitrates for BRGH=1 and BRGH=0?
    2. is this a limitation of a pic/max/pc?

    PS: I am using COM2 on pc which is set to 115200 in devices, and then set to appropriate BAUD in terra term. Also, fifo buffers are off.


    Thank You.
    Try the 10mhz high speed and x=10 or 40mhz, low speed, and x=10... i am surprised no one responded ... one has to wonder...

    Dan
    0

  3. #3
    Papabravo Papabravo is offline
    Assuming integer division, and the parentheses the value of X is 1. I'm no expert but I think the error is kinda large. Using the value 1 the calculated baudrate is 78125. Sure enough this error is just too large. If you use BRGH = 1 to use a 16x baudrate clock then I compute a value of 9. This gives an actual baudrate of 62500. This is better but not quite what you want. If you need 57600 then I suggest that you consider changing crystals so that you get what you want. I think that 9.216 MHz. would work, and there may be other values
    0
    Last edited by Papabravo; 23rd February 2007 at 07:03 PM.

  4. #4
    cadstarsucks cadstarsucks is offline
    Quote Originally Posted by Papabravo
    Assuming integer division, and the parentheses the value of X is 1. I'm no expert but I think the error is kinda large. Using the value 1 the calculated baudrate is 78125. Sure enough this error is just too large. If you use BRGH = 1 to use a 16x baudrate clock then I compute a value of 9. This gives an actual baudrate of 62500. This is better but not quite what you want. If you need 57600 then I suggest that you consider changing crystals so that you get what you want. I think that 9.216 MHz. would work, and there may be other values
    According to the data sheet:
    --------------------------------
    Solving for X:
    X = ( (FOSC / Desired Baud Rate) / 64 ) – 1
    X = ((16000000 / 9600) / 64) – 1
    X = [25.042] = 25
    --------------------------------
    10000000/57600/16-1 (for BGRH=0=high speed, 64 is for low speed) = 10
    Going the other way we get 56818 for an error of 1.3%
    The moral of the story is: counters do not use integer division.

    Dan
    0

  5. #5
    Nigel Goodwin Nigel Goodwin is offline
    Super Moderator
    Nicely worked out cadstarsucks, but you misssed the fact that he's using the 18F in HS+PLL mode, which means it runs at 40Mhz instead of 10MHz. But what you probably didn't know is that the PICList has an online generator for these values at http://www.piclist.com/techref/microchip/spbrgcalc.asp this gives either a +1.4% (43 and BRGH=1, or 10 and BRGH=0) or -0.9% (42 and BRGH=1) error using 40MHz.
    0
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  6. #6
    Mike, K8LH Mike, K8LH is offline
    My SPBRG Calc application shows similar results with better tolerance for 57600 baud using BRGH=1.

    The SPBRG Calc spreadsheet (post #1) and single file application (post #5) can be found on this Forum.Microchip thread; Revised SPBRG Calc



    The Spreadsheet can provide SPBRG solutions for non-standard baud rates (by editing the BAUD rate cells in the left most column).

    Attached Images
    0
    Last edited by Mike, K8LH; 23rd February 2007 at 08:34 PM.

  7. #7
    cadstarsucks cadstarsucks is offline
    Quote Originally Posted by Nigel Goodwin
    Nicely worked out cadstarsucks, but you misssed the fact that he's using the 18F in HS+PLL mode, which means it runs at 40Mhz instead of 10MHz. But what you probably didn't know is that the PICList has an online generator for these values at http://www.piclist.com/techref/microchip/spbrgcalc.asp this gives either a +1.4% (43 and BRGH=1, or 10 and BRGH=0) or -0.9% (42 and BRGH=1) error using 40MHz.
    Actually he said he had tried both. The switch to 40MHz only entails using the low speed option instead of the high speed one. I did look it up. You see, I do this sort of thing all the time at work as the software guys tend to miss the minutia.

    The spreadsheets can often have errors if they are included at all. My boss has learned that it is better to trust my position than most any other in the school of hard knocks.

    For me to find and answer that question was all of 10 minutes because it is what I do every day for a living. That and find the most effective solutions for a whole myriad of design problems.

    Dan
    0
    Last edited by cadstarsucks; 24th February 2007 at 04:04 PM.

  8. Thread Starter #8
    BOBKA BOBKA is offline
    Thank you guys for you response. I have played with it a little bit more. It appears that the XTAL should be the actual clock at the clock pin and not the one after the PLL. I figured out that the clock division was the limiting factor, so I will be stack with it for a while.

    I finally figured out how the BRGH works, to summarized:

    There are two models for calculating SPBRG, one is based on low speed transmission (BRGH=0) and the other is based on high speed transmission (BRGH=1). Those high/low speed transmission settings can be derived from looking at the results of those two models.
    Low speed transmission: SPBRG = (XTAL*d'1000000')/(d'64'*BAUD_RATE) - 1
    High speed transmission: SPBRG = (XTAL*d'1000000')/(d'16'*BAUD_RATE) - 1
    * the SPBRG should be rounded to the nearest integer
    *BAUD_RATE is the desired baud rate (19200, 57600, etc)

    Since the above SPBRG equations round the results, we want to calculate the actual transmission rate, and the percentage error due to this rounding.
    low speed: ACTUAL_BAUD = XTAL*d'1000000' / (d'64'*(SPBRG+1))
    high speed: ACTUAL_BAUD = XTAL*d'1000000' / (d'16'*(SPBRG+1))

    The percentage error is calculated as (same eqn for both models):
    % error = ((ACTUAL_BAUD-BAUD_RATE)/BAUD_RATE)*100%



    Example: we want to get transmission rate of 19200 bits/s w/ crystal of 10M
    => XTAL=10, BAUD_RATE=19200

    low speed: SPBRG = (10*1000000) / (64*19200) - 1 = 8.138 - 1 => 7
    ACTUAL_BAUD = 10*1000000 / (64*(7+1)) = 19531.25
    % error = ((19531.25-19200)/19200) * 100% = 1.73%

    hish speed: SPBRG = (10*1000000) / (16*19200) -1 = 32.55 - 1 => 31
    ACTUAL_BAUD = 10*1000000 / (16*(31+1)) = 19531.25
    %error = 1.73% as for low speed, but if we were to round the SPBRG, we would get SPBRG= 32.55-1 = 32,
    then ACTUAL_BAUD = 10*1000000 / (16*(32+1)) = 18939.4
    and %error = ((18939.4-19200)/19200) * 100% = -1.14%

    Results: using high speed model is always better cause it provides smaller % error. The only situation where it cannot be used, is when SPBRG for it is greater than 255. This will typically occur at high clock frequencies and very slow transmission rates. In this cases low speed transmission model should be used.

    Hope this will help someone
    0

  9. #9
    eblc1388 eblc1388 is offline
    Quote Originally Posted by BOBKA
    It appears that the XTAL should be the actual clock at the clock pin and not the one after the PLL.
    Are you 100% sure about this?

    The baudrate table shown in Microchip datasheet has an entry for 40MHz, which could be the result of using 10MHz crystal + 4x PLL. It also has an entry for 10MHz too. The SPBRG values are different.

    How do the above program knows whether PLL is used or not?
    0
    Last edited by eblc1388; 25th February 2007 at 11:21 AM.
    L.Chung

  10. #10
    Nigel Goodwin Nigel Goodwin is offline
    Super Moderator
    Quote Originally Posted by eblc1388
    Are you 100% sure about this?
    Sounds wrong to me.


    The baudrate table shown in Microchip datasheet has an entry for 40MHz, which could be the result of using 10MHz crystal + 4x PLL. It also has an entry for 10MHz too. The SPBRG values are different.

    How do the above program knows whether PLL is used or not?
    I suspect he's possibly not got the clock multiplier switched on?, so is only running at 10MHz.
    0
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

12 Last »
Tags
Similar Threads
Thread Starter Forum Replies Last Post
Help with data input for 18f452 cromb Microcontrollers 3 11th January 2007, 06:38 AM
18f452 reset pin problem esconele Microcontrollers 1 1st November 2006, 03:52 AM
18f452 blinks all led of port??? dowhua Electronic Projects Design/Ideas/Reviews 0 16th February 2004, 02:01 PM
interrupts with timer2 pic 18f452 french_student Microcontrollers 11 1st February 2004, 10:24 PM
PIC 18F452 Will cause my death....ack brentonw2004 Microcontrollers 5 10th January 2004, 07:54 PM
Electronic Circuits  |  Learning Electronics

Join our community with over 100,000 Members! It's free, easy and when you're logged in you have many more features! Click to register.
Page Time: 0.10227 seconds      Memory: 7,723 KB      Queries: 17      Templates: 0