PDA

View Full Version : TMR1 100Hz Exactly Interrupt Routine



manumenzella
- 14th February 2007, 03:15
Hi.
Im working on a datalogger project that needs to take a reading every exactly 1 second. I want to make TMR1 interrupt every 10 ms, so that every 100 TMR1 interrupts, I get exaclty one second. This is the routine I got to write. I have not tested yet in a PIC, because I run out of them after burning to of them because of wrong polarity :(. LOL. By the way, I am using 16F877A. This is the code.

/////////////////////////////////////////////////

INT:
@ BCF T1CON,0 'STOP TIMER1
@ MOVLW 0xC3 'LOAD WITH 65527 BECAUSE
@ MOVWF TMR1H '50000 (20MHz / 4 / 100) - 2 CYCLES
'TO ENTER INT
@ MOVLW 0x48 '- 6 CYCLES TO LOAD THE REGISTERS, AND
@ MOVLW TMR1L 'TURN IT ON AGAIN
@ BSF T1CON,0 'TURN TIMER1 ON AGAIN

X = X + 1 'INCREMENT REG
PIR1.0 = 0 'CLEAR INT FLAG
CONTEXT RESTORE 'RESTORE REGs AND RETFIE

/////////////////////////////////////////////////

Can you please tell me if its OK??? Thanks for your help, and sorry for my english. Manuel

manumenzella
- 14th February 2007, 03:20
Hey its me again. I have just realized that in the number to load to TMR regs, i have to take into account the cycles needed to save all the regs. But anyway, I just want to know what you guys think about the general of the routine. Would it work??? Would it be accurate??? Thanks again.
Manuel

paul borgmeier
- 14th February 2007, 07:03
Why 10 mS, 100 Counts? You are thinking like a person and not a computer. Computers like powers of 2s not 10s.

Why not use a 4.194304MHz Xtal (DigiKey has them in stock for $0.70 ea), let Timer 1 run free, set the prescaler to 8, and count TMR1 overflows. 2 Overflows = 1.000 sec. This gives you lots of time to do other stuff in between interrupts.
(math = 4/4.194304*65536 * 8 * 2 = 1000000 uS = 1 S)

Oh yeah, you also do not have to deal with accounting for the timing for saving registers etc because the timer is free running.

manumenzella
- 14th February 2007, 17:44
Hi Paul
Thanks for your help!!!!!!!!!
Youre right, im quite new to programming and still not used to power of 2!! :)
Anyway, which crystal do you recommend to use to run close to 20MHz???
Thanks a million
Manuel

Acetronics2
- 14th February 2007, 18:00
100 Hz ...

might have very precise 100 Hz timebase from mains ... just a trafo and two rectifiers !!!

just an idea ...

Alain

Bruce
- 14th February 2007, 18:23
Have a look here http://www.pbpgroup.com

Darrel has some excellent PBP examples.

manumenzella
- 15th February 2007, 01:23
Hi. Thanks for all the help.
Is the line frequency accurate enough to make a clock??? Hope it is!!!!! Great idea anyway. Thanks.
Manuel

ronsimpson
- 15th February 2007, 03:13
DigiKey has a 19.6608mhz crystal. If I did the math right...

math = 19,660,800/4/65536 =75 cycles
Your interrupt will happen 75 times/second. In software count to 75 and then add 1 to the second number.

or

last time I used a 20,000,000 crystal
20,000,000/4/65536=76.29395hz In software dividing by 76 is not good but close.
The interrupt happens 76.29395 times/second.
I used a 16bit variable and on every interrupt added 859 to the number. The variable overflows once per second. I think the math is off by .001%

manumenzella
- 15th February 2007, 04:07
hi ronsimpson!!! thanks for your help. i have two questions:
to calculate the interrupts with TMR1, I thought you had to divide the value by 66535 (0xFFFF). But, as you say, its 66536 because 0x0000 counts, right?
And one last thing. Suppose that a WORD variable is loaded with 60,000
Now we add to that variable, say 1000. Which is going to be the next result??? Is it going to be 0000 because it overflowed, or is it going to be 4535??? (60,000 + 1,000 - 65,535)???
Thanks for your help. Really quick answers you get in this forum!!!!!!!
Manuel

manumenzella
- 15th February 2007, 04:10
Sorry in my last post, it should have been:
The variable should have been loaded with 65.000!! NOT 60.000
And when we add a thousand, do we get just 0x0000 or 465 (65000 + 1000 - 65535)??? Thanks
Manuel

paul borgmeier
- 15th February 2007, 04:56
1st question divide by 0x010000
2nd question = 464

Do you really need 1 second intervals and how exact do you need to be?

manumenzella
- 15th February 2007, 05:06
Thanks paul. I am planning to build a temperature datalogger, that will collect data every two minutes for a month or two, so I need to get it as accurate as possible. Also, in my country, Argentina, there are not RTC availables....

Now following Ronsimpson way, (the second one, with the 20.000 MHz), the variable to which we add 859 on every interrupt, I should never clear it, right??? Or should I clear it every time the variable overflows???

Sorry for my english, and thanks again.
Manuel

paul borgmeier
- 15th February 2007, 05:14
>>collect data every two minutes for a month
Check out
http://www.picbasic.co.uk/forum/showthread.php?t=2129
It would be easy to add in the data logger every two minutes.

With Ron's approach, do not stop the timer, just add fast after the interrupt.

Why 20MHz for a datalogger?

manumenzella
- 15th February 2007, 05:27
Hey
Thanks so much for the links. There great because the dont have interrepts :) and they are not too complicated!!!
Yes youre right 20MHz is WAY too much for this project. I could use only 32KHz and bring down the current draw.
I am going to use an LM34 or LM35 to measure temperature, and I was thinking of shutting it down while im waiting to save power. How long before making the actual measurement do you think I should turn it on so that the sensor has stabilized???
Thanks
Manuel

paul borgmeier
- 15th February 2007, 05:47
>>I was thinking of shutting it down while im waiting to save power

I am guessing you are referring to the LM34/35 and not the Pic? If so, I am sure it is in the datasheet. If you are planning to run off of batteries, then the lower XTAL speed would be ideal for the PIC

A free runing TMR1 will overflow every 8 seconds (prescaler = 1) with a 32768 XTAL. You could count 15 overflows for 2 minutes.

Or you cold poll TMR0, also free running (prescaler = 256) with a 32768 XTAL. You could count 15 overflows for 2 minutes. The link I gave you has an example of polling TMRO.

Too many options - also check the LM34/35 datasheet and see what it needs in the way of communication speeds, etc. I have not looked for some time and do not recall the method.

There have been a zillion questions in this forum about the LM34/35 over the years - search and you shall be rewarded.

manumenzella
- 15th February 2007, 05:51
Hi. Thanks for your help.
Yes, as you said, it would be ideal the low frequency crystal, and the timing is also much simpler :) LOL.
The LM35 outputs a linear 10 mV/`C and the LM34 is 10 mV/`F
Anyway, that wouldnt be a problem with speed, but will I be able to interface an I2C EEPROM like 24LM32 with that speed??? I guess yes because its syhncronus transmission, but i ask to be sure :)
Thanks a lot
Manuel