tmr2 (computer app)

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona

tmr2 (computer app)

Post by brandonb »

to demystify the tmr2 interrupt, a friend (rod maher) and i came up with a tmr2 calculator computer app that will speed up project development time and increase confidence in using this interrupt, here is a link,
Zipped executable
(28.51 KiB) Downloaded 438 times
or direct link: ... ulator.exe
there are three input parameters:
- oscillator speed (crystal or rc value)
- target tmr2 interrupt speed (desired tmr2/4/6 interrupt frequency)
- max prescaler 16:1(standard) or 64:1(enhanced midrange chips)

enter input values then select "do it" and the closest match will be returned with all the parameters, if a match is not found with in the search criteria it will return message "refine search"
the search range is half of the "target tmr2 interrupt speed" value, but applies to both above and below the desired frequency. what i mean by this is, if you desire 100 hz, it will set up a search range of (50 to 150), if desired 1000 it will set a range of (500 to 1500), allowing quicker results by only logging near matches, as it runs through the program when ever a returned value is closer than the high or low search range it modifies the search range to the new high or low range value, this is how it finds the closest match, by looking above the desired and below the desired, then after all is processed it checks to see which is closer to the desired (range_high or range_low), and returns the logged values for those as output results

if you want to see all the math calculations which are many, you can select "more" then hit "do it", it will take a few seconds to log the data, then can scroll veiwing all the combinations for the oscillator and max prescaler settings which are (returned tmr2 freq,prescaler,postscaler,rollover)

there are a couple other helpful features, in theoutput results box there are grey boxes to the left of the results, if you click on one of these boxes you can select to copy the whole line or just the number to clipboard, this allows you to copy the c code lines directly into your flowcharts or the numbers directly into look up tables to run the timers,
notice even thought it says t2con to use this code for tmr4 and tmr6 all you have to do is change the register name from t2con/pr2 to t4con/pr4 or t6con/pr6, the values are the same

another nice feature that rod put into this is the ability to do math on the input values, there are calculator icon's at the end of the oscillator and target timer2 interrupt speed input areas.... here is where this comes in handy, suppose you what to toggle a pin at 1124.589 hz so you type this in the desired frequency area... but then you realize that you are gonna toggle the pin so you'll need 2 times this frequency, just click on the calculator at the end of the line, it will open with the value that is currently in the box, then hit ( * 2 = "accept and close" ) it will now put the result on the line so you can resimulate
another good use of this is by using the calculator thats on the oscillator line, suppose your using a oscillator speed of 19660804 and you need 1hz tmr2 interrupt speed.... the way you go about this is put 19660804 in the osc box then put 1 in the desired tmr2 freq box, then select your max prescaler!
hit "do it" , yep you get a message "refine search" no worries though, open your calculator on the oscillator line and hit
( /2 = "accept and close" ) after that hit "do it"
you would keep doing this until you get the value that you want, so check this out

Code: Select all

1 hz
16:1 max
no results   but divide the osc by 2 and resimulate until you get 1hz

you will see that if you divide it down enought by 2 you will arrive at a direct match with this

oscillator = 153600.03125
Frequency Result: 1
Prescaler: 16
Postscaler: 16
Rollover: 150

ok so your thinking how is that gonna help me right?
  if you take your hardware osc that you first entered in which was 19660804 / 153600.03125 you get 128, because your just divided by two each time,
  so now you can use the results for 153600.03125 crystal with the 19660804 crystal if you only run the code in the tmr2 isr every 128th time, this can be done with a count variable, if count == 128, count=0, allow_ isr kind of thing
  does that make sense?
what i usually do is have a look up table with t2con values, another table with pr2 values, and another table with timer divide values, then use a variable to access the tables to write the tmr2 and divide variable with
the "don't conferm copy" box, if check eliminates the message that says message sent to clipboard
the values that were entered into the program will be saved, so when you open calculator it will contain previous session, also can open many of these at the same time,.... i hope you guys find this calculator as useful as i do....

here is picture in basic mode
basic.jpg (45.84 KiB) Viewed 17420 times

here is picture of "more" box checked
more box (checked).jpg
more box (checked).jpg (78.35 KiB) Viewed 17420 times
here is the flowchart as well (simulation only)
very reduced tmr2 calculator.fcf
(26.13 KiB) Downloaded 326 times

here is the algarithm that i created in form of a function to find the closest match

Code: Select all

crystal  enter your oscillator speed here
target  enter you target tmr2 interrupt speed here
max_prescaler  ENTER (3 for 16:1) || (4 for 64:1)
 16f1800 and 16f1900 series chips use extended 64:1 prescaler values
 this give a total of 16384 combinations of timer2
void calculate (float crystal, float target, char max_prescaler){

float instruction_speed ;
float range_high;
float range_low;
unsigned int prescaler = 1;
unsigned int postscaler = 1;
unsigned int rollover = 1;
float result = 0;
float result_high = 0;
float prescaler_high = 0;
float postscaler_high = 0;
float rollover_high = 0;
float result_low = 0;
float prescaler_low = 0;
float postscaler_low = 0;
float rollover_low = 0;
float difference_high = 0;
float difference_low = 0;
float output_result = 0;
float output_prescaler = 0;
float output_postscaler = 0;
float output_rollover = 0;
char t2con_register = 0;
char t2con_high = 0;
char t2con_low = 0;
char pr2_register = 0;
float float_prescaler;
float float_postscaler;
float float_rollover;
char i;
  send_string("please wait ");

  instruction_speed = crystal / 4;
  range_high = (target / 2) + target;
  range_low = target - (target / 2);

        result = instruction_speed;
        result = result / float_prescaler;
        result = result / float_postscaler;
        result = result / float_rollover;
        if(result >= target){
          if(result < range_high){
            range_high = result;
            result_high = result;
            prescaler_high = float_prescaler;
            postscaler_high = float_postscaler;
            rollover_high = float_rollover;
        if(result <= target){
          if(result > range_low){
            range_low = result;
            result_low = result;
            prescaler_low = float_prescaler;
            postscaler_low = float_postscaler;
            rollover_low = float_rollover;
  difference_high = result_high - target;
  difference_low = target - result_low;
  if(difference_high < difference_low){
    output_result = result_high;
    output_prescaler = prescaler_high;
    output_postscaler = postscaler_high;
    output_rollover = rollover_high;
    output_result = result_low;
    output_prescaler = prescaler_low;
    output_postscaler = postscaler_low;
    output_rollover = rollover_low;
  if(output_prescaler==1)       t2con_low=0;
  else if(output_prescaler==4)  t2con_low=1;
  else if(output_prescaler==16) t2con_low=2;
  else if(output_prescaler==64) t2con_low=3;

  t2con_high = output_postscaler - 1;
  t2con_register = t2con_low;
  t2con_register |= (t2con_high) << 3;
  t2con_register |= 1 << 2;


    send_string("- redefine search -");
    send_string("results out of range");
if you find this useful please thank rod for his efforts by thanking post :wink:
Last edited by brandonb on Wed Oct 23, 2013 9:17 am, edited 2 times in total.

User avatar
Valued Contributor
Valued Contributor
Posts: 9014
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK

Re: tmr2 (computer app)

Post by medelec35 »

Hi Brandon
Thanks for posting this useful app.

I will certainly be using it when I need to set up timer 2 interrupt timer.

Looking at the app and your algorithm , there are some clever programming skills (by the both of you)!
I would love to create apps like that. :)


Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona

Re: tmr2 (computer app)

Post by brandonb »

Looking at the app and your algorithm , there are some clever programming skills (by the both of you)!
thanks man,... the c code was the algarithm that i wrote for rod using mikroc,... he copied it over and made some mods so it works with his compiler, then over the next week he added all the cool features to make it work the way it does,
i don't know how big the program is but i can ask if he would let me post it, or if its even practical to post it :?:

Posts: 29
Joined: Tue Aug 30, 2011 9:20 pm

Re: tmr2 (computer app)

Post by DannyBerry »

Thanks Brandon and Rod for your efforts....Awsome :D

Posts: 1
Joined: Mon Jul 29, 2013 9:21 am

Re: tmr2 (computer app)

Post by Irwin »

Awesome program, especially for us noobies! (or at lest me!) Having used your flowcode version of this I can say thanks :D , there are some features in this application that aren't there in the flowcode program. Great job and thanks to you and Rod for all of your work and sharing!

Posts: 2
Joined: Sun Feb 10, 2013 8:32 pm

Re: tmr2 (computer app)

Post by davehindmarsh »

Thanks Brandon you and rod both ROCk !,This programs great ! :D

Post Reply