Changing Timer2 properties with variables.

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

Changing Timer2 properties with variables.

Post by medelec35 »

I have been asked if this is possible, so here is my solution and V5 Flowchart:

All you need to do is change any or all of the 3 variables anywhere in you Flowchart.
E.g

Code: Select all

Prescale = 16
Postscale = 3
Rollover = 54
Does not have to be at the beginning. Variables can be altered within any macro. After you have changed any of the 3 variables, to allow variables to alter timer2 interrupt frequency just call Timer2_ADJ macro, then timer2 register will be altered with new values.
The C code is just directly changing timer2 register directly.
To change:
Prescale:

Code: Select all

t2con.T2CKPS0=FCV_T2CKPS0;
t2con.T2CKPS1=FCV_T2CKPS1;
Postscale:

Code: Select all

t2con = t2con|= FCV_POSTSCALE;
Rollover:

Code: Select all

pr2=FCV_ROLLOVER;
Target is 16F88, but should work with other devices with timer2 interrupt.
Attachments
Setting Timer2 Values.fcf
Flowcode V5
(13.75 KiB) Downloaded 466 times
Martin

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Changing Timer2 properties with variables.

Post by Benj »

Great article Martin, Looks like this could come in very useful if you need to tweak the timer on the fly.

I use something similar for my prop clock where the segments are drawn on a timer tick. Every complete rotation I check how many segments have been output and then adjust the timer settings so that the number of segments is balanced against the motor speed to get a stable display.

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: Changing Timer2 properties with variables.

Post by medelec35 »

Thanks Ben,
I believe it was requested for more control with frequency generator.

Yes on the fly would be best way of using this method.

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: Changing Timer2 properties with variables.

Post by brandonb »

hey martin, thanks for posting this, it was alot of help, and if anyone want to check it out heres my interpertation of it in fcf below with t2con and pr2 c code,... and yes i know i wrote the ratios backwards :lol: ...did you notice the prescaler on the new chips can go down to 1:64 :!: which it can now be used as a alternative for extra timer 1 macros .... when the program starts press switch 3, this switch is used to toggle the screen between "tmr2(prescaler) tmr2(postscaler) tm2(rollover)..... when selected the option that you want to set select switch 1 for increase or switch2 for decrease the value......... the rollover has quick access features if you hold switch 4 while incrementing or decrementing it will increase increase value by x10.. same with the switch 5 but will increase by x100, this is on roll over only, the values get updated each time you hit increment or decrement switches... switches 1-5 are on b1-b5 output is on a0..... the ontime pulse is set to 8micro seconds, so anything above 60Khz will lock it up and the main will take forever to run.... change the ontime by increasing the loop count in the tmr2 macro, 50 works good but the max obtainable frequency is limited less then 50K
Attachments
pro_tmr2_onthefly.fcf
(36.8 KiB) Downloaded 388 times

Post Reply