|
|
View previous topic :: View next topic |
Author |
Message |
darren logan Guest
|
Precision 1 second counter |
Posted: Sun Nov 03, 2002 9:53 am |
|
|
Hello,
I need a precision 1 second (1Hz) counter in my circuit.
Couple of options I can think of are:
1. Use a real time clock (like a DS1302) - this is a relatively expensive method.
2. Use a watch crystal controlled logic oscillator (connected to a 4060), then divided once more to give 1Hz.
- Much cheaper than option 1.
Now then, my question to you all is can I do away with any external components (like the above) and simply use the PIC (run from a precision crystal) to do the 1 second count for me ?
Im thinking perhaps using the RTCC (timer0).
Code examples from anyone who has implemented such a system would be great.
Cheers,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 8451 |
|
|
johnpcunningham Guest
|
Re: Precision 1 second counter |
Posted: Sun Nov 03, 2002 10:18 am |
|
|
There are 2 basic ways to do this within the PIC chip:
1) Software loops
Create assemply delay lopps that count for 1 second. Each command will take 1 clock cycle (jumps take 2). Probably have to create a counter in a counter. This method can give you precise 1Hz signals but the MCU wont be able to do anything else cause it is caught up in a software loop. If this is all your are doing then it is easy.
2) Timer1 control
Timer 1 can count up to 16 bits and can be pre-scaled by up to 8. If you were running a 4MHz clock the max timeout would be:
[(2^16) * prescale)/(clock mhz/4) =
[(2^16) * 8)/(4000000/4) = .524288 seconds
a 32.768khz clock could give you:
[(2^16) * 8)/(32768/4) = 64 seconds
For 1 second, set the divider to 1 and the counter value to 65565-8192 (8192 counts is one second). That way it will time out properly and interrupt.
[(65565-8192) * 1)/(32768/4) = 1 seconds
To interrupt properly and to get an exact 1 second you would set the divider up to a achieve a little under 1 second. How much less? The time it takes to interrupt, to run the command to turn on or off an output pin, and to reset new values to TIMER1s 16bit count value.
Unless you are running the entire MCU off of a clock that is made for accurate time (i.e. 32.768KHz), Timer 1 has the advantage of having its own clock inputs. You could run the processor on a different clock, say 4 mhz, and be able to keep accurate counts.
JC
___________________________
This message was ported from CCS's old forum
Original Post ID: 8454 |
|
|
R.J.Hamlett Guest
|
Re: Precision 1 second counter |
Posted: Sun Nov 03, 2002 10:36 am |
|
|
:=Hello,
:=
:=I need a precision 1 second (1Hz) counter in my circuit.
:=Couple of options I can think of are:
:=
:=1. Use a real time clock (like a DS1302) - this is a relatively expensive method.
:=
:=2. Use a watch crystal controlled logic oscillator (connected to a 4060), then divided once more to give 1Hz.
:=- Much cheaper than option 1.
:=
:=Now then, my question to you all is can I do away with any external components (like the above) and simply use the PIC (run from a precision crystal) to do the 1 second count for me ?
:=
:=Im thinking perhaps using the RTCC (timer0).
:=
:=Code examples from anyone who has implemented such a system would be great.
:=
:=Cheers,
:=Darren
Some PICs allow one timer channel to be run off a seperate crystal. If you are using the 16F873, mentioned in another post, this can do this on Timer1. This oscillator then operates at it's own clock rate, and can use a seperate crystal, at a lower frequency, making longer time intervals easy. The question though is, how accurate you need to be?. If you need really high accuracies, then you would actually need more complex hardware (such as a crystal oven), whichever route you use. However off the shelf crystal modules, are available with better than 10ppm tolerance over normal temperature ranges (corresponds to significantly better than 1 second/day). If you drive the processor off one of these, you can use timer2 to count to 256, and interrupt on every 16 counts, with a *16 prescale, and by chosing a 'binary' crystal value (like 4194304Hz), give an interrupt at 16Hz. Then this code:
int sectoggle;
#INT_TIMER2
void tick(void) {
static int ticktime=16;
if (ticktime--==0) {
ticktime=16;
++sectoggle;
}
}
Will give a value 'sectoggle', which will increment every second. This type of approach needs no extra hardware, and gives accuracy comparable (better with a 10ppm oscillator module) than most digital clocks.
Any interrupt driven clock of this type will have very good long term behaviour (depending on the crystal), but will exhibit some tolerance on the individual clock changes (depending on the interrupt latency - this varies according to what your code is doing).
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 8456 |
|
|
darren logan Guest
|
Re: Precision 1 second counter |
Posted: Sun Nov 03, 2002 10:45 am |
|
|
Hello,
Thank you to both Mr.Cunningham and Mr.Hamlett (i assume Mr!),
I need an accuracy of around 2 days in 500 days. So although quite accuracte, not world beating accuracy.
If I can better at low cost that then that's fantastic !.
A loss/gain of 1 second a day is nothing (around 8 minutes in 500 days !!).
How do you think the cheapo 3-pin resonators will perform ?
Regards,
Darren
___________________________
This message was ported from CCS's old forum
Original Post ID: 8458 |
|
|
johnpcunningham Guest
|
Re: Precision 1 second counter |
Posted: Sun Nov 03, 2002 1:20 pm |
|
|
To get exact timing, you should use a crystal/oscillator that is specifically used for timekeep (i.e. 32.768Khz). But if your accuracy is +/- 1sed per day, then you may be able to use a standard crystal (1,2,4,8,10,20 Mhz. Calculations would be something like:
86400 seconds = 1 day
Using 4MHz clock you get a time out of:
[(2^16) * prescale)/(clock mhz/4) =
[(2^16) * 8)/(4000000/4) = .524288 seconds
Therefore:
86400/.524288 = 164794.9218 counts
Theoretically if you let simply let the Timer free run and count each time it times-out (i.e. tolls from 0xFFFF to 0x0000), then you would only be off by one count which would be 0.52488 seconds and within your spec for being less than a second a day. When your count value gets to 164795 then you have counted for a day. You could use another count to keep up with the days, then years, and so forth. You could get moreaccuracy by increasing the speed fo the external clock, reducing the prescale (1,2,4,8) or a combination of both.
If your keeping this time internal, then there is your answer. You could also get up to 16794 counts an adjust the last count to compensate for the error (0.9218).
If you are actually pulsing an output and you want to use a standard crystal/oscillator then you need to get the timeer1 value to time out as close to 0.5000 seconds or at least some multiple of that. That way a pin can go on for 0.5 seconds, there off for 0.5 seconds whcih gives a 1Hz rate. I know there are some some other oscillators in higher frequencies (3.768Mhz) range that are made for accurate time keep as well, but the 32.768KHz is the general standard.
Try digikey.com for a search on clocks, crystals, and resonators:
<a href="http://www.digikey.com/scripts/US/DKSUS.dll?KeywordSearch" TARGET="_blank">http://www.digikey.com/scripts/US/DKSUS.dll?KeywordSearch</a>
As for the resonators, I have not been able to find one that runs a 32.768KHz. Generally they are 100KHz up to 20 MHz. they are not as accurate as crystals or oscillators but may work for you.
Hope this answers your question. Best of luck.
JC
___________________________
This message was ported from CCS's old forum
Original Post ID: 8464 |
|
|
Charlie U Guest
|
Re: Precision 1 second counter |
Posted: Sun Nov 03, 2002 5:57 pm |
|
|
:=Hello,
:=
:=I need a precision 1 second (1Hz) counter in my circuit.
:=Couple of options I can think of are:
:=
:=1. Use a real time clock (like a DS1302) - this is a relatively expensive method.
:=
:=2. Use a watch crystal controlled logic oscillator (connected to a 4060), then divided once more to give 1Hz.
:=- Much cheaper than option 1.
:=
:=Now then, my question to you all is can I do away with any external components (like the above) and simply use the PIC (run from a precision crystal) to do the 1 second count for me ?
:=
:=Im thinking perhaps using the RTCC (timer0).
:=
:=Code examples from anyone who has implemented such a system would be great.
:=
:=Cheers,
:=Darren
If your application could tolerate a 2 second clock, you could use the Timer1 oscillator with a 32khz crystal and just let it free run. Then enable the Timer1 interrupt and count the times you are interrupted. Since you are not resetting the timer each time, then the interrupt latency is not a factor. Refer to the 16F87x data sheet and review the section for Timer 1. According to this section, you may be able to achieve accuracies on the order of +/- 10ppm.
___________________________
This message was ported from CCS's old forum
Original Post ID: 8469 |
|
|
R.J.Hamlett Guest
|
Re: Precision 1 second counter |
Posted: Mon Nov 04, 2002 3:48 am |
|
|
:=Hello,
:=
:=Thank you to both Mr.Cunningham and Mr.Hamlett (i assume Mr!),
:=
:=I need an accuracy of around 2 days in 500 days. So although quite accuracte, not world beating accuracy.
:=If I can better at low cost that then that's fantastic !.
:=
:=A loss/gain of 1 second a day is nothing (around 8 minutes in 500 days !!).
:=
:=How do you think the cheapo 3-pin resonators will perform ?
:=
:=Regards,
Simple answer, look at the data sheets...
Generally resonators are less accurate than crystals, at low frequencies especially (I don't know of any that go this low). What is your temperature range?. Typical HF resonators are +/- 5ppm/C, and this is 'on top' of their basic accuracy error. You will probably end up spending more for a resonator that has the required accuracy at such a low frequency, than for a crystal. Remember that with the crystal, if you use an external crystal, you will have to consider the need to 'trim' it, to get good accuracy (otherwise the tolerance of the capacitors, and of the PCB tracks, will take you out of spec). A assembled module oscillator removes this problem. Typical resonators (the cheap ones), have an initial tolerance of about 0.5\%, and a temperature tolerance of about 0.3\% over normal operating temperatures. This is more than you can accept. At the end of the day, you have to balance the costs of parts, against the required error, and any initial setup.
Go to one of the manufacturers sites - like: <a href="http://www.vanlong.com/index.htm" TARGET="_blank">http://www.vanlong.com/index.htm</a>
And you can then browse the various products, and work out the spec required. You need a total error less than 0.4\% over the required temperature range, and allowing for device aging. A 100PPM crystal, with typical temperature drift, and aging, will comfortably get inside this, while a ceramic resonator generally won't.
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 8474 |
|
|
Sherpa Doug Guest
|
Re: Precision 1 second counter |
Posted: Mon Nov 04, 2002 8:15 am |
|
|
:=To get exact timing, you should use a crystal/oscillator that is specifically used for timekeep (i.e. 32.768Khz). But if your accuracy is +/- 1sed per day, then you may be able to use a standard crystal (1,2,4,8,10,20 Mhz. Calculations would be something like:
Your requirement of 1sec/day is 11ppm, so any standard 5ppm crystal should do fine. There are a lot of apparently oddball frequencies that actually work out very well for time keeping. I am currently using 8.192MHz to generate even 1kHz multiples.
There is nothing magic about 32.768kHz crystals except that they are a power of two and they are used by the billions in very low power clocks. If your uP is going to be running all the time then it will draw more power than the oscillator so the low power you get by using such a low frequency is not significant. On the other hand 32kHz crystals can be fragile and finicky. If you build such an oscillator test it well over temperature, voltage, and shock limits.
:=Theoretically if you let simply let the Timer free run and count each time it times-out (i.e. tolls from 0xFFFF to 0x0000), then you would only be off by one count which would be 0.52488 seconds and within your spec for being less than a second a day.
If you keep the Timer free running that uncertainty of 0.52488 seconds does not accumulate, so you get 0.52488 seconds for 1 day, and also 0.52488 seconds for 1 year! Making sure the error does not accumulate is as important as making sure the error is small to start with.
:=As for the resonators, I have not been able to find one that runs a 32.768KHz. Generally they are 100KHz up to 20 MHz. they are not as accurate as crystals or oscillators but may work for you.
Ceramic resonators don't usually come anywhere near the 11ppm spec you need.
___________________________
This message was ported from CCS's old forum
Original Post ID: 8483 |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|