Jump to content

18f452 / 18f4520 - Same Code Different Results .. Why?


Recommended Posts

I have been playing with some straightforward BoostC code to measure frequencies of oscillators, and have struck a problem that I can't understand.

Using an 18F452, I enable Timer1 as a counter for 1 second (delay_s(1)). Each time the high byte rolls over to 0, the ISR kicks a 32-bit Rollover variable by 1. At the end of the 1-second delay I disable the clock and interrupts and read the Timer1 Hi and Lo byte registers (using the one-gulp RD16 method - ie. read lo then hi, write hi then lo).

I use MakeShort() to combine the high and low bytes into a short, multiply the long rollover count by 65536, and add the long and the short to give a frequency value in Hz. OK so far. The circuit works fine on a breadboard, values seem reproduceable and roughly match those I see on the oscilloscope. This is reading an LC oscillator running at around 650KHz.

With a wire connected to Rc0 (Timer1 pin) on the 18F452, I get 0Hz when it's grounded, and 50Hz (presumably some mains hum) when it's floating. (Must add some more decoupling I guess).

I was pleased to find that when I connect the wire to the OSC1 pin, between the 20MHz crystal and the capacitor, I get a count of 20.142MHz.

Now, I'd like to use a 18F4520 for a couple of reasons. The 18F452 is obsolete I'm told, plus I'd like to use the comparator and voltage ladder that's on the 4520 to build up a all-in-one meter that measures frequency, Inductance and capacitance. The fact that the 4520 can run at 40MHz should help as well.

 

At last ... to the point.

When I take the exact same code that I have been running on the 18F452 and compile it for the 18F4520 (sure, configuration is different, but not much), the 18F4520 produces consistently lower counts from the same pair of oscillator sources.

For instance - the 452 reads 648.691KHz and 20.142Mhz, while the same code on the 4520 reads 589.906KHz and 19.570Mhz for the same 2 sources. That's 3% off at high speed and 10% off at low speed.

How can the 4520 be missing counts that the 452 detects under the same conditions? I haven't been able to even form a wild hypothesis that I can test. I'd expect the 4520 to have faster silicon, being the replacement for the 452, and so would have expected the results to be the other way.

 

Suggestions anyone?

Link to post
Share on other sites

Hi

 

I'm not familar with these chips but I intend to order some next time I do. Maybe............

 

Are your readings rock solid or do you get random events like; 19.583 and then 19.699?

 

If they are random, not stable, your measurement intrument ( scope probe, whatever ) is loading (pulling) your source.

I found your example of 20.142 mHz to be a tremendous amount of error to indicate a software problem.

My experience with crystals of that range should only vary 10 to 15 kHz at the most. 142 kHz is a hugh amount of error.

Crystals for micros are mostly parallel coupled. Maybe the crystal is a series type. If you have a series type it would account for the error providing the software is right on. Which I'm sure you've checked and rechecked.

 

I've made many of these frequency counters in the past and am in fact working on a new faster program.

 

The different is value of the two chips can be expected at least I think. Two different chips; 2 different input impedances.

Link to post
Share on other sites
I found your example of 20.142 mHz to be a tremendous amount of error to indicate a software problem.

 

My experience with crystals of that range should only vary 10 to 15 kHz at the most. 142 kHz is a hugh amount of error.

 

I've made many of these frequency counters in the past and am in fact working on a new faster program.

 

The different is value of the two chips can be expected at least I think. Two different chips; 2 different input impedances.

 

Hi Carlos

Thanks for your comments, most helpful. Although .. where I thought I had just one problem, in fact I had two!.

I had no idea how what sort of accuracy to expect from a 20MHz crystal, the one in question was marked "SIWARD 20.000".

Of course, you are correct, I had left a software error in the LCD print routine that converts the raw count to MHz.

The 100mS count for both of 2 different 20MHz crystals converts to 20.042730 MHz, 43KHz off, still more than the 10-15KHz you mention. The circuit is running on a plastic prototyping "breadboard", so lots of stray capacitance about, and I don't know what the timing accuracy is like on the BoostC delays. Hopefully things will improve when I transfer to a PCB.

 

