tmr2 interrupt flowcode calculator!

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
brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

tmr2 interrupt flowcode calculator!

Post by brandonb »

if you want to quickly get all the prescaler,postscaler.rollover values of a desired tmr2 frequency or get all the frequencys and adjustment settings in a certain range around a desired frequency this is for you....i created this as a simulator flowchart to help pick those frequecies and settings easy, the first code block at the top of the flowchart you'll see these four lines of code in a block that you can modify

f_osc = 16000000
target = 1000
range = 0
display_delay = 2

f_osc--> type in the oscillator speed of the crystal or internal osc
target--> type in the target tmr2 frequency that your trying to hit
range--> type in the range around the desired tmr2 freq that you would like to hit, for instance if you type in 25, it will search for the target frequency and any frequency that within 25 numbers positive and 25 numbers negative of the target freq
1000 (1025 to 975)
also if you select "0" it gives you direct hits only, these are all long variables so you can use big numbers, if you exceed more than 254 readbacks it will say ">254" on the lower right side of the screen and only give you 254 readbacks
display_delay--> type in the second delay you want between the readback data numbers

here is a screen shot that tells you what your looking at[img]
TMR2 CALCULATOR EXPLAINATION.jpg
TMR2 CALCULATOR EXPLAINATION.jpg (68.78 KiB) Viewed 13967 times
[/img]
tell me what you guys think?
Attachments
flowcode_tmr2_calculator.fcf
(35.32 KiB) Downloaded 499 times

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

Re: tmr2 interrupt flowcode calculator!

Post by brandonb »

something important to note that i forgot to mention on the calculator, if your searching something like 10hz you can do

f_osc = 16000000
target = 10
range = 9
display_delay = 2

**BUT THE "RANGE" NUMBER HAS TO BE LOWER THAN THE "TARGET" NUMBER
OR IT WILL NOT RETURN ANYTHING*** example below is bad juju,
range cannot be equel to target or greater than target

f_osc = 16000000
target = 10
range = 10
display_delay = 2

i always try range = 0 to see if there is a direct hit first, then adjust the range to other values progressivly till you get what your looking for

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

Re: tmr2 interrupt flowcode calculator!

Post by brandonb »

on this one i think i have it all fixed right, i added the rollover value of 256(oops), and made the range and target both floating point variables, now if you select "0" it will give you only the exact matches +/- .1
i also fixed a couple of minor things, now you will get alot more returns but more focused if you set your range correctly,
another thing i did with this fcf is if the returned number of results are greater than 254 it will tell you "buffer overflow refine results" instead of showing the 254 results, reason why i chose this is the array buffers are overflowed and you would be veiwing only part of the info,
same rules apply with target and range--> range cannot be equel to or greater than target
for range and target you can type in whole numbers or floating numbers, it will return floats either way
this new fcf is alot better than the old one, thanks for checking it out
Attachments
flowcode_tmr2_calculator_fix.fcf
(37.24 KiB) Downloaded 519 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: tmr2 interrupt flowcode calculator!

Post by medelec35 »

Thanks for posting Brandon.

What a great idea :idea:
I was going to do a similar thing with excel, and kept putting it off due to how involved it was going to be.
Glad you have done this, since I don't need to work it out myself :D

Martin
Martin

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

Re: tmr2 interrupt flowcode calculator!

Post by brandonb »

Glad you have done this, since I don't need to work it out myself :D
i was hoping you would like it and would use it to make future projects :) speaking calculators i have a 32 to 4 x 8bit converter somewhere on the forums as well...hopefully i put this in a place where other members can easily find it if they need to

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
Contact:

Re: tmr2 interrupt flowcode calculator!

Post by kersing »

brandonb wrote:hopefully i put this in a place where other members can easily find it if they need to
May-be this could be moved/copied to the tips & tricks section??
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: tmr2 interrupt flowcode calculator!

Post by medelec35 »

kersing wrote:
May-be this could be moved/copied to the tips & tricks section??
Good idea!
Sorted. :)

Martin
Martin

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

Re: tmr2 interrupt flowcode calculator!

Post by brandonb »

i really should get more sleep lately, 5 hours a night just dont cut it :lol: i've been playing with it alot and tonight i ran into my first incorrect result with the last file with the settings osc 32000000 target 30 range .6 it returned 30.39hz with settings 64-16-1 which is way off, i traced it down to image below, rollover division math is 1-256 but i used a 8 bit array to store the number in so what i did is i would add 1 to the loop value of rollover from starting point zero, do the math on it, subtract a count and store it in a array, when i recall it i add one back to it for the display, i was thinking rollover 256 but i should have been thinking rollover array 255.... it tests out proper after fcf second fix update below (sorry, hopefully there are no more groggy minded errors)
Attachments
flowcode_tmr2_calculator_second_fix.fcf
(37.23 KiB) Downloaded 452 times
oops my bad.jpg
oops my bad.jpg (92.87 KiB) Viewed 13865 times

