Timer or interupt?

For general Flowcode discussion that does not belong in the other sections.
Post Reply
hippalator
Posts: 23
http://meble-kuchenne.info.pl
Joined: Tue Jul 15, 2025 9:05 pm
Has thanked: 3 times

Timer or interupt?

Post by hippalator »

Adding a heart beat LED to an output pin to let me know if the PIC is actively running.
Using every CIP I can for my project to leave thecore to more important tasks.
I did get a flashing LED using delay but do not want any delays if I don't have to.
Asking if it's better to use an interupt to toggle the pin or a timer.
Last edited by hippalator on Fri Nov 21, 2025 8:19 pm, edited 1 time in total.

hippalator
Posts: 23
Joined: Tue Jul 15, 2025 9:05 pm
Has thanked: 3 times

Re: Timer or interupt?

Post by hippalator »

Having a difficult time learning interupts.
Not much out there on interupts.
Any links anyone know of?

mnfisher
Valued Contributor
Posts: 1716
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 146 times
Been thanked: 799 times

Re: Timer or interupt?

Post by mnfisher »

An interrupt is easier to use for real times tasks - doing things in a loop with delays gets much more complicated as the task gets harder - imagine flashing two (or more) LEDs at different intervals for example...
The mistake most commonly made it trying to do too much on the interrupt handler (ISR) - which needs to be as quick as possible.
Of course other interrupts are available too besides timer - and they allow MCUs to respond to events in 'real time' so are well worth getting the hang of 🙂 (for example reading intput from a UART is very difficult without using interrupts)
Which MCU are you using?

Martin

hippalator
Posts: 23
Joined: Tue Jul 15, 2025 9:05 pm
Has thanked: 3 times

Re: Timer or interupt?

Post by hippalator »

mnfisher wrote:
Fri Nov 21, 2025 8:26 pm
An interrupt is easier to use for real times tasks - doing things in a loop with delays gets much more complicated as the task gets harder - imagine flashing two (or more) LEDs at different intervals for example...
The mistake most commonly made it trying to do too much on the interrupt handler (ISR) - which needs to be as quick as possible.
Of course other interrupts are available too besides timer - and they allow MCUs to respond to events in 'real time' so are well worth getting the hang of 🙂 (for example reading intput from a UART is very difficult without using interrupts)
Which MCU are you using?

Martin
I'm using the 16f18877.

mnfisher
Valued Contributor
Posts: 1716
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 146 times
Been thanked: 799 times

Re: Timer or interupt?

Post by mnfisher »

Okay - a simple example that toggles a pin (set in properties - I used RD0) - every 1s - on a PIC16F18877 running on internal oscillator at 32MHz.

One of the problems with timer interrupts is generating exact times - and AI is definitely your friend here - I used Gemini to calculate the values to load into TMR0H and TMR0L (reloaded in the ToggleLED macro - and I modified the Flowcode generated interrupt code to use TMR0 in 16bit mode (the datasheet is your friend here). (See T0CON0 and T0CON1)
Using the 8bit timer and modifying pre and post-scaler allows you to get close to 1Hz (but not exactly)

For ultimate control - you can use the custom ISR too.

My oscilloscope reckons 999ms per pulse :-(

Martin
Attachments
PIC16F18877_Blink.fcfx
(10.24 KiB) Downloaded 10 times

hippalator
Posts: 23
Joined: Tue Jul 15, 2025 9:05 pm
Has thanked: 3 times

Re: Timer or interupt?

Post by hippalator »

Thank you for taking the time.
Much appreciated!

I'm new to the programming world.
You're work is levels above my pay grade.
After looking at your code and doing some research and using a little bit of AI help I am getting a much clearer insight at coding and respect for Flowcode.
Your typical Google AI, though helpful, does not give FC justice.
I don't have the knowledge or experience at traditional coding so Flowcode has been leaps for me and certainly helps at wrapping my head around coding.
I've been reading the forum, new and old, and watching a lot of Youtube videos but most of those YT videos are using older versions.
I'm using V10 and trying to navigate V10 after watching video's using V7 or V8 adds to the newby fustration.

Again thanks for the reply and effort.
Much appreciated.

medelec35
Matrix Staff
Posts: 2192
Joined: Wed Dec 02, 2020 11:07 pm
Has thanked: 665 times
Been thanked: 744 times

Re: Timer or interupt?

Post by medelec35 »

An easy way of setting a timer interrupt up is by using the Timed Interval component (Component Libraries > Math > Timing)
No calculations are necessary :-)
All you need to do is first add a new User Macro for the timer to call when interrupt is triggered.
Then within properties, select the time unit (Days, Hours, Minutes. Seconds, Milliseconds, or Microseconds)
Enter Time Period then finally Use the Callback Macro to select the new User Macro that was created.

Finally Drag the StartTimerInterval component function on to the start of Main (or anywhere suitable).
Martin

mnfisher
Valued Contributor
Posts: 1716
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 146 times
Been thanked: 799 times

Re: Timer or interupt?

Post by mnfisher »

And there is always something new to learn :-)

Keep playing - and practicing - Flowcode is a great environment to do it in!

Martin

Post Reply