Page 1 of 1

TIMER 2

Posted: Tue May 31, 2022 1:05 pm
by siliconchip
Hi all
id like to know if possible if i could alter the output of timer 2 within software, i have a port toggling at 40 hz but would like to alter this frequency to say 49 hz for the first 10 seconds of operation then revert back to 40 hz, at this point frequencys are examples i realise i need to adjust prescaler, postscaler and roll over but how if at all do i achive this ??? i did try using a c box with PR2 = 210-1 To change the rollover this gives a frequency of 49hz which is correct using a decision box if less than 10 seconds however it would not go back to 40hz after 10 seconds but the c code must be right as in hardware i got 49 hz I also changed timer tick to 99 as per rollover change

ive added a simple flow chart thanks in advance

bob

Re: TIMER 2

Posted: Tue May 31, 2022 4:10 pm
by medelec35
Hi Bob.
The easiest way is by preloading the timer interrupt with a set value.
I have done that for a timer 0 example.
there is no reason why you can't do that for timer 2 as well.

Re: TIMER 2

Posted: Tue May 31, 2022 5:16 pm
by siliconchip
hi martin

thanks for the reply my desired frequency is 40Hz therefore from your reply i did the following
using timer 2 i set
prescaler rate 1:16
postscaler 1:13
rollover 256

for nearest value to 40 is 37.560 Hz

then i did preload = 256 - (8000000/4x16x40) = 3125 therefore 256 - 3125 = -2869 ???

ive obviously gone wrong somewhere but following the calculation im unsure ??

bob

Re: TIMER 2

Posted: Tue May 31, 2022 6:27 pm
by siliconchip
Hi martin
ive been playing around in software, using Timer 2 i cant seem to make the mentioned calcs work so i stuck with Timer 2 and made some changes with PR2 ive attached what i did and now for 20 seconds i get 51.90 Hz and the other 20 seconds i get 40 Hz then it repeats ive attached the flowchart and would value your advice

cheers bob

Re: TIMER 2

Posted: Wed Jun 01, 2022 7:28 am
by Bachman
Try to use these values (with 8 MHz clock):

Prescaler: 1:64
Postscaler: 1:13
PR2: 59 (40.064 Hz)
PR2: 48 (49.058 Hz)

Re: TIMER 2

Posted: Wed Jun 01, 2022 8:58 am
by siliconchip
Hi bachman
Thanks i will give it a try where did you get the chart from ?

Bob

Re: TIMER 2

Posted: Wed Jun 01, 2022 9:56 am
by Bachman
You can download the application from here (pCloud folder): https://u.pcloud.link/publink/show?code ... 0zDyoi9ySk

Please note: the application contain caclulations that are not available for every PIC microcontrollers. Check datasheet of the microcontroller. Eg.: you can use Timer 2 prescaler up to 128 but prescaler in PIC18F14K22, it can be only 1, 4 or 16.