PDA

View Full Version : Laptimer



brodin
- 9th November 2003, 22:10
I am building a laptimer. So far i have tried using the TMR0, with not perfect results.

PIC i am using is 16F876A.

My timer is doing the interupt every 8.192 ms.

Isn't it possible to set it up better? I want to have a interupt every 10ms exactly. Or anything that is devideble with 10. Is that possible. What configuration should i use then?


I have also looked at a I2C RTC: PCF8583
This device can manage 10ms.


What would you recomend?

mikep
- 10th November 2003, 01:04
I just did almost the same thing. Accuracy is not that important here since it's being used for R/C cars, however it is pretty accurate.

You can see the interrupt code at:

http://www2.rconline.ca/horde/chora/

Click on start-lights and look at main.bas.

I'm displaying MM:SS.SS.
So 1/100th of a second.


Mike

brodin
- 10th November 2003, 11:09
ok, i will check that. But my project has to be EXTREMLY accurate. It have to work for hours with the correct hundreds of asecond.


I got another answer from another site:



You want exactly 10msec and it's set for 8.192msec right now? Replace the crystal you're using right now to 81.92% of the value. For example, if it is 4.000Mhz right now, then replace it with a 3.2768Mhz crystal.


Okay! That sounds good and easy! I am using a LCD and serial communication, but i guess that will work fine if i define the new crystal in the code, right?
I found a program on the net today, it's a TMR0 calculator. Watch this image: http://w1.278.telia.com/~u27812750/Files/tmrsetting.GIF

According to this, it would be possible to change the TMR0 Offset and get exactly 2 ms. If this is possible, how do i do that then?

mikep
- 10th November 2003, 13:08
TIMR0 can use an independant external oscillator I think.
I haven't done it yet, but take a look at the datasheet. I saw something about it in the registers.

I was thinking of using 32.768 Khz watch crystal to be able to keep accurate time.

I might try it on a breadboard today if I can find the time and will let you know how it comes out.

There's also the possibility of using a RTC with 1/100th of seconds.

mikep
- 10th November 2003, 21:59
Well for the fun of it, I tried using the external oscillator with TIMR0 using a 32.768Khz crystal.

The oscillator is basically a watch crystal, a couple of caps, some resistors and a hex inverter IC.

Leaving the pre-scaler at 1:1 though you end up with 128 interrupts (ticks) per second.

32,768/256=128 so that doesn't really help with your 1/100th of a second. But the timing is bang on! I suppose you could do some fancy math here to increment TIMR0 every so often to get it to 100 but not worth it.

So let's say you move up to a 3.2768Mhz crystal.
In that case every 128 ticks will be 1/100th of a second. And that would be perfect for your app.

3,276,800/256=12,800

increments a 1/100th of a second var every 128 interrupts.

So you need a ticks var... and in the ISR when ticks=128 you increment your centisecond var (1/100th of a second var).

When the centisecond var gets to 100 increment your seconds var and so on.

The other cool part about it is the PIC is still running at 20Mhz.


Cheers,
Mike

mikep
- 10th November 2003, 23:39
After letting it run for a while it's not as dead on as I thought.

Now it's running on a breadboard and crystals are not too happy when they are not well isolated.

I wonder if it's that or something else that's happening.

Here's the code in case anyone ever needs it.

-----------------------------------------------------------------------------


' Version 1.0.0
' Timer.bas
' Test for using an external 32.768Khz oscillator with TIMR0
' by Michael J. Pawlowsky
' Nov 10, 2003

DEFINE __16F628

Include "modedefs.bas"

' Define Oscillator Speed
DEFINE OSC 20

' Set transmit register to transmitter enabled
DEFINE HSER_TXSTA 20h
' Set baud rate
DEFINE HSER_BAUD 9600


' Set the port directions
' 0=output 1=input
TRISA=%00010000 ' Set PORTA A.4 input for oscillator
TRISB=%00000000 ' Set PortB

