Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

TMR1 Overflowing Time

Status
Not open for further replies.

Suraj143

Active Member
I have loaded TMR1H with b’11000000’ & TMR1L with b’00000000’.
My XTAL is 32.768Hhz & TMR1 Pre scaller 1:1

From how many cycles will it be give an interrupt by overflowing it?

Help me to calculate this.

TMR1H = 11000000
TMR1L = 00000000
 
Suraj143 said:
Hi how did you calculate that?
With a calculator! :D

But seriously, I took Fosc = 32768/second and divided it by 4. That gives me 8192 ($2000) cycles per second that a 1:1 timer increments.

You set your timer to $c000. TMR1H = 11000000 = $c0. TMR1L = 00000000 = $00. Put together as a 16-bit value they're $c000.

Divide $c000 by $2000 to get seconds. That equals 6 seconds.
 
Last edited:
Hi futz I missed one thing. The Xtal 32.768 KHhz is the Timer1 input clock.Not the system clock.
For the system clock I'm using 4MHz.
 
Suraj143 said:
Hi futz I missed one thing. The Xtal 32.768 KHhz is the Timer1 input clock.Not the system clock.
For the system clock I'm using 4MHz.
That's different then.

Then you get an interrupt every 1.5 second. I think you might want to set your timer to $8000 instead of $c000.

So, you're building a clock huh? I'm gonna do that just for fun one of these days.
 
Last edited:
futz said:
Then you get an interrupt every 1.5 second. I think you might want to set your timer to $8000 instead of $c000.

futz this is the part i cannot understand.How did you get 1.5 seconds?
 
Suraj143 said:
futz this is the part i cannot understand.How did you get 1.5 seconds?
Your timer gets 32768 clocks per second from that crystal. 32768 = $8000

Your timer is set to roll over at $c000 (49152).

$c000/$8000 = 1.5 seconds
49152/32768 = 1.5 seconds

You wanna set your timer to $8000 to get 1 second.

Try it at $c000. All that will happen is that your clock will run real slow.
 
Your calculation is slightly wrond due to the fact that Timer1 is incremented. So the delay will be half a second.

In the 16F88 data sheet, in the Timer1 section, it has example code for implementing a RTC.

Mike.
 
Hi Mike exactly its giving an interrupt in every half a second thats what I need to blink the two dots in the clock.

Thanks
 
If you leave timer1 free running and you check bit 6 of TMR1H, you will find that it alternates every half second. Bit 7 alternates every second - also useful.

Mike.
 
Pommie said:
If you leave timer1 free running and you check bit 6 of TMR1H, you will find that it alternates every half second. Bit 7 alternates every second - also useful.

Mike.

You mean loading 8000h to the TMR1.What a method by checking bit6 of TMR1H.

For sure I'm going to try this one.So I don't want to calculate 0.5 seconds interrupt.

I can setup TMR1 to 8000h & check bit 6 of TMR1H.

Thanks for that.
 
To load 8000h into your timer registers you can just do bsf TMR1H,7. This will eliminate any interrupt latency.

Mike.
 
Pommie said:
Your calculation is slightly wrond due to the fact that Timer1 is incremented. So the delay will be half a second.

In the 16F88 data sheet, in the Timer1 section, it has example code for implementing a RTC.

Mike.
I don't get it? Thought I understood that thing.

I'm looking at the 16f88 datasheet now and it says 1 second for Timer1 loaded with $8000. It clearly says that it takes 2 seconds for the $ffff timer to roll over.

Since the register pair is 16 bits wide, counting up to
overflow the register directly from a 32.768 kHz clock
would take 2 seconds. To force the overflow at the
required one-second intervals, it is necessary to preload
it;
I don't understand how you get 1/2 second. Please enlighten me. :)

EDIT: OHHHHH!!! It just came to me (slaps forehead)! I'm counting the wrong way again. I'm forever doing that. You are correct. At $c000 it would roll over at 1/2 second intervals.

At least I wasn't wrong about the $8000 preset thing. Whew!
 
Last edited:
This appears to be turning into an apples and oranges debate. Apples = Do you want Timer1 to create an interrupt every 1 second? Oranges = Do you want to leave Timer1 free running and check bits 6 or 7 for time changes?

Pommie is correct about preloading the timer if you want to achieve a 1 second delay before interrupt. Just set bit 7 of TMR1H (Timers were cleared when off, or set right after interrupt while running is fine), turn it on, and wait for rollover. You can also consider the Special Event Trigger of the CCP module to create the interrupt on match.

As Pommie also said, as a free-running timer check bit 7 of TMR1H for alternating seconds.... 1 second ON, 1 second OFF, and so on. Check bit 6 for 1/2 second ON, 1/2 second OFF. Thus, Bit 6 will blink ON each second. But it also "blinks" OFF each second.

For what it's worth, the semantics get in the way.
 
I have another question (this is a seperate circuit)

Hi I have 12F675 I need to toggle a LED for every 0.5 second.
I can do a good coding using TMR0 by using a 4 MHz crystal.

But if I give input to the TMR1 using 32.768 KHz (after removing 4Mhz) crystal there is no place to put a 4 MHz crystal for the system clock.

Any idea to use 32.768 KHz as a system clock as well TMR1 input clock.
 
Suraj,

Why don't you use the internal 4MHz oscillator to run your code and the 32K crystal to run timer 1. That way you have accurate timing and fast code.

Mike.
 
Pommie said:
Suraj,

Why don't you use the internal 4MHz oscillator to run your code and the 32K crystal to run timer 1. That way you have accurate timing and fast code.

Mike.

Oh I see earlier I thought it will crashed the two sections.

Thats a very good suggestion.I'm going to use that for sure.

Thanks Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top