PIC newbie - clock configuration - Page 1
Forum section: electronics / beginners

author: pman92
author_rank: Contributor
timestamp: June 15, 2023, 10:46:57 am
content: Hey guys,I'm working on a project that will use the J2716 SENT protocol (receiving SENT data, modifying it, and then retransmitting it on to the ECU it was originally destined for).Since I started out with Arduinos years ago, I'm fairly familiar (at least as far as a hobbyist goes) with AVR's - I've done quite a few projects with them. But after finding out the dsPIC's have a built in SENT hardware module I thought it would be a great opportunity to throw myself in the deep end with them.I've bought myself a pickit, downloaded MPLAB X and the XC16 compiler, and designed up and ordered a simple PCB to break out some of the I/O pins to start experimenting. The PCB's I ordered include a DSPIC33EV256GM102. I haven't received them yet but I've been reading the datasheet and playing with MPLAB X in preparation.I've got a few questions regarding configuring the clock. I included a 16MHZ crystal on the PCB between pins OSC1 + OSC2 with some 20pF capacitors.(newbie mistake: I didn't realize the PIC instruction cycle clock (Fcy) would be less than the crystal frequency (Fosc), so it will actually be running at 8mhz, but that doesn't really matter anyway for now).1. Should I have the 1 meg ohm resistor between the OSC1 + OSC2 pins? (See attached picture)Looking at oscillator configuration in the datasheet there is no mention of it: (Figure 9-1 on P127)Datasheet linkLooking at the relevant "Family reference manual" it says it is required: (Page 3)Reference Manual link2. What is the benefit of the IESO (Internal External startup bit option) in the FOSCSEL register, IE. starting up with the internal oscillator and then switching the the configured external oscillator? Is there any reason not to disable that and startup straight from whatever I have configured?3. To use the external 16MHZ crystal instead of the internal one, am I correct that all I basically need to do is:- Set FNOSC to 010 (Primary XT/HS/XC) in the FOSCSEL register- Set POSCMD to 10 (HS mode - 10mhz to 40mhz) in the FOSC registerThanks in advance

author: Ian.M
author_rank: Super Contributor
timestamp: June 15, 2023, 12:47:43 pm
content: You've got more options than that - I believe your PIC's oscillator module supports HSPLL mode, so set the PLL prescaler to ÷4 to get your 16MHz crystal down to 4MHz, near the middle of the PLL's lock range, set PLLDIV for a multiplier of 64 to get 256MHz Fsys, (near the middle of the VCO range) and set the postscaler to ÷8 for 32MHz Fosc giving you the 16 MIPS performance you originally intended. Obviously other Fosc frequencies are possible without changing the crystal, following the formulae and limits on page 128 of the datasheet. Note that the various divider and multiplier values are offset - check the formulae and the register descriptions carefully to get the actual number that must go in each register to get specific ratios.1. The family reference manual generally gives more details of the peripheral in question than the datasheet. As the FRM Figure 5-1: Crystal or Ceramic Resonator Operation (XT or HS Oscillator Mode) shows the 1 Meg resistor when a crystal is used, then yes you do need the external bias resistor. Its odd that it also shows an integrated internal bias resistor in parallel with it, but the most likely explanation is that Microchip found that was inadequate for operation over the full voltage and temperature range of some parts so fixed the documentation rather than the silicon and added the external resistor. If you haven't made provision to fit it, you really should, even if you have to glue down the resistor and bodge-wire it, as your mention of J2716 and an ECU implies an automotive application, so even if it appears to oscillate correctly on the bench, I'd want it for a higher probability of reliable operation over the temperature range its likely to experience.2. IESO lets your code run even if the external crystal or clock source has failed,or as Mike points out below, hasn't started yet. Whether or not that's useful depends on how accurate an oscillator you need for basic functionality, but at least if the crystal is bad you can blink a fault code to aid diagnosis and repair rather than the board appearing totally dead.3. I don't use your PIC so cant verify that. Try a simple blinky LED test program. If not, MPLAB code configurator should give you workable settings for any supported clock configuration - dig into its generated code and copy what SFR and configuration settings you think it needs to your blinky LED project, verifying each against the datasheet as you go. OTOH, if you don't mind the bloat, use the MCC code as generated!

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: June 15, 2023, 12:53:17 pm
content: Quote from: pman92 on June 15, 2023, 10:46:57 amnot usually needed but can help startup time on marginal crystals.QuoteMuch faster startup, e.g. if you need to periodically wake from sleep, check something and only start the crystal if you need to stay awakeOr if you need to do something very quickly after powerup

author: pman92
author_rank: Contributor
timestamp: June 16, 2023, 01:51:56 am
content: Thanks for the help.I didn't try out the MCC before this post, but it looks like I should be able to experiment with that to learn a lot of stuff. I was quite surprised how easy it was to use and what it can do.Definitely a lot more features on PIC's than I've ever used before. I was a bit hesitant to try them out before, but now I'm starting to get my head around it I'm glad I did.So far the only potential downside I can see is having to use C rather than C++. Which probably isn't much of a downside really, especially for small embedded stuff you would be using a PIC for.Very excited to start looking into the debugging stuff. I've got absolutely zero experience with that but I can see it being very useful.Cheers