PDA

View Full Version : DT_Elapsed timer constant for 40 MHz?



circuitpro
- 10th July 2010, 02:39
I'm running with the version of DT_INTS found on your website, and ASSuming those are the latest.

http://darreltaylor.com/DT_INTS-18/home.html

One small problem, though. I'm running this PIC at 40 MHz, and would like to use your Elapsed timer interrupts. Do you have a TimerConst that I can use at this frequency?

Len

Kamikaze47
- 11th July 2010, 08:10
40mhz is too fast to get a 100hz interrupt without a pre-scaler.

So what you will have to do is add this to the frequency part of Elapsed_INT-18.bas


If OSC == 40
TimerConst = 0CF2Dh
EndIF

And then add this line somewhere near the start of your program to set a 1:8 prescaler on TMR1.


T1CON.4=1
T1CON.5=1

*note* untested

*edit* changed values to make it more accurate

circuitpro
- 11th July 2010, 17:28
Thanks for that. Inside the Elapsed_INT-18.bas file, was reference to a link that was suppose to show you how to calculate different TimerConsts for different crystal frequencies, but it is now dead. I will test this tomorrow.

Can I add the prescaler to the Elapsed_INT-18.bas like this, or does it have to be external?


If OSC == 40
T1CON.4=1 ; Set a 1:8 prescaler
T1CON.5=1 ; on TMR1
TimerConst=0CF2Dh
EndIF

circuitpro
- 11th July 2010, 20:09
doh! (nevermind)

Darrel Taylor
- 12th July 2010, 01:06
Kamikaze47's numbers are theoretically correct, and it will appear to work.

However, this is the reason I never added OSC frequencies above 20mhz to the Elapsed timer ...
Anything over 24mhz would require a prescaler to get 100hz interrupts.
And every time you reload the Timer, the Prescaler gets cleared.

With a 1:8 prescaler, it could lose up to 7 counts on every interrupt.
At 100 hz, it could lose up to 700 counts every second. That's 70uS with 40mhz OSC.
Not good for a clock, especially in the long term.

I was thinking about it yesterday and realized that I could make a software "postscaler" in the ISR and run the interrupts at higher frequencies for the chips running >24mhz.

So that's what I did.
The attached version of Elapsed_INT-18.bas (ver 1.2) should work with any OSC.

The TimerConst is now calculated automatically instead of using "constants".
The Timer is always 1:1, and at 40mhz will run at 200hz.
Then the handler counts off 2 groups of 100 ticks (1:2 postscaler).
@ 64mhz the timer is 400hz, with a postscaler of 1:4.

You can use the INT_Handler as an ASM "type" interrupt to save overhead.

I've only had it running a few hours @40mhz, but it seems to be keeping time really well. (better than my PC).
We'll see what it's like after a few days.

hth,