Calculation Baudrate BSCALE and BSEL values for UART

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

the datasheets of the XMEGA128A1 are mentioning "Fper" (pheriperal clock). I don't find a clear explanation for that and I need the value of Fper to calculate my settings for BSCALE, BSEL.

So: What is Fper and how much is this? I work with a 32Mhz internal CLK for the microcontroller.

Can I program Fper somewhere by setting registers?

I don't understand this! :oops:

thanks for the help!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fper is the peripheral clock which is the same as your system clock for UART.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So, in this(my) case this should be the same as f-oscillator, cause in the blockdiagram I see it is connected directly to the internal baudrategenerator of the microcontroller. I will try to calculate and see what baudrate I get in real.

thank you for your answer.

Fabien

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It is certainly possible to calc the baud rate divisors on the fly, or use a look up table.

When I was first starting with the Xmega the above wasn't worth the trouble, and a straight coded routine let me move on to other parts of the program.

You can check your calculations against these, which are in decimal.

Usartbrsetupc0:
   'USART Baud Rate Setup
   'For XMega's Clock = 32 MHz.
   'Mode is N, 8, 1  No Parity, 8 Data Bits, 1 Stop Bit
   'Uses Bscale = 0
   'Supports:         (Could add others)
   '1200, 9600, 38400, 57600, 115200
   'On Entry have: XbrC0, the desired Baud Rate.  e.g. XbrC0 = 57600
   'This sets the BaudCtrlA and BaudCtrlB Registers with the correct
   'divisor(s).
   'Set up USART Baud CtrlA, Baud Rate Control Register A
   '8 Least Sig bits of 12 bit divisor
   'Usartc1_baudctrla = 207    0000 1100 1111 as 12 bit
   'Set up USART Baud CtrlB, Baud Rate Control Register B
   '4 Most Sig bits of 12 bit divisor, and Bscale (0 for now).
   'Usartc1_baudctrlb = 0      0000 0000

   If Xbrc0 = 1200 Then
      Usartc0_baudctrla = 130
      Usartc0_baudctrlb = 6
   Elseif Xbrc0 = 9600 Then
      Usartc0_baudctrla = 207
      Usartc0_baudctrlb = 0
   Elseif Xbrc0 = 38400 Then
      Usartc0_baudctrla = 51
      Usartc0_baudctrlb = 0
   Elseif Xbrc0 = 57600 Then
      Usartc0_baudctrla = 34
      Usartc0_baudctrlb = 0
   Elseif Xbrc0 = 115200 Then
      Usartc0_baudctrla = 16
      Usartc0_baudctrlb = 0
   Else
      'Error, unsupported Baudrate, set it for 9600
      Usartc0_baudctrla = 207
      Usartc0_baudctrlb = 0
   End If
   Return

JC

Edit: Typos.

Last Edited: Thu. Jun 9, 2011 - 04:39 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi!

thank you very much JC, this is very helpfull! I also came out on 207 for baudCTRLA register with 32Mhz internal cristal.

Sometimes something works(i had good working serial communication) but you don't know why and won't accept it. At least with this additional info I now also understand how it works!

Again:thanks!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I wrote up some code for both compile-time and run-time generation of the best possible BSCALE-based values for a given baud rate and F_CPU:

http://blog.omegacs.net/2010/08/18/xmega-fractional-baud-rate-source-code/

I would note however that I think you're going to be better off using the 2MHz internal oscillator and running the PLL at x16 than using the 32MHz oscillator. I've seen it be far more wild than the 2MHz, so if you're trying to lock onto a particular baud rate, you'll want the better accuracy.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
I would note however that I think you're going to be better off using the 2MHz internal oscillator and running the PLL at x16 than using the 32MHz oscillator.

Omegahacker, please tell a bit more specific about it.
I'm using in my project 32MHz internal source divided by 2 to result in 16MHz system clock. Does it mean that it would be better to use internal 2MHz source multiplied by 8 to result in the same 16MHz? What is a difference, is there some measured values to prove the 32M oscillator's higher "wildness" vs 2M's, or it's just your personal intuition?
I also use high-speed UARTS intensively in my prj, and there is very desirable to have the most precise timings possible for them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Look at the Data Sheet for your individual chip. The Xmega64A1, for example, under Section 33.13 Oscillator Characteristics, states that both the 2 MHz and the 32 MHz oscillators are +/- 1.5 % @ 3 V.

You may want to run your baud rate and clock frequency through AVRCALC to check the baud rate error. You want it under 2%. Preferable well under 2%.

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Datasheet? That one that says that I have 12-bit resolution ADC? ;-)

I use xMega128a1. Datasheet says that both 2M and 32M oscillators have -1.5 .. +1.5% accuracy @ T = 85°C, VCC = 3V.
So, if datasheet is right, both are accurate enough, and no priority can be decided.

But generally there is two different kinds of devices - a device from datasheet, and the real device. :-)

And what about baud rates, thanks for tool, but I already have self-made spreadsheet to calculate errors and possible baud rates. After common Mega I really admired new Fractional Baud Rate Generation feature of xMega. Huge respect to authors! It's simple in design but powerful. Now error is minimal at any system clock. Amazing.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
But generally there is two different kinds of devices - a device from datasheet, and the real device.

Agreed. And cool that the Xmega has up to 8 Usarts.

I have used both the internal 2 MHz and the internal 32 MHz osc's, as well as an external crystal, and have had reliable USART communications with other boards and with a PC. My boards have been indoors, in a relatively stable thermal environment.

I only have a couple of Xmega boards up and running, too, not ton's of mass produced boards to judge from. There are other threads, also, however, that also report good success using the internal clocks for USART communications.

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

But generally there is two different kinds of devices - a device from datasheet, and the real device.

Including the "errata"?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I also think that they must be Ok enough. If one of internal oscillators would be too wild then this would be discovered in short time and well documented. So, currently I'm sure that there is nothing too bad to expect from the clock system.

clawson wrote:

Including the "errata"?

Sometimes including errata too. :-(
I've read a number of articles about the way in which Atmel handles its datasheets and erratas updates, fixes silicon bugs, etc, and there is no cause to trust those docs too much, as for me.