Delay, Timer

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 8.

Moderator: Benj

Corto
Posts: 26
Joined: Thu Sep 04, 2014 7:01 am
Has thanked: 7 times
Been thanked: 6 times
Contact:

Delay, Timer

Postby Corto » Wed Nov 07, 2018 5:28 pm

Hello All
my question is about timers. If I want, for example, to turn on the backlight of an LCD for 2 minutes, by pressing a push button and otherwise the main program continues to run. I imagine that I can bring in a timer (interupt), but if the delay should last 8:00, how can I (PIC 16F1829).
The second question is, I have a PIC which is possible to work with internal clock (INTOSC) at 64 MHz with the PLL x4.in the Flowcode configuration, do I need to set the clock to 16MHz (because I set the PLL enable) or 64MHz?
Many Thanks in advance

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

Re: Delay, Timer

Postby Benj » Wed Nov 07, 2018 6:41 pm

Hello Corto,

my question is about timers. If I want, for example, to turn on the backlight of an LCD for 2 minutes, by pressing a push button and otherwise the main program continues to run. I imagine that I can bring in a timer (interupt), but if the delay should last 8:00, how can I (PIC 16F1829).


Yes simply enable a timer interrupt with the slowest possible timeout. Depending on the timeout simply increment a count variable and once you have reached the correct amount of interrupts you know that the right amount of time has passed.

Here is a simple example to wait 8 minutes at 32MHz using a 16F1829. The program waits for the switch to be pressed and then lights an LED. The LED will go out after 8 minutes and the program will repeat. Multiple switch presses will reset the timer back to 8 minutes. I recommend for development you reduce the 8 mins to a more manageable time so you're not waiting for ages to confirm if it works.

Demo.fcfx
(11.57 KiB) Downloaded 150 times


The second question is, I have a PIC which is possible to work with internal clock (INTOSC) at 64 MHz with the PLL x4.in the Flowcode configuration, do I need to set the clock to 16MHz (because I set the PLL enable) or 64MHz?


You should use the clock speed 32000000, i.e. the speed the chip is running at. I believe 32MHz is the fastest that the 16F1829 will run based on page 348 of the datasheet.

Corto
Posts: 26
Joined: Thu Sep 04, 2014 7:01 am
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: Delay, Timer

Postby Corto » Thu Nov 08, 2018 7:06 am

Thank you very much Benj for all
In my question, about 8, I forgot to add "Hours". Yes I wonder how one can manage to count several hours, knowing that at the bottom, in general, with a prescaler at 1 the frequency of interruption is around 125 Hz => 125x3600x8 = 3600000.
Sorry Benj for this forgetfulness in my question.
Regarding the 64 MHz I also forgot to mention that it referred to another PIC, otherwise you're right the 16F1829 is 32 max.
however, I think that if for the 16F1829 it is necessary to set in Flowcode 32 MHz the procedure is identical for all the others.

Corto
Posts: 26
Joined: Thu Sep 04, 2014 7:01 am
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: Delay, Timer

Postby Corto » Thu Nov 08, 2018 8:05 am

Benj,
I watched the demo you added.
the OSCCON is settled:
SCS = Oscillator Timer 1;
IRCF = 31 KHz;
SPLLEN = 4 x PLL disabled
I do not understand :
the interrupt frequency you wrotte at 15.259 Hz instead of 31 KHz;
the timer is disabled (perhaps to avoid calling a macro, only for the demo).

Do I have to add a second PIC in my circuit just for this timer? because there would be two different frequencies

Corto
Posts: 26
Joined: Thu Sep 04, 2014 7:01 am
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: Delay, Timer

Postby Corto » Thu Nov 08, 2018 8:12 am

Sorry, I unsheathe too quickly! lol
forget the question on 15.259 (the prescaler must be set to 1: 8)
oh my God

User avatar
medelec35
Valued Contributor
Valued Contributor
Posts: 8536
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2456 times
Been thanked: 3525 times
Contact:

Re: Delay, Timer

Postby medelec35 » Thu Nov 08, 2018 8:57 am

Corto wrote:the interrupt frequency you wrotte at 15.259 Hz instead of 31 KHz;

It is wise to set the interrupt time for as low as you can get away with.
Reason being when interrupt is triggered, the program will pause where it is then will automatically run the macro that interrupt is set up to call, complete the interrupt macro then carry on from where it left off.
With the interrupt set at 15.259Hz just means the TimerInt macro will automatically get called 15.259 times every single second.
Every time the TimerInt macro accessed then Tcount variable will be incremented by 1, Value will increase by 15 every second
If you want to time 8 minutes = 480 seconds then Tcount within decision branch will have to be 15.259 * 480 = 7324.32 as Ben shows within interrupt call macro.

A personal preference, I use timer2
Timing can be much more accurate.
Take a look here.

Hope this makes this clearer
Martin

If you read a post that is useful, please show appreciation by clicking on thumbs up Icon.

Corto
Posts: 26
Joined: Thu Sep 04, 2014 7:01 am
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: Delay, Timer

Postby Corto » Thu Nov 08, 2018 10:01 am

Hello Medelec
thank you for this answer, I understand the role of the interruption, by cons if I have a main program that runs at 32 MHz (for example OSCCON 0xF0 for the 16F1829) and I want to involve this delay triggered by an interruption every 15.259 Hz (OSCCON 0x38), I can not mix the 2 OSCCON, hence my question, should I add a second PIC on my circuit? to only manage the delay.
I would say yes, considering that the clock must be set to 31 KHz and the pre-scale 1/8 to 15.259. or maybe put several timer in cascade.
the first calls a macro, start with an OSCCON at 0x38 and another interrupt that it would be called 15.259 times per second and increment in another macro a counter!
or is it completly crazy, like me probably !

Corto
Posts: 26
Joined: Thu Sep 04, 2014 7:01 am
Has thanked: 7 times
Been thanked: 6 times
Contact:

Re: Delay, Timer

Postby Corto » Thu Nov 08, 2018 10:13 am

Medelec,
Forget all, sorry, I confused all.
But for a delay of 8 hours, if I set the timer 2 the minimum frequence is 122.070
for 8 hours 122.07 x 3600 x 8 =3515616
it is too much for the counter

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

Re: Delay, Timer

Postby Benj » Thu Nov 08, 2018 2:17 pm

Hello,

for 8 hours 122.07 x 3600 x 8 =3515616
it is too much for the counter


Change the timer count variable from an UINT to an ULONG and then you will be able to count that high :)

DemoSS.jpg
DemoSS.jpg (79.63 KiB) Viewed 1625 times


Also no you definitely don't need a second device, run the IC at 32MHz using the OSCCON setting I provided in the demo. Note I setup the OSCCON but the configuration setting may not have been correct as you observed.