Choosing a crystal for PIC USB project - Page 1
Forum section: electronics / general

author: BytesGuy
author_rank: Regular Contributor
timestamp: August 27, 2015, 07:46:49 pm
content: Hi everyone,This may seem like a bit of a simple question, but with the PIC18F4550 which frequency should be chosen for the crystal?I have been reading the datasheet and understand that the USB module needs to be running at 48MHz, but the rest of it was a bit confusing. It seemed like you could use pretty much any frequency crystal (4MHz, 10Mhz, 20Mhz etc), so what is the advantage of using a higher one when you could just chose 4MHz for example.Perhaps I have the wrong end of the stick hereThis is quite ambitious for my first PIC project, but I am determined to learnI have used things such as Arduino, Picaxe, FPGAs etc, but they have always been with pre-built boards.Thanks!

author: Brutte
author_rank: Frequent Contributor
timestamp: August 27, 2015, 08:13:31 pm
content: QuoteYou need a 48MHz clock for USB FS or 6MHz for USB LS so not any.

author: BytesGuy
author_rank: Regular Contributor
timestamp: August 27, 2015, 08:25:18 pm
content: Quote from: Brutte on August 27, 2015, 08:13:31 pmYeah I realise it needs 48MHz, but the datasheet was talking about being able to derive 48MHz from various crystal frequencies - this the bit that is confusing me.

author: Ian.M
author_rank: Super Contributor
timestamp: August 27, 2015, 08:47:39 pm
content: The crystal needs to be an exact multiple of 4MHz for the PLL and divider chains to be able to generate the USB clock. 10MHz is *NOT* suitable. The maximum crystal frequency for USB operation is 24MHz, but a higher external clock can be used up to 48MHz. See datasheet fig. 2-1 and oscillator CONFIG settings from table 2-3

author: MarkF
author_rank: Super Contributor
timestamp: August 27, 2015, 11:40:23 pm
content: The key thing to remember here is that you're not only picking a frequency for USB support; but you're selecting the processor frequency. The clock for the USB Peripheral and the CPU clock are from the same source. So pick the CPU clock frequency you want that will still support the USB operation.See FIGURE 2-1: PIC18F2455/2550/4455/4550 CLOCK DIAGRAM andTABLE 2-3: OSCILLATOR CONFIGURATION OPTIONS FOR USB OPERATION.

author: marshallh
author_rank: Unknown
timestamp: August 28, 2015, 04:59:19 am
content: Some crystals may be harder to find in some form factors than others. I.e. a tiny SMD one versus HC49.

author: hans
author_rank: Super Contributor
timestamp: August 28, 2015, 07:55:43 am
content: See page 26 of the datasheet.The PIC contains a 4 to 96MHz PLL. This multiplies the frequency basically. The frequency is divided by 2 which makes the USB clock of 48MHz.The CPU clock can be chosen quite flexible, with it's own post-divider. So you could run the CPU at 48MHz too.If you look at the diagram, there is a divider before the PLL to get your 4MHz input frequency. Think backwards to get the crystal frequency; 4MHz x prescaler. That means this PIC supports USB from 4, 8, 12, 16, 20, 24, 40 and 48MHz clock sources. In theory you could pick any, but I would stick to 12MHz as it's a very common frequency for USB applications. (likely to match 12Mbit/s USB speed)This particular PIC has no way to clock the USB from the internal FRC crystal. Some newer PIC18's can from the K-series I believe, and also some decent PIC24's have accurate enough oscillators for basic USB operation.

author: Brutte
author_rank: Frequent Contributor
timestamp: August 28, 2015, 10:18:53 am
content: So now when you know a 10MHz won't do, one more thing worth noting is that if you plan to use XC8 compiler then in a crippled mode the USB stack eats up half of the memory.

author: BytesGuy
author_rank: Regular Contributor
timestamp: August 28, 2015, 12:26:13 pm
content: Thank you all for your input, I really appreciate it!I think I will chose a 12MHz crystal then, I have a bunch lying around somewhere. The frequency for the processor doesn't need to be anything more than that, I'm not going to be doing anything taxing or dependent on speed.Brutte, interesting to hear that about the compiler... I guess they want you to pay for the upgraded versionAny suggestions for alternatives?

author: Brutte
author_rank: Frequent Contributor
timestamp: August 28, 2015, 07:36:18 pm
content: Quote from: BytesGuy on August 28, 2015, 12:26:13 pmI would suggest aNucleo-STM32L152orSTM32L152C-DiscoveryThese run similar 32MHz Cortex-M3 chips. Start with uVision (32k limit?) and run through all blinky examples provided by STM and Keil. Once you learn how to blink with uVision, you can move to gcc-arm-embedded toolchain which is not as cutting edge but free (as in freedom).