Page 1 of 1
Timer or interupt?
Posted: Fri Nov 21, 2025 8:14 pm
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.
Re: Timer or interupt?
Posted: Fri Nov 21, 2025 8:16 pm
by hippalator
Having a difficult time learning interupts.
Not much out there on interupts.
Any links anyone know of?
Re: Timer or interupt?
Posted: Fri Nov 21, 2025 8:26 pm
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
Re: Timer or interupt?
Posted: Fri Nov 21, 2025 9:33 pm
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.
Re: Timer or interupt?
Posted: Fri Nov 21, 2025 11:00 pm
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