PICs, crystals and headscratching - Page 1
Forum section: electronics / beginners

author: baljemmett
author_rank: Unknown
timestamp: August 11, 2010, 07:47:53 pm
content: Bit of a newbie question here, so thought this seemed a good place for it -- hopefully somebody can offer some advice...I'm working on a small (literally, a 12x14-hole piece of stripboard) PIC-based project which seemed to be suffering from timing inaccuracy -- receiving RS232 using the internal oscillator gave framing errors on about 1.5% of the received bytes, even after as much fiddling as I can stomach. Some testing revealed the clock drifts pretty significantly during use, so I figured the easiest thing to try would be a quartz crystal at 3.6864MHz (baud-rate friendly, so eliminating both possible causes of inaccuracy at the same time.)The first time I fired it up with the crystal wired up, my timing test passed with flying colours. So far, so good. However, I then had to fix a solder bridge on some unrelated pins that I'd accidentally introduced when adding the crystal, and now whatever I do I simply cannot get the external oscillator to work.I can think of three possible causes for this: the way I've attached the crystal is hopeless (crystal + caps soldered "dead bug" style in free air, hooked up to the main board with ~1" of fine enamelled wire to each leg -- I didn't want to commit board space to anything before I'd tested it!), or the PIC is somehow damaged and so failing to kick the resonant circuit into life, or alternatively I simply don't have a clue what I'm doing and should go back to the day job.My diagnostics so far have proved hopeless, as I can only find a +5v signal on the OSCOUT pin which might have a high-frequency signal of about 1mV superimposed over it -- as best as I can tell, I should be seeing approximately a 5v peak-to-peak sine wave on that pin if the oscillator's started up correctly. Everything else seems to check out in terms of connectivity -- no shorts or open circuits.Does anybody have any thoughts or suggestions of things I could double-check before I try putting a fresh PIC in? If it's possible that I damaged the first one somehow, I'd rather not do the same thing to a second!Cheers for any advice offered...

author: DavidDLC
author_rank: Frequent Contributor
timestamp: August 11, 2010, 07:52:54 pm
content: Please share a picture of the system you are working on.

author: jahonen
author_rank: Super Contributor
timestamp: August 11, 2010, 07:56:15 pm
content: Do you have oscillator type correctly configured, XT, HS etc.?Regards,Janne

author: Simon
author_rank: Unknown
timestamp: August 11, 2010, 08:53:07 pm
content: check you have the registers set-up correctly for the clock, i spent an hour arsing about with a breadboard prototype trying to figure why a simple program did not work only to realise I had not configured the internal clock and the compiler defaulted to external crystal.What are you programming with ?

author: baljemmett
author_rank: Unknown
timestamp: August 11, 2010, 08:57:21 pm
content: DavidDLC: I think I'm managed to attach one to this -- not great photos, and not exactly great soldering either alasI've had to compromise and rework the layout a couple of times, and it shows... The crystal is hooked up with those two S-shaped enamelled wires around the centre of the board, just because that's the only board space I have left.Janne: Yep, I've used XT mostly -- that was what it worked with on the one occasion it did work -- and have also tried HS in case the crystal needed a higher drive level. Unfortunately the Microchip app notes say "use HS for crystals that require a higher drive level" but I don't know what qualifies as 'higher'... I've also disabled two-speed startup and the fail-safe mode, otherwise it (of course) just uses the internal oscillator.Simon: As far as I can tell, they're all set up right; I know it worked once with these settings, at any rate, and changing the oscillator type in the configuration word back to 'internal with I/O on pins' gets the chip running (at the faster 4MHz speed) -- so I think the 'XT' setting is being picked up correctly when I set it. I'm working in MPLAB ASM.

author: DavidDLC
author_rank: Frequent Contributor
timestamp: August 11, 2010, 09:27:28 pm
content: Can you solder the XTAL and caps underneath the board ?Try to solder them the closest possible to the microcontroller. This way you will minimize the noise added to the osc.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: August 11, 2010, 09:54:38 pm
content: Although calibrated to 1%, I've seen PIC oscillators have quite a lot of jitter, which can cause problems especially at higher baudrates.Noise on the supply can also increase jitter.Crystal startup problems can occur if the stray capacitance between the xtal pins starts to approach that of the load capacitors, so poor layout can be problematic. In some cases this can be fixed using a high value (~1M) resistors across the crystal.

