Why is an external oscillator needed? - Page 1
Forum section: electronics / general

author: boingaon
author_rank: Newbie
timestamp: January 05, 2013, 07:02:02 am
content: 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

author: Psi
author_rank: Super Contributor
timestamp: January 05, 2013, 07:08:48 am
content: 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 timingIf 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 20mhzThere'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).

author: boingaon
author_rank: Newbie
timestamp: January 05, 2013, 07:13:02 am
content: Thank you for the speedy reply. So if you're not using serial communication, the internal oscillator may serve just fine?

author: Psi
author_rank: Super Contributor
timestamp: January 05, 2013, 07:17:03 am
content: 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.

author: boingaon
author_rank: Newbie
timestamp: January 05, 2013, 07:19:32 am
content: 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?

author: Psi
author_rank: Super Contributor
timestamp: January 05, 2013, 07:26:15 am
content: 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 osc2400,4800,9600 are 0.2% errorbut after that it quickly climbs to 8.5% errorIf you change to a 3.6864mhz external crystal (slower)The error rate is 0.0% at all common baudrates

author: westfw
author_rank: Super Contributor
timestamp: January 05, 2013, 07:31:30 am
content: QuoteNo, 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.

author: boingaon
author_rank: Newbie
timestamp: January 05, 2013, 07:48:17 am
content: Making those kinds of mistakes aids in learning faster. Thanks for pointing that out.

author: CarlG
author_rank: Regular Contributor
timestamp: January 05, 2013, 10:03:50 am
content: Quote from: boingaon on January 05, 2013, 07:19:32 amBaud 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-7bit timesperipheral  clock periods from when the start bit was detected.//C

author: amyk
author_rank: Super Contributor
timestamp: January 05, 2013, 11:15:23 am
content: 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.

author: svofski
author_rank: Regular Contributor
timestamp: January 05, 2013, 11:45:50 am
content: 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.