## Timer 2 Calculator

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

Moderators: Benj, Mods

medelec35
Valued Contributor
Posts: 8825
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2561 times
Been thanked: 3720 times
Contact:

### Timer 2 Calculator

I prefer using timer 2 over other timers as it allows much more precise control.
That is because you have a Postscaler, Prescaler and a rollover value to deal with.
While having more options gives you much better flexibility, it's downfall is its more complicated to calculate correct values to give desired frequency.
For example, I'm after an interrupt hat gives me exactly 60Hz, how do I determine the correct Postscaler, Prescaler and Rollover values?
That is where the attached Excel sheet comes in extremely handy.
Just enter the oscillator frequency and required interrupt frequency and the Excel sheet gives all the answers!
Timer 2 Calculator.png (85.74 KiB) Viewed 1918 times

Timer 2 Flowcode.png (37.22 KiB) Viewed 1918 times

As you can see if maximum prescaler is only 16 then the nearest value to 60 will be 75Hz.
How about I would like to know all the whole frequency values?
Not a problem, excel sheet can do that as well:
Timer 2 Calculator whole freq.png (135.29 KiB) Viewed 1918 times

Hope this helps you as much as it has helped me.
The Excel sheet was modified with Libra Office, but I see no reason why it won't work with excel.
Note: After entering frequency you must select the drop-down and what maximum Prescaler you have, before results are shown.
Attachments
Timer 2 Calculator V2.xlsx
These users thanked the author medelec35 for the post (total 6):
kersing (Tue Mar 10, 2020 8:19 pm) • AbhijitR (Wed Mar 11, 2020 4:29 am) • LeighM (Wed Mar 11, 2020 8:28 am) • Benj (Wed Mar 11, 2020 11:03 am) • Steve001 (Mon Mar 16, 2020 7:59 pm) • jgu1 (Mon Apr 06, 2020 7:58 am)
Rating: 30%

Martin

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

medelec35
Valued Contributor
Posts: 8825
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2561 times
Been thanked: 3720 times
Contact:

### Re: Timer 2 Calculator

V2 shows a more instant values without having to select drop-down filter.
Timer 2_Calculator V2 Instant View.png (43.93 KiB) Viewed 1886 times

Also, selection bug fix.
These users thanked the author medelec35 for the post (total 4):
Benj (Wed Mar 11, 2020 11:02 am) • kersing (Wed Mar 11, 2020 4:05 pm) • jgu1 (Mon Apr 06, 2020 7:59 am) • chipfryer27 (Sat Apr 11, 2020 10:21 am)
Rating: 20%

Martin

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

Steve001
Valued Contributor
Posts: 1128
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 521 times
Contact:

### Re: Timer 2 Calculator

Thanks Martin

Appears to work ok with Apache open office

Steve
These users thanked the author Steve001 for the post (total 2):
medelec35 (Mon Mar 16, 2020 8:06 pm) • Benj (Mon Mar 16, 2020 9:44 pm)
Rating: 10%

Success always occurs in private and failure in full view.