author: baljemmett
author_rank: Unknown
timestamp: August 11, 2010, 10:53:53 pm
content: Quote from: DavidDLC on August 11, 2010, 09:27:28 pmI'll see if they'll fit -- they may make the assembled board too thick to fit in the space available in the housing I intended to build this in. I suppose worst case is I abandon that idea and use a less cramped board!Quote from: mikeselectricstuff on August 11, 2010, 09:54:38 pmYep, I see about 5% jitter on the internal oscillator; the data sheet for this 16F689 quotes accuracy of 2% at room temperature and 5V supply. I've run it on the 4MHz setting at 300, 1200, 2400, 4800 and 9600 baud and get framing errors on about 1.2% - 1.5% of the data regardless.Quote from: mikeselectricstuff on August 11, 2010, 09:54:38 pmAt times like this I wish I had a more capable multimeter! I can't measure stray capacitance, unfortunately, but I'll see if I have a 1M resistor or thereabouts in the junkbox and give that a shot...

author: Simon
author_rank: Unknown
timestamp: August 12, 2010, 06:55:43 am
content: That's a mess !Really can't you breadboard it to get it going first ? or just redesign it with the crystal, I don't know what your making and why space is a premium before you've even got it going but really although I'm no expert I can't see the point in anyone offering advice until you have a clean and tidy linkup between the crystal and pic, you could try all the software approachs available only to find it was your dodgy wiring, that much wire hanging around will probably make your clock about as unstable as the pics own internal one if not moreQuote from: baljemmett on August 11, 2010, 08:57:21 pm

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: August 12, 2010, 08:29:11 am
content: QuoteThis isn't the sort of thing that you can measure easily as capacitance of leads etc. overwhelms what you are measuring - it's just a case of good layout.A useful hint I picked up at a seminar years ago, when talking about optimising load capacitance etc. is that a 'happy' xtal oscillator has similar signal levels on both pins. If the levels are significantly different, then frequency accuracy and startup margin is probably non-optimal.However you need to take care to use a low-capacitance scope probe like a x100, or put a 10M resistor in series with a normal probe.

author: baljemmett
author_rank: Unknown
timestamp: August 12, 2010, 12:08:06 pm
content: Quote from: Simon on August 12, 2010, 06:55:43 amA fair pointYep, I know it's a mess -- it looks/looked a lot better without the connectors and associated wiring cluttering the shop, of course. I must admit I skipped the breadboarding for this one because in terms of wiring it's almost identical to an earlier project that worked fine, just with a slightly larger PIC; had I known the crystal was going to be requred I would probably either have breadboarded it first or abandoned the idea! Plus if the crystal hadn't worked the first time I tried it (with much worse layout) I wouldn't even be wasting time with trying to get it to work in its current state.The device is a RS232 to parallel converter; nothing exotic, but the idea was to assemble it from mostly scrap-box parts including the enclosure (one of those small boxes with D-sub connectors at each end and a space for wiring/circuitry in between). Hence the space constraint, although at this point I fear abandoning that and going for a soap-on-a-rope design may be more workable.Actually, looking at those photos with fresh eyes this morning, I think I've just noticed a mistake which means last night's testing was totally pointless -- will check that out when I get home this evening.

author: Mechatrommer
author_rank: Super Contributor
timestamp: August 12, 2010, 12:20:21 pm
content: try evasive action:1) go back to internal osc setup and see if its working.2) try another chip and check for any connection errors (short, open etc)3) try another oscilator, maybe it fried4) restart design on breadboard as Simon suggested

author: baljemmett
author_rank: Unknown
timestamp: August 14, 2010, 12:14:42 pm
content: Well chaps, thanks for the kick up the backside -- sometimes it does indeed take someone beating you around the head with what you already know ("you're doing this the hard and stupid way!")I fixed the daft mistake I spotted in the photos (there's a wire sticking out at the right-hand side that can be shorted to ground to put the PIC into a frequency test loop at startup or dump debug stats to the printer when running -- I'd broken it off the PCB pin when getting the chip out for reprogramming during testing the other night and reattached it to the wrong one, foolish!), but although that allowed me to confirm it still worked on the internal oscillator I still had no luck with the crystal.This morning I decided I was obviously making life too difficult and plonked the PIC, crystal and caps on a breadboard (I need to invest in a few more, had to dismantle part of another project to fit it on!), tried a couple of different layouts for the crystal and still couldn't get a peep out of it. At this point I figured I had nothing more to lose and tried a fresh PIC -- instant success! Now I get a lovely baud-rate-friendly clock; I guess I must have somehow blown up part of the oscillator driver on the first chip? Anyway...After lunch I'll squeeze the oscillator onto the breadboard (there's just about enough room to put it under the MAX232, where there's only a half-inch wire run to the PIC and a handy ground track), then we'll see if that sorts the timing accuracy out. I have high hopes, looking at the timing stats my logic analyser's showing me from the breadboard!Cheers again guys, a valuable lesson learned here

author: Simon
author_rank: Unknown
timestamp: August 14, 2010, 01:37:59 pm
content: your welcomegood luck with itI cringe every time I put a new program together (not to mention project) as there are so many things to look out for, my last experience was altering a program rearranging the I/O's on the pic as i was adding things and had to make the best use, I had copied and pasted the program into a new window of the compiler and spent an hour trying to figure out why i was getting really weird behaviour and no sense out of the pic before i realized I had not set the clock up at all, it was set for an external crystal but I was using the internal one.

author: RayJones
author_rank: Frequent Contributor
timestamp: August 14, 2010, 10:18:34 pm
content: The first thing I do, despite the complexity some of my programs end up as, is to write a simple LED flasher or similar to prove the correct config bits etc.Next step is a CRO to check timings, then go wild with the more complex stuff.Likewise when you start using a peripheral, make a test program that solely exercises that part.I refrained from commenting upon the original photos earlier, but yeah, good luck with that configuration, especially with such long leads on the Xtal and caps. (inductance effects).The data sheets usually state these particular parts should be intimate with the PIC, and preferably with a ground plane beneath.If you do anything, I think a PCB that simply hosts the PIC, Xtal and caps and the rest of the signals brought out as berg strips would be a useful standard for quick lash ups.An improvement would be to include a RJ12 that matches the standard used by microchip / CCS for ICSP.Hmmm, may fire up Protel just to design such a handy piece of kit now...

author: RayJones
author_rank: Frequent Contributor
timestamp: August 15, 2010, 12:04:56 am
content: Protel has been bashed about, and these fell out the back of it:Note that the Bottom Layer is not mirrored, ie it's as it would appear looking down through the board.

author: RayJones
author_rank: Frequent Contributor
timestamp: August 15, 2010, 12:30:20 am
content: I suppose I should add that I designed this for a PIC18F2580, which is pin compatible with a PIC16F876A.Both of these devices I have been using of late, so this board would be useful to myself.If others also can make use of it, why notNo doubt the board could be modded for the ubiquitous 16F84 varieties, but that can be someone else's turn

author: baljemmett
author_rank: Unknown
timestamp: August 16, 2010, 03:23:48 pm
content: Hmm, I thought I replied to this yesterday -- seems not. Bah. Sorry about that, wasn't ignoring folk.One thing this little voyage of discovery has shown me is that I really should get my head around etching PCBs at some point. Never done it, and not sure how I'd go about it, so some research needed there. I see you have a thread elsewhere that may be useful for that!As it happens, that bonkers dodgy incompetent lashup of crystal and caps floating around did actually work fine once I put a fresh PIC in (I tried it for interest's sake, nothing more!); now it's all soldered down properly reasonably near the chip and running very nicely. If I had intended the system to use a crystal originally I'd have designed the board layout to accomodate it following the best practices in the app notes etc., of course