RE: [bascom] Timer1 interrupt every second?


From "Per Alvin K. Frederiksen" <pef@post1.com>
Date Sat, 27 Mar 2004 19:27:03 +0100

Here is what I have discovered about Timers in AVR chips.

- by default they are UP counters.
- I set my counter like this:

Set constant with the value of the timer count:
   <Max Counter ability> - <count that I want>
In interrupt code, reload the counter value

With 8MHz AVR it works fine for me like the following code example,
also the following is NOT precise timing.

You can decide weather to put the code inside the interrupt rutine,
or in the main DO-LOOP

Best of luck, 
Alvin

-- Code Example ----------------

   Const 100us = 65536 - 1000
   Config Timer1 = Timer , Prescale = 8
   Timer1 = 100us

   'Then the usuals
   On Timer1 Timer_1_int
   Enable Timer1
   Enable interrupts

   'additional counters and Flag signal for the Main DO-LOOP
   Dim Tcount1 As Word
   Dim Flag1 As Bit


'***** MAIN PROGRAM LOOP ***** 
DO
   'something to do every second
   If Flag1 = 1 Then
      'Code here is repeated every 1 second 
      Reset Flag1
   End If
LOOP

Timer_1_int:

   ' Timer0 is reloaded as to be ready for counting the next time slice
   Timer1 = 100us
   ' In the AVR, Timer0 automatically restarts

   Incr Tcount1
   'Code here is repeated every 100 micro second (100uS)

   If Tcount1 >= 10000 Then
      Tcount1 = 0
      Set Flag1
      '
      'Code here is repeated every second 
   End if 
Return



-----Original Message-----
From: owner-bascom@grote.net [mailto:owner-bascom@grote.net]On Behalf Of
PatM
Sent: 27. marts 2004 17:51
To: Bascom@Grote.Net
Subject: [bascom] Timer1 interrupt every second?


I'm having a heck of a time wrapping my brain around this. I need an
interrupt once a second (high accuracy not required - timing out 5 minute
intervals).

What I thought was

System clock speed / prescale = timer tick speed.

So 1000000 / 64 = 15625 (0x3D09) ticks per second.

So if I used

Config Timer1 = Timer , Compare A = disconnect , Compare B = disconnect ,
Prescale = 64

On Timer1 Tim1_isr
Enable Timer1
Enable Interrupts

Do
    LCD "Seconds:";Seconds;" "
Loop

Tim1_isr:
        Tcnt1H = &HC2
        Tcnt1L = &HF6
        Timer1 = 0
        Seconds = Seconds + 1
        If Seconds > 59 Then Seconds = 0
Return

I'd get a 1 second ISR. problem is that it appears to be a fair bit longer
than one second...

How do I calculate this puppy properly?