I haven't found any information on port input impedances for the 18F452 and 4520 in the data sheets for these chips. The input is coming from the output pin of a LM311 comparator that is driving the oscillator LC "tank". The waveform is triangular with a swing from 0-4V. There is a 6.8K resistor in the line to the PIC. (This is an old much-built circuit, many versions on the web)

Would you expect different impedances to pull or damp the oscillator to different degrees, changing the frequency, or just cause the counter to read higher on the waveform and skip counts?

 

Best Regards

PeterJL

Link to post
Share on other sites

The diff in input impedance would be minimal and Microchip may not spec it.

Still there would be some. It all depends if the pins interface internally to a

bipolar or FET device, low and high respectively.

 

Stray capacitance can be murderous to a developing circuit as you say.

 

I've had problems with these "SOFTWARE" counters that gate with timers.

 

Your problem could only be the fact that the crystal may be a series type.

 

I suggest you try a half size TTL clock oscillator. Since these are buffered it

is unlikely that your circuit will pull it. Thats critical for these counters. I bought

hundreds of these on eBay. They are 25 mHz so I'm overclocking slightly.

You can check overclocking tests on Sparkfun.

 

There is a drawback....no trim adjustment!

 

I'd like to see the schematic.

 

Right off though, hitting the micro with a square wave will help.

You need sharp edges. A saturated transistor, 2N3904, with do that.

Common emitter, 5 volts though 470 ohms to collector. Output at the collector.

 

You may contact me directly [carl@n9efj.net] if you wish.

Link to post
Share on other sites
I have been playing with some straightforward BoostC code to measure frequencies of oscillators, and have struck a problem that I can't understand.

Using an 18F452, I enable Timer1 as a counter for 1 second (delay_s(1)). Each time the high byte rolls over to 0, the ISR kicks a 32-bit Rollover variable by 1. At the end of the 1-second delay I disable the clock and interrupts and read the Timer1 Hi and Lo byte registers (using the one-gulp RD16 method - ie. read lo then hi, write hi then lo).

I use MakeShort() to combine the high and low bytes into a short, multiply the long rollover count by 65536, and add the long and the short to give a frequency value in Hz. OK so far. The circuit works fine on a breadboard, values seem reproduceable and roughly match those I see on the oscilloscope. This is reading an LC oscillator running at around 650KHz.

With a wire connected to Rc0 (Timer1 pin) on the 18F452, I get 0Hz when it's grounded, and 50Hz (presumably some mains hum) when it's floating. (Must add some more decoupling I guess).

I was pleased to find that when I connect the wire to the OSC1 pin, between the 20MHz crystal and the capacitor, I get a count of 20.142MHz.

Now, I'd like to use a 18F4520 for a couple of reasons. The 18F452 is obsolete I'm told, plus I'd like to use the comparator and voltage ladder that's on the 4520 to build up a all-in-one meter that measures frequency, Inductance and capacitance. The fact that the 4520 can run at 40MHz should help as well.

 

At last ... to the point.

When I take the exact same code that I have been running on the 18F452 and compile it for the 18F4520 (sure, configuration is different, but not much), the 18F4520 produces consistently lower counts from the same pair of oscillator sources.

For instance - the 452 reads 648.691KHz and 20.142Mhz, while the same code on the 4520 reads 589.906KHz and 19.570Mhz for the same 2 sources. That's 3% off at high speed and 10% off at low speed.

How can the 4520 be missing counts that the 452 detects under the same conditions? I haven't been able to even form a wild hypothesis that I can test. I'd expect the 4520 to have faster silicon, being the replacement for the 452, and so would have expected the results to be the other way.

 

Suggestions anyone?

 

 

I may be off here but if you are measuring the frequency of the crystal that is running the pic I would think any error in the crystal would throw off the circuit just enough to cancel any error in the crystal. So any error would indicate a software or hardware problem.

 

Russ

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...