TIMER 2

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

TIMER 2

Post 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
Attachments
TOGGLE PIN.fcfx
(13.68 KiB) Downloaded 104 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: TIMER 2

Post 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.
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: TIMER 2

Post 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

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: TIMER 2

Post 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
Attachments
TOGGLE PIN.fcfx
(15.27 KiB) Downloaded 101 times

User avatar
Bachman
Posts: 116
Joined: Sun Sep 07, 2014 11:37 am
Location: Hungary
Has thanked: 9 times
Been thanked: 53 times
Contact:

Re: TIMER 2

Post 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)
Attachments
ScreenShot_153.png
ScreenShot_153.png (190.73 KiB) Viewed 8236 times

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 24 times
Contact:

Re: TIMER 2

Post by siliconchip »

Hi bachman
Thanks i will give it a try where did you get the chart from ?

Bob

User avatar
Bachman
Posts: 116
Joined: Sun Sep 07, 2014 11:37 am
Location: Hungary
Has thanked: 9 times
Been thanked: 53 times
Contact:

Re: TIMER 2

Post 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.

Post Reply