Page 1 of 1

TIMER QUESTION

Posted: Tue Dec 01, 2020 7:52 pm
by siliconchip
hi all,
id like to ask a question regards timers is it possible to use timer 2 under 2 different macro names ie timer 2 and timer 2a and adding 2 interupts with said names so i can toggle the same output A0 but for a different amount of time ie timer2 for 20 seconds and timer2a for 60 seconds, i had a play but think i need 2 seperate timers with seperate outputs ie A0 and A1,thanks in advance

regards bob

Re: TIMER QUESTION

Posted: Tue Dec 01, 2020 8:23 pm
by medelec35
Hi Bob,
What I would do is use set a variable e.g called to set the SecondsDuration to the required seconds duration e.g 20.
So you could then use a decision branch within timer interrupt: if SecondsDuration = Seconds then seconds = 0; toggle pin.
The SecondsDuration could of course be any value.
Give that a try and if you get stuck, post a flowchart & I will help you further.

Re: TIMER QUESTION

Posted: Wed Dec 02, 2020 10:45 am
by Benj
Hi Bob,

You cannot enable an interupt for a different macro. The way the underlying C code works only the first macro will ever be called.

You can however enable the interrupt several times for the same macro, you can also reference global variables in the timer interrupt macro and so you can do different rates and different things by making decisions on the variable.

Of course what Martin has said is also valid and you can keep the interrupt rate the same but have a software prescaler using a count variable and changing the count rollover using a global variable.

Hope this makes sense.

Re: TIMER QUESTION

Posted: Wed Dec 02, 2020 2:55 pm
by siliconchip
hi benj and martin

As always thanks for the replys im not too familiar with interupts are there any tutorials on them and how to use them a bit more in depth ?? Ive added my code and would appreciate it if you could give any pointers if ive over complicated it, its pretty much doing what i want now but i think ive duplicated to much code ?? Thanks in advance


Bob

Re: TIMER QUESTION

Posted: Wed Dec 02, 2020 4:13 pm
by Benj
Hi Bob,

Looks ok to me. I've moved the duplicated code into a macro so you can just call the macro at various points.
60Hzfreq v2.fcfx
(23.11 KiB) Downloaded 154 times

Re: TIMER QUESTION

Posted: Wed Dec 02, 2020 4:58 pm
by siliconchip
Hi benj
Thats great many thanks

Bob