Working on timers

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

im currently working on timers to use them as rtc.

im in a confusion between frequency and second.

#include <avr/io.h>
#include <avr/interrupt.h>

volatile uint8_t count;
//fcpu is 16mhz
void main()
{
   // Prescaler = FCPU/1024
   TCCR0|=(1<<CS02)|(1<<CS00);

   //Enable Overflow Interrupt Enable
   TIMSK|=(1<<TOIE0);

   //Initialize Counter
   TCNT0=0;

   //Initialize our varriable
   count=0;
  

   //Enable Global Interrupts
   sei();

   //Infinite loop
   while(1);
}

ISR(TIMER0_OVF_vect)
{
   //This is the interrupt service routine for TIMER0 OVERFLOW Interrupt.
   //CPU automatically call this when TIMER0 overflows.

   //Increment our variable
   count++;
}  

i got this code from the internet.

what i understood is in this code ....since timer 0 is 8 bit...we are resoluting  using prescaler to match its range.

so we are setting prescaler to fcpu/1024=15625 Hz

Frequency of Overflow = 15625 /256 = 61.0352 Hz

interrupt is sent whenever there is overflow.

i.e at each 61hz ...interuppt is sent.

i want to count seconds.

how?

i understood the concept of overflow but im not getting things between frequency and time.

thanks.

Salman

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why use an 8bit timer if you are trying to achieve periods as long as 1s on 16MHz? My calculator tells me that even if a timer is prescaled with /1024 (usually the max possible) then it must still count to 15625 (16000000 / 1024) to achieve 1 second. A 16bit timer (0..65535) can easily count to 15625. An 8bit timer only has a counting range of 0..255 so will overflow 61 times before it achieves 1 second.
.
Ergo, use 16bit.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
. A 16bit timer (0..65535) can easily count to 15625.  . Ergo, use 16bit.

in 8 bit with the help of over flow interuppt we come to know that certain cycles have passed and we update some variable accordingly.

in 16bit in this scenario how will we know that it has counted 15265?

Salman

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You use the compare mode of the timer. Set one of the OCR registers to the 15625 value and arrange to get a compare interrupt when it occurs or even just poll the state of the bit that says that has occurred. 

 

If you go with 8bit then put a static counter in the ISR and just increment it until it reaches 61 then trigger the 1 second action. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

clawson wrote:
If you go with 8bit then put a static counter in the ISR and just increment it until it reaches 61 then trigger the 1 second action.
Or for a RTC where you perhaps don't want to "lose" the fractional part, you can do it like this:

#define TIMERTICKS_PER_SECOND (F_CPU/1024)

ISR(TIMER0_OVF_vect) {

    static uint16_t ticks = 0;
    
    ticks += 256;
    if (ticks >= TIMERTICKS_PER_SECOND) {
        ticks -= TIMERTICKS_PER_SECOND;
        seconds++;
    }
    
}  

 

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

My math skills need looking at i missed the .035 bit of 61.035 - doh!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thankssmiley

Salman