ow to calculate my prescaler.

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

Given the example.

 

 

//Configure timer

   TCCR1B = (1<<CS12);    //1:256 prescaler

 

   for(;;){

      //Read timer value and act according with it

      if(TCNT1 >= 31249){      //Our pre-calculated timer count

         PORTB ^= (1<<PB5);    //Toggle the led state

         TCNT1 = 0;            //Reset the timer value

      }

   }

 

My understanding is that it will be checking my time register and toggling my LED state 256th of my clock. If my clock is 12MHz how can I figure out how many ms my prescaller is firing apart? I found a few calculations but struggling a bit. My goal here is to set up a check in this loop that fires every 20ms.

 

 

This topic has a solution.
Last Edited: Tue. Oct 15, 2019 - 11:21 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What does 12000000/256/31250 equal? My calc says 1.5. Not quite the 50Hz (20ms) you want.

Try 12000000/256/50. I get 937.5.

 

There's also 12000000/8/30000 = 50

 

If you use CTC mode, you can test the output compare interrupt flag rather than do a comparison of the tcnt.


if (TIFR1 & (1<<OCF1A))
    {
        //20 ms has expired
        TIFR1 = (1<<OCF1A); //reset the interrupt flag
    }
    

 

Last Edited: Mon. Oct 7, 2019 - 03:52 AM