medelec35
Matrix Staff
Posts: 9520
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times
Contact:

Re: tmr2 interrupt flowcode calculator!

Post by medelec35 »

Hi Brandon,
I have just made some minor modifications.
My favorite mod allows the best setting out of all the results to be always left on the display.
E.g if you want 70 as the target and results are:
70.303, 70.028, 69.754, 70.323 70.028, 69.735, 70.382, 70.067, 69.754

The Calculator will show 70.028:
Timer2 cal.png
(19.28 KiB) Downloaded 8982 times
The other mod is to be able to enter in the first calculation box either 16 (prescaler1 to 16) or 64 (prescaler1 to 64)

some clever calculations you did within flowchart Brandon!
I will definitely use this instead on online calculators!

Martin
Attachments
flowcode_tmr2_calculator_second_fix with Rounding4.fcf
(49.49 KiB) Downloaded 487 times
Martin

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

Re: tmr2 interrupt flowcode calculator!

Post by brandonb »

some clever calculations you did within flowchart Brandon!
I will definitely use this instead on online calculators!
thanks martin, its nice to be able to give something back to the community :D i like the new features! i think we are making this too easy for people :lol: in flowchart below i added generated c code for the t2con register and pr2(rollover), tell me what you think?
tmr2 calculator + register sets+comments.fcf
(46.81 KiB) Downloaded 428 times
i had issues trying to figure out the string magic you did?
dannyberry wanted to see what the binary for the t2con register reads as well, so heres a flowchart that shows that,
tmr2 calculator+generated c code and binary.fcf
(49.84 KiB) Downloaded 433 times
(thanks enamul for showing how to show binary numbers http://www.matrixmultimedia.com/mmforum ... ers#p41438)
explaination.jpg
explaination.jpg (45.83 KiB) Viewed 13797 times
*** i changed the flowcharts to give // decimal number for doing lookup tables and such ***
Last edited by brandonb on Mon Dec 03, 2012 2:25 am, edited 2 times in total.

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: tmr2 interrupt flowcode calculator!

Post by Enamul »

Thanks Bandonb for your nice efforts. It's really great effort.
Enamul
University of Nottingham
enamul4mm@gmail.com

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

Re: tmr2 interrupt flowcode calculator!

Post by DannyBerry »

Thanks Brandon...I have to give you a big thumbs up, especially since I was the :D picky one about adding the binary part. :lol:

That was quick. :D

Danny

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

Re: tmr2 interrupt flowcode calculator!

Post by brandonb »

there are two different calculators below
------ AUTOMATIC FIND-------------------
tmr2_calculator_automatic_find.fcf
(60.91 KiB) Downloaded 368 times
-------------------
in this one you set only the F_OSC and F_TARGET and it will give you back the results that matches or the closest to your target frequency, the f_range is automaticly adjusted with in the running program, adjusting it does nothing
f_osc--> running oscillator frequency
f_target--> frequency you want to find the closest or direct match for

Code: Select all

//MODIFY ITEMS IN THIS BLOCK
f_osc = 16000000 //float variable for oscillator speed
f_target = 19 //float variable (target freq)
display_delay = 2 //byte (display pause in m/s)
//-------------------------------------------
----------LOOPING -------------------
tmr2_calculator_looping.fcf
(61.52 KiB) Downloaded 364 times
-----------------------------------
this one is made for creating frequency generators, you set the number that you want to increment in and it will find the closest single match to the number you input then increment by the number you selected for the incrementor and continue to show you the closest results each time it runs, f_range is automaticly adjusted with in the running program
f_osc--> running oscillator frequency
f_increment_target--> put in the number you want to increment by
f_target--> in this mode you set it where you want to start incrementing from

Code: Select all

//  *** MODIFY THESE ITEMS ***

f_osc = 16000000 //float variable (oscillator speed)
f_increment_target = 10 // increments by this number
f_target = 0 // where to start incrementing from
//------------------------------------------------
matrix.jpg
matrix.jpg (68.8 KiB) Viewed 13527 times
for those familiar with the previous calculators, when automatic is enabled as is by default, the range is calculated by
dividing the target by 2 and giving that above the target and below the target giving a large area of search,
it writes to different arrays and after checking all 16384 combinations there will be a closest number >= target and a closest number<=target, these are checked to see which one is closer, those arrays are linked to the lcd to display the best choice values... the numbers to right of the comment lines "//" are the numbers i use to write code with
*** NOTE: these are set up to check 64:1 prescaler and will return them as best choice if one is avalible ***
"martin can you fix these fcf's to your preferances" :D

Post Reply