![]() |
![]() |
![]() |
|
|
|||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
|
|
Thread Tools | Display Modes |
|
|
(permalink) |
|
What's the most accurate way to get the PIC to keep time well for weeks together. I mean with +/- 1 min accuracy per month for example. Obviously using an external crystal would be a good start, but any other pointers?
And what about the software? I'm currently getting it to count 100ms per interrupt and then keeping the time from there, currently using the TMR1 of the PIC18F4520 (clocked using internal OSC at 8MHz, which I know isnt the most accurate!). |
|
|
|
|
|
|
(permalink) |
|
Use a 32768 Hz watch crystal on the TMR1 osc pins. These little crystals are very accurate.
__________________
Bill Home of the Firefly PIC Tutor Inchworm ICD2 http://www.blueroomelectronics.com |
|
|
|
|
|
|
(permalink) |
|
Another approach would be to use the AC mains as your frequency reference.
The power companies make the average frequency of their mains very accurate. If you want to run it from a battery during mains failures, you could use a crystal as the freq standard. In other words, the PIC would monitor the mains supply, and if it fails, switch to the crystal for the duration. If the mains failure is only an hour or three, the gain or loss of time would be minimal.
__________________
Len |
|
|
|
|
|
|
(permalink) |
|
Right, now comes the more interesting question: How can you make sure that whatever else you PIC is doing, it still stops it, counts up a second or 100ms or whatever the increment is without ever missing one. I know I can use a timer interrupt, but are there any pointers with respect to the coding in the main()? Obviously the timer isr() shouldn't take longer than the timer period to execute, but what other parts of a program could possibly affect the timer accuracy?
|
|
|
|
|
|
|
(permalink) |
|
Here's a reprint from a Forum.Microchip post that I hope may prove useful.
Happy Holidays. Mike, K8LH Software version of Xtal Oscillator Trimmer Capacitor A 20-MHz crystal oscillator doesn't necessarily oscillate at exactly 20-MHz which can be a 'bummer' in RTC applications (grin). Normally I would use a ceramic trimmer capacitor to 'tune' the oscillator circuit but last year I tried a "software" trimmer capacitor in a couple projects. So far it's proven very reliable and accurate. The "software" trimmer code consists of 8 instruction words in two locations within the ISR; Code:
;
; ISR_Trim routine is used to adjust the RTC 1-second period to
; within plus or minus 200-nsecs to make up for a crystal which
; may be slightly off frequency. The routine adds or subtracts
; one 200-nsec count (1 Tcyc) from Timer 2 for the first 'CCTR'
; number of 1-msec interrupts each second. Theoretical accuracy
; to within 6.3 secs/year, not including temperature drift and
; crystal aging.
;
; variables: CCNT, correction count from EEPROM [00 to FF]
; CVAL, correction value from EEPROM [FF or 01]
; CCTR, correction counter reloaded from 'CCNT'
; variable each 1-second period
;
; range: ±255 200-nsec counts/second (±51.0 usecs/sec)
;
ISR_Trim
movf CCTR,W ; correction counter 0? |B0
bz ISR_Sw_Input ; yes, branch, else |B0
decf CCTR,f ; decrement counter |B0
movf CVAL,W ; get correction value FF or 01 |B0
addwf TMR2,f ; apply 1 Tcyc timer correction |B0
;
Code:
;
; the Real Time Clock 1-second 'heartbeat' timer/counter code
; counts 1,000 1-msec interrupts before performing all of the
; once-per-second procedures and functions
;
ISR_RTC
decfsz RTCL,f ; RTC counter lo = 0? |B0
goto ISR_Calibrate ; no, branch, else |B0
movlw d'250' ; |B0
movwf RTCL ; reset RTCL for 250-msecs |B0
decfsz RTCH,f ; all four 250-msec periods? |B0
goto ISR_Calibrate ; no, branch, else |B0
bsf RTCH,2 ; reset RTCH for 4 (x 250) |B0
;
; reload timer Trim correction counter
;
movf CCNT,W ; reload correction counter var |B0
movwf CCTR ; |B0
;
Adjusting the CVAL 'correction value' and CCNT 'correction count' values and saving them to EEPROM is entirely up to you. In one of my projects I use a PCB jumper to display the values, a rotary encoder to edit them, and then remove the PCB jumper to save the edited values back to EEPROM. Have fun. Kind regards, Mike |
|
|
|
|
|
|
(permalink) |
|
typical watch crystals are 20 ppm accurate. 1 min/month is 23 ppm so you should be good with that. You should definitely us interrupts.
|
|
|
|
|
|
|
(permalink) |
|
A generic micro controller crystal is usually good till 50ppm. If you have a refrence signal of any kind (AC mains) calibrating it over the longer term is very easy. Just use something like a basic zero cross detect on the AC mains line to synchronize a real time clock. Mind you in the U.S. at least there is a 10mhz signal that is recievable in almost every location in the U.S. that can give you sub micro second accuracy using very basic receivers. It comes down to that anyways, comparing your local clock with some sort of standard.
__________________
"Because I be what I be. I would tell you what you want to know if I
could, mum, but I be a cat, and no cat anywhere ever gave anyone a straight answer, har har." |
|
|
|
|
|
|
(permalink) |
|
If you want to get fancy a GPS would give you very accurate time.
__________________
Bill Home of the Firefly PIC Tutor Inchworm ICD2 http://www.blueroomelectronics.com |
|
|
|
|
|
|
(permalink) |
|
You could use timer2 instead of timer1, it has a pre-scaler, post-scaler and a PR reg which makes it flexible for configuring time intervals to trigger interrupts with no need to re-load the counter after each interrupt.
If you have enabled interrupt priorities for using several interrupts, the clock timer interrupt must be set to high priority. As for time accuracy, I also tried using the internal clock for a very basic timer and found it to be about 1-2% inaccurate. The factory calibration is supposed to be to within 1% but it also varies with temperature and power supply. There is an OSCTUNE reg to allow adjustment but it is not fine enough for your needs. A crystal oscillator can give reasonable results, but choose a good quality crystal - cheaper ones are only accurate to 50ppm or worse, better ones are usually about 20ppm or 10ppm. You will still need some means of fine adjustment and the accuracy is affected by temperature and crystal ageing. For better accuracy, Dallas/Maxim do a real-time clock chip with built-in temperature compensation but it is more expensive. Or you could sync with radio time signals, MSF (UK) or DCF77 in Europe. GPS is also very good if you get one with an accurate 1pps output, but you also need to consider the antenna placement. |
|
|
|
|
|
|
(permalink) |
|
I would suggest using an external battery backed timer chip - my I2C tutorial shows how to use a Philips one, but there are many different types available.
|
|
|
|
|
|
|
(permalink) | |
|
Quote:
i feel once crystal is in place, we can safely forget mains as a frequency source. afterall 32.768KHz ones are not costly neither 3.579545MHz. Only issue is temp maintenance. towards this end, the tinycrystal can be safely pushed into a thermocole or glassfiber stuff and insulated(isolated from atmosphere.(if space permits) and the associated caps could be temp quotient ones as philips(now NXP) was supplying these for RF circuits.
__________________
Regards, Sarma. Last edited by mvs sarma; 10th December 2006 at 05:12 PM. |
||
|
|
|
|
|
(permalink) | |
|
Quote:
|
||
|
|
|
|
|
(permalink) |
|
Right, for a zero cross detector - any particular ICs in mind? And what about the associated circuitry?
I'm just sizing up my options. Currently using the internal OSC and after 3 hours of fine tuning, I've got it to <6 mins accuracy per week.
__________________
www.myspace.com/crimsonroadmap |
|
|
|
|
|
|
(permalink) | |
|
Quote:
From memory, I believe that some PICs have a Schmitt on some i/o so you may not need an external Schmitt.
__________________
Len |
||
|
|
|
|
|
(permalink) | |
|
Quote:
However, I would prefer to do it after the bridge rectifier - simply place a single diode between the output of the bridge and the electrolytic capacitor. You can then take a feed from the output of the bridge, through a resistor to the base of an NPN transistor - it's collector (pulled high via a resistor to 5V) will have a lovely zero-crossing pulse on it!. |
||
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Latest |
| Standalone Pic Programmer Using ICSP or RS232 | Neiwiertz | Micro Controllers | 11 | 27th May 2006 01:22 AM |