Author Topic: Why is an external oscillator needed?  (Read 12531 times)

0 Members and 1 Guest are viewing this topic.

Offline boingaonTopic starter

  • Newbie
  • Posts: 9
Why is an external oscillator needed?
« on: January 05, 2013, 07:02:02 am »
Hello,

I have come across several circuit diagrams where an external oscillator is connected to a microcontroller that already has an oscillator built-in. Why would someone want to do this, and how does the microcontroller know which oscillator takes priority?

Here's an example:

The AtMega1284P has an internal 20MHz oscillator.
http://maniacbug.wordpress.com/2011/11/27/arduino-on-atmega1284p-4/#comment-874
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10224
  • Country: nz
Re: Why is an external oscillator needed?
« Reply #1 on: January 05, 2013, 07:08:48 am »
The internal oscillator isn't very accurate and it can cause problems when you use the UARTS to communicate or do anything which requires exact timing

If you checkout the UART section of the datasheet you will notice tables of UART error rates at various clock frequencies.
For the UART to work without communication errors it requires a clock rate that's a multiple of the serial baudrate which is where an external oscillator is useful.
You will also notice that the built in oscillator cannot go as fast as the micro supports. On AVRs they often max out at 8mhz while the chip may run up to 20mhz

There's no "priority", you need to tell the microcontroller which clock source to use using the fuse bits (and what speed to run at if using the builtin oscillator).
« Last Edit: January 05, 2013, 07:15:11 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline boingaonTopic starter

  • Newbie
  • Posts: 9
Re: Why is an external oscillator needed?
« Reply #2 on: January 05, 2013, 07:13:02 am »
Thank you for the speedy reply. So if you're not using serial communication, the internal oscillator may serve just fine?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10224
  • Country: nz
Re: Why is an external oscillator needed?
« Reply #3 on: January 05, 2013, 07:17:03 am »
Yeah, if you're not doing anything that needs a fast clock or exact timing the internal oscillator will work fine.

Serial communication will actually work using the internal oscillator if you pick your baudrate wisely.
Its just more prone to getting a character wrong every so often.



« Last Edit: January 05, 2013, 07:19:58 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline boingaonTopic starter

  • Newbie
  • Posts: 9
Re: Why is an external oscillator needed?
« Reply #4 on: January 05, 2013, 07:19:32 am »
Thanks again.

If the oscillator frequency is say 5x the desired baud rate, how exactly do you go about converting that multiple from 5x to 1x? Do you use a prescaler?

If a prescaler is used, then couldn't you apply the prescaler to just about any frequency you want, and not have to worry about getting a multiple of the baud?



 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 10224
  • Country: nz
Re: Why is an external oscillator needed?
« Reply #5 on: January 05, 2013, 07:26:15 am »
The UART does most of it for you, its got a hardware timer built in.
All you do is set the correct UART control bits to specify the baudrate you want (which effectively sets its internal prescaler).
Some baudrates will have more errors than others. (see the datasheet for the table of these values)

eg, taking an ATmega8 as an example.

If using 4mhz internal osc
2400,4800,9600 are 0.2% error
but after that it quickly climbs to 8.5% error

If you change to a 3.6864mhz external crystal (slower)
The error rate is 0.0% at all common baudrates

« Last Edit: January 05, 2013, 07:31:30 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4307
  • Country: us
Re: Why is an external oscillator needed?
« Reply #6 on: January 05, 2013, 07:31:30 am »
Quote
The AtMega1284P has an internal 20MHz oscillator.
No, it doesn't.
Or you're not understanding the difference between an oscillator circuit and an oscillator module.
The ATmega1284P has a built-in "calibrated" RC oscillator module that runs at approximately 8Mhz.
And another module that runs at about 128kHz and isn't calibrated.
It also has a couple of "crystal oscillator circuits" that are designed to work with an EXTERNAL crystal as a timing reference (up to a 20MHz crystal),  which is what the schematic you referenced shows.
There is another kind of external crystal oscillator that includes both crystal and some electronics to make it oscillate (typically has 4 pins, two of which are for (significant amounts of) power.

You'd use an external crystal with the chip's internal oscillator circuits whenever you want a frequency other than the 8MHz, 1MHz (with a divider), or 128kHz provided by the completely internal circuits, OR you want better accuracy than the internal oscillator circuits (which do NOT have a crystal) can provide.
 

Offline boingaonTopic starter

  • Newbie
  • Posts: 9
Re: Why is an external oscillator needed?
« Reply #7 on: January 05, 2013, 07:48:17 am »
Making those kinds of mistakes aids in learning faster. Thanks for pointing that out.
 

Offline CarlG

  • Regular Contributor
  • *
  • Posts: 155
  • Country: se
Re: Why is an external oscillator needed?
« Reply #8 on: January 05, 2013, 10:03:50 am »
If the oscillator frequency is say 5x the desired baud rate, how exactly do you go about converting that multiple from 5x to 1x? Do you use a prescaler?

Baud rate of what? Are you using a synchronous or asynchronous interface)? In general, if it's a asynchronous interface (UART) the clock to the UART peripheral typically needs to be at least 16x the baud rate (16x is more or less standardized as far I know, though it's not a universal law that it must be that high). On top of that, there might be fixed clock dividers either for the peripheral clock or inside the peripheral so you might end up with a baud rate of fosc/32 or less. It's different for different chips; you'll have to read the datasheet and the user's manual of the specific device you're using to find out.

The reason for the 16x factor is that the UART peripheral must be able to sample the incoming data stream at a higher rate than the actual baud rate, first in order to find the start bit, then sampling the data, e.g by a majority decision of 3 samples offset by 6-7 bit times peripheral [sample] clock periods from when the start bit was detected.
//C
« Last Edit: January 05, 2013, 11:38:49 am by CarlG »
 

Online amyk

  • Super Contributor
  • ***
  • Posts: 8404
Re: Why is an external oscillator needed?
« Reply #9 on: January 05, 2013, 11:15:23 am »
Also applicable but not in all circumstances is that sometimes you will want a synchronised clock throughout the entire system, and in that case the MCU is just fed a single clock signal input on one of the XTAL pins; the other XTAL pin is essentially the output of an inverter and used to provide feedback to the crystal.
 

Offline svofski

  • Regular Contributor
  • *
  • Posts: 53
  • Country: ru
    • svo's interactive persuasion vehicle
Re: Why is an external oscillator needed?
« Reply #10 on: January 05, 2013, 11:45:50 am »
If you're designing for indoors, not aiming at some particular high UART speed (115200 could be problematic, but slower rates are fine), not trying to generate video signals or keeping real time clock from the main clock (silly idea anyway), not needing more than 8MHz clock, I'd say you're perfectly fine with the builtin RC oscillator on ATmega. I used builtin RC a lot and I only had a problem with factory calibration once — fixed it by writing OSCCAL word later. Other types of microcontrollers may differ but I cannot say much about them.

On a somewhat related note, I remember that on MSP430 you can crank the builtin DCO up to about 18MHz, but it's not going to be the same clean 18MHz as from a real crystal oscillator — more like spread-spectrum random phase signal with average frequency of about 18MHz.
The dark boxes are coming
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf