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

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

tmr2 (computer app)

Postby brandonb » Sun Oct 20, 2013 9:27 am

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,
PICTimer2Calculator.zip
Zipped executable
(28.51 KiB) Downloaded 395 times

or direct link: https://dl.dropboxusercontent.com/u/10367218/PICTimer2Calculator.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

19660804
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
t2con=126;
pr2=149;

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
basic.jpg (45.85 KiB) Viewed 16575 times


here is picture of "more" box checked
more box (checked).jpg
more box (checked).jpg (78.35 KiB) Viewed 16575 times


here is the flowchart as well (simulation only)
very reduced tmr2 calculator.fcf
(26.13 KiB) Downloaded 294 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);


  prescaler=1;
  send_string("*");
  for(i=0;i<max_prescaler;i++){
    for(postscaler=1;postscaler<17;postscaler++){
      for(rollover=1;rollover<257;rollover++){
        float_prescaler=prescaler;
        float_postscaler=postscaler;
        float_rollover=rollover;
        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;
          }
        }
      }
    }
    prescaler*=4;
    send_string("*");
  }
  clear();
  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;
  }
  else{
    output_result = result_low;
    output_prescaler = prescaler_low;
    output_postscaler = postscaler_low;
    output_rollover = rollover_low;
  }
  clear();
  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;

  clear();

  if(output_result>0){
    send_float(output_result);
    cursor(0,1);
    send_number(output_prescaler);
    send_ascii(32);
    send_number(output_postscaler);
    send_ascii(32);
    send_number(output_rollover);
    cursor(0,2);
    send_string("t2con=");
    send_number(t2con_register);
    send_string(";");
    cursor(0,3);
    send_string("pr2=");
    send_number((output_rollover-1));
    send_string(";");
  }
  else{
    send_string("- redefine search -");
    cursor(0,1);
    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.
These users thanked the author brandonb for the post (total 16):
medelec35 (Sun Oct 20, 2013 9:34 am) • Kenrix2 (Sun Oct 20, 2013 9:38 am) • kersing (Sun Oct 20, 2013 10:13 am) • davehindmarsh (Sun Oct 20, 2013 11:26 am) • dazz (Sun Oct 20, 2013 3:42 pm) • Errol (Sun Oct 20, 2013 8:15 pm) • DannyBerry (Sun Oct 20, 2013 11:13 pm) • jgu1 (Mon Oct 21, 2013 6:48 am) • Werner (Tue Oct 22, 2013 5:56 pm) • acestu (Tue Oct 22, 2013 6:51 pm) and 6 more users
Rating: 80%
 

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8618
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2480 times
Been thanked: 3567 times
Contact:

Re: tmr2 (computer app)

Postby medelec35 » Sun Oct 20, 2013 10:20 am

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. :)

Martin
These users thanked the author medelec35 for the post:
brandonb (Sun Oct 20, 2013 10:38 am)
Rating: 5%
 
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: tmr2 (computer app)

Postby brandonb » Sun Oct 20, 2013 10:46 am

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 :?:
These users thanked the author brandonb for the post (total 2):
DannyBerry (Sun Oct 20, 2013 11:07 pm) • medelec35 (Sun Oct 20, 2013 11:13 pm)
Rating: 10%
 

DannyBerry
Posts: 29
Joined: Tue Aug 30, 2011 9:20 pm
Has thanked: 11 times
Been thanked: 1 time
Contact:

Re: tmr2 (computer app)

Postby DannyBerry » Sun Oct 20, 2013 11:09 pm

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

Irwin
Posts: 1
Joined: Mon Jul 29, 2013 9:21 am
Has thanked: 1 time
Contact:

Re: tmr2 (computer app)

Postby Irwin » Wed Oct 23, 2013 6:04 am

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!

davehindmarsh
Posts: 2
Joined: Sun Feb 10, 2013 8:32 pm
Has thanked: 3 times
Contact:

Re: tmr2 (computer app)

Postby davehindmarsh » Thu Nov 21, 2013 8:33 pm

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