'1 Second Timer Interrupt'
Hello! I have a pic16c84/04 here and I would like to know if there is
a way to make the timer interrupt go on each second. How can I do
that? Thank you very much!
BTW: sorry for the newbie question!
Jean-Francois Joly - capitalnet.comjolyal
At 01:07 PM 5/8/97 -0400, you wrote:
>Hello! I have a pic16c84/04 here and I would like to know if there is
>a way to make the timer interrupt go on each second. How can I do
>that? Thank you very much!
if you want really interrupts on each second you must use a very low
frequency for the crystal. at normal speeds pic16c84 timer 8 bit +
prescaler 8 bit will alwasy overflow faster than once per second
the best way to get 1 second interval is to set the timer reload value
and prescaler to some suitable values and extend the timer in software
by counting intrrupts until one second is elapsed.
No Limits Go To
Basics of AVR AVR
AVRBASIC http://www.avrbasic.com Born for Basic
Grand opening in August.
Watch out for first week offers.
Byron A Jeff
> Hello! I have a pic16c84/04 here and I would like to know if there is
> a way to make the timer interrupt go on each second. How can I do
> that? Thank you very much!
Well presuming that you're going to clock it at 4 Mhz, there isn't a way
to do it. It's a hardware limitation.
The clock oscillator divides the input clock by 4. That gives you 1Mhz.
The TMR0 is 8 bits. You can also prescale up to 8 bits which in effect
divides the clock by 256. So the maximum divide factor in hardware is
65536. Dividing this into 1 Mhz gives you 15.2587890625 HZ. The amounts
to one interrupt every 65 milliseconds.
The solution that is usually put to use isn't hard. Simply let the interrupt
tick faster than the clock you want and count the ticks. When the ticks add
up to your desired 1 second, then call the routine that actually does something
once a second.
So for the same example, take the 1 Mhz and prescale by 64. This gives
a tick rate for TMR0 of 15625 Hz. Now set up TMR0 to roll over after 125
ticks (which is the largest factor of 15625 - in fact it's the square root).
This will give you exactly 125 ticks per second. You'll have the reset the
TMR0 register each interrupt to count off 125 more ticks until the next
interrupt. Also note the number of interrupts that has occured. When you
reach 125 interrupts, you know that one second has elapsed and then you
do your one second routine.
Now this isn't the most precise method because resetting the TMR0 each
interrupt resets the prescaler too. This means you'll lose a bit of time
If timing is important and needs to be real precise then choosing a better
initial clock frequency will help immensely. The objective is to get the
TMR0 to roll over naturally without having to be preset. Then the free
running TMR0 and prescaler will never lose any time.
For example changing the input clock to 4.096 Mhz will accomplish this task.
The 1.024 Mhz input can be prescaled by 32 (giving exactly 32000 ticks to
TMR0. With this clock a free running TMR0 will generate exactly 125 interrupt
each second. And because both the prescaler and TMR0 are free running the
clock will be quite accurate.
Hope this helps,
More... (looser matching)
- Last day of these posts
- In 1997
, 1998 only
- New search...