Using an external crystal as a clock source

Information and discussion regarding the main Firewing development board

Using an external crystal as a clock source

Postby alan » Wed Jan 01, 2014 12:34 pm

The Firewing has a position to fit an external crystal, so I thought I'd give it a try. Then I had to work out how to switch from the internal oscillator to the external crystal, and how to set up the PLL etc.. Everything works fine, so I thought I'd write it up as a 'tutorial' with code sample and a handy Excel calculator ... but suddenly I don't understand why it works at all !

I'm using a 10 MHz crystal, so 'XT' or 'HS' modes are equally acceptable. To give users the greatest flexibility, I wanted to explain how to choose between these modes, so I looked at the code in the 'loader' listings.

Q1 - In more general terms, what is the difference between the 'r1' and 'r2' loaders, and how do I know which I'm actually running ?

Q2 - I am setting the NOSC bits in OSCCON to a value of b011 in my program ( as well as new values for PLLPRE, PLLDIV, and PLLPOST ) and everything works just fine. I can switch from the FRC to the crystal and go at full speed. Do I also need to manipulate the POSCMD bits in FOSC ( by changing values in the config section of the loader ) to choose between XT or HS modes depending on chosen crystal frequency ... or am I on the wrong track ? Not everyone has an ICSP programmer available to allow them to change the loader ...

Q3 - Microsoft state that "Direct clock switches between any primary oscillator mode with PLL and FRCPLL mode are not permitted. This applies to clock switches in either direction. In these instances, the application must switch to FRC mode as a transition clock source between the two PLL modes." ... well I've only just discovered that, and I seem to be doing just fine by ignoring it. Am I right - the Firewing loader starts up in FRCPLL mode ?

Any help appreciated - I normally get on fine with Microchip documentation, but this is just too complex for New Year's Day !

LATE ADDITION - so I kind of answered Q1 myself : it looks like the compiler will tell me which loader is errr ... loaded ( using menu "View / Loader information" ). This generates Q4 - if loader version r1 goes with an 'R1' board ( presumably a hardware revision ), and loader version r2 goes with an 'R2' board ... what's the difference ? Was it a hardware change that required a loader change ?

Thanks.
alan
 
Posts: 3
Joined: Sat Dec 07, 2013 6:04 pm

Re: Using an external crystal as a clock source

Postby David John Barker » Thu Jan 02, 2014 12:29 pm

I've never tried switching clock source myself, although I have successfully used Firewing with an external clock source in commercial products. There is no real difference between R1 and R2 loaders other than mapping of RX and TX to different pins. Unless your board explicitly states "R2", you will be using R1. The fuse settings are used to configure which clock source is used during POR only. If FCKSM is enabled (which the loader does) then clock switching is enabled. My basic understanding though is you need to execute a set sequence of ASM instructions to perform a clock switch (very much like a ROM write). There is a good Microchip document which shows what instructions are needed:

http://ww1.microchip.com/downloads/en/DeviceDoc/39726a.pdf

page 38:22 onwards should give you the information you need.
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: Using an external crystal as a clock source

Postby funlw65 » Sun Aug 15, 2021 2:18 pm

Using Microchip16 toolchain... if I use an 8MHz crystal, the spreadsheet calculator shows exactly 40 MIPS for PLLFBD=38:

clk1.png
clk1.png (183.58 KiB) Viewed 26778 times


But is this wise? To be so close? I mean, if my 8MHz crystal has a little tolerance, it can throw me in the red zone.

If this can happen, then the closest value is for PLLPRE=8 and PLLFBD=197:

clk2.png
clk2.png (194.08 KiB) Viewed 26778 times


I am thinking this wrong?
funlw65
 
Posts: 40
Joined: Thu Jan 16, 2014 2:32 pm


Return to Main Board

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x