wsave var byte $20 SYSTEM '$20 Save location for the W register if in bank0
wsave1 var byte $A0 SYSTEM ' Save location for the W register if in bank1
wsave2 var byte $120 SYSTEM ' Save location for the W register if in bank2
ssave var byte Bank0 SYSTEM ' Save location for the STATUS register
psave var byte Bank0 SYSTEM ' Save location for the PCLATH register

INTF var INTCON.1 ' External Interrupt pn RB0/INT
T0IE var INTCON.5 ' TMR0 Overflow Interrupt Enable
T0IF var INTCON.2 ' TMR0 Overflow Interrupt Flag
GIE var INTCON.7 ' Global Interrupt Enable
PS0 var OPTION_REG.0 ' Prescaler division bit-0
PS1 var OPTION_REG.1 ' Prescaler division bit-0
PS2 var OPTION_REG.2 ' Prescaler division bit-0
PSA var OPTION_REG.3 ' Prescaler Assignment (1= assigned to WDT)
T0CS var OPTION_REG.5 ' Timer0 Clock Source Selec
T0SE var OPTION_REG.4 '

hour var byte ' Holds the hours value (0-23)
minute var byte ' Holds the minutes value (0-59)
second var byte ' Holds the seconds value (0-59)
ticks var byte ' Holds the pieces of 1/100th of seconds value (0-59)
flag var bit ' Flag

GOTO Init ' Jump over interrupt routine

' Define interrupt handler
define INTHAND myint

' Assembly language interrupt handler
asm
; Save W, STATUS and PCLATH registers
myint movwf wsave
swapf STATUS, W
clrf STATUS
movwf ssave
movf PCLATH, W
movwf psave


btfss INTCON, T0IF ; Look to see if we have a TIMR0 overflow
goto EndInt ; If not goto EndInt

incf _Ticks ; Increment the TICKS variable
cjbe _Ticks,#127,Clock_Exit ; Have we reached a second yet?

; One second has elasped so update the time variables
clrf _Ticks ; Reset TICKS
incf _Second ; and increment Second
bsf _flag ; set the flag so we update the time

Second cjb _Second,#60,Minute ; Have we reached a minute yet?
clrf _Second ; YES! so reset Seconds
incf _Minute ; and increment Minutes

Minute cjb _Minute,#100,Clock_Exit ; Have we reached an 100 minutes yet?
clrf _Minute ; YES! so reset MINUTES

Clock_Exit bcf INTCON,T0IF ; Clear the TMR0 overflow flag
goto EndInt


EndInt
; Restore PCLATH, STATUS and W registers
movf psave, W
movwf PCLATH
swapf ssave, W
movwf STATUS
swapf wsave, F
swapf wsave, W
retfie

endasm


Init:
Minute=0
Second=0
cSecond=0
Ticks=0

GIE=0 ' Turn off global interrupts
While GIE=1:GIE=0:Wend ' Make sure they are off
PSA=1 ' Assign the prescaler to WDT at 1:1
PS0=0 ' Set the prescaler
PS1=0 ' to increment TMR0
PS2=0 ' every 1 instruction cycle
T0CS=1 ' Assign TMR0 clock to to transition on RA4
T0SE=0 ' Inc on low to high
TMR0=0 ' Clear TMR0 initially
T0IE=1 ' Enable TMR0 overflow interrupt
GIE=1 ' Enable global interrupts


Main:
IF flag = 1 THEN
HSEROUT [0, dec2 Minute, ":", dec2 Second, 13]
Flag = 0
ENDIF
GOTO Main


----------------------------


I'll attach the oscillator circuit as well.

I use a SN74HC04 since I didn't have a 4069.

Let me know if you are able to get it accurate over several hours and how you did it. So far I've been using RTCs for my apps that require accurate timing but I would like it if I could save some pins at times.


Regards,
Mike

brodin
- 12th November 2003, 22:31
Okej, but lets say i change the to a 3.2768Mhz crystal. What happends with my serial communication and LCD? Guess the timing will go to hell with them, or?

I don't think 3.2768Mhz is supported by Picbasic Plus...

mikep
- 12th November 2003, 22:51
"Picbasic Plus..." - I don't have that version.. I have PicBasic Pro. :-)

I've never seen a:

DEFINE OSC 3.2768

So I will tend to think that it is not supported as well.

But that's where having the external oscillator comes in on A.4

But at that point, personally I would probably go to a RTC if you absolutely need it to be so accurate.

If you did not need the accuracy, what I would do is simply keep comparing it with a known accurate source. And then say something like every time seconds gets to 40 or whatever it will take, add one tick to get it accurate.

brodin
- 13th November 2003, 12:34
What RTC do you use? How big is it, 8 pin?

Is there anything else needed then the RTC device? I mean, do the RTC need any external compnents to work?

How much does it cost?

mikep
- 13th November 2003, 14:06
The hard part is finding one that does 1/100th of a second. I have not used one yet. 1 second accuracy was more than adequate for the projects I have done needing RTCs. I have seen the ones up to 1/100th of a second as 14 pin SOIC, 28 pin SDIP, 44 pin etc.

Just do a search on the net or on different manufacturers sites for "RTC 1/100" or "RTC HH:MM:SS:hh".

The cheaper RTCs need a crystal and some caps.
Then there are some that only need a crystal.
And others that have them all in the IC.

Epson use to make some, not sure if the still do or not, Maxim has some and surely there are others.

Cost.. for one with 1 sec. accuracy is less than a couple of dollars, for 1/100 accuracy between 5$-25$.

brodin
- 13th November 2003, 14:33
http://www.elfa.se/elfa/produkter/en/2013468.htm

PCF8583P
73-855-37


This is a 8 pin I2C RTC which handels 1/100 seconds. Rather cheep to. It looks though like you need external crystals.

mikep
- 13th November 2003, 15:32
Looks good. I found the datasheet at:

http://www.semiconductors.philips.com/acrobat/datasheets/PCF8583_5.pdf

Also looks like you will need a decent scope to be able to evaluate the cap needed to keep it accurate. Or simply play with different caps over longer period of time to see how well it keeps time. The datasheets say +- 5 minutes over 1 year should be achieved.

I could not find somewhere to get it in N.A. easily though. I guess you are in the E.U.?

brodin
- 13th November 2003, 18:12
Yes, i am in Sweden and have bought stuff from that firm a lot of times...

I have a scope, so i'll be able to check that. I think i'll buy one RTS to laborate with...

mikep
- 13th November 2003, 19:18
I wouldn't mind playing with one as well. If I PayPal you the funds + a couple of dollars for shipping, would you pick me one up as well and just put in in an envelope for me. I'm in Canada.

Mike

brodin
- 14th November 2003, 21:22
Guess i could!

I'll check what the shipping will be at the postoffice, guess it's only like 2 dollars. I'll order it next week. I will tell you when i got them!

mikep
- 14th November 2003, 21:56
That's great thanks.

I've been looking at different RTC with 1/100 resolution and the only one they have a DigiKey is a 44pin surface mount at 35$ or so. Not exactly an optimum part for the kind of stuff I usually play with.

Tim B
- 16th November 2003, 11:57
brodin

I answered your request on another site

http://www.picbasic.org/forum/showthread.php?s=&postid=7441#post7441

But for the benifit of others here it is. PBp users will have to do some conversions but it would not be hard

Tim

brodin
- 23rd November 2003, 22:29
mikep: Do you still want a RTC? If will order from ELFA tomorrow, so if you want one i can order one for you!

mikep
- 23rd November 2003, 23:25
Yes please....

Just let me know how much to send you and I can either pay pal it or mail you a money order.

Thanks,
Mike

You can e-mail me directly at mikejp[at]videotron.ca