Page 1 of 1

Changing Timer2 properties with variables.

Posted: Tue Jun 19, 2012 6:19 pm
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.

Re: Changing Timer2 properties with variables.

Posted: Wed Jun 20, 2012 10:29 am
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.

Re: Changing Timer2 properties with variables.

Posted: Wed Jun 20, 2012 1:37 pm
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

Re: Changing Timer2 properties with variables.

Posted: Thu Nov 22, 2012 1:35 am
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