Advice on Hardware Interrupts

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
RyanPierce
 
Posts: 11
Joined: Thu Aug 23, 2012 7:12 pm

Advice on Hardware Interrupts

Post by RyanPierce »

I'm interested in having an Arduino count pulses from an old civil defense Geiger counter via an opto-isolator. My first thought: fire a hardware interrupt. A simple ISR just increments a volatile counter. My loop() would check the time and periodically record the counts, derive CPM, convert to microsieverts / hour, etc. But I'd also like to have some kind of user interface and display, say, an LCD or TFT shield. And the libraries for these often call delay functions that disable interrupts. This got me thinking about interrupts.

1. If an interrupt is triggered while the ISR is running, what happens? Does it get dropped? Interrupt the ISR? Or is it somehow queued and the ISR executed after the ISR terminates?

2. If interrupts are disabled, and the interrupt pin gets triggered, what happens? Is the interrupt dropped? Or does it somehow queue, and execute the ISR once interrupts are enabled?

3. For #1 and #2, assuming queuing happens, and an interrupt is queued, and N more interrupts get triggered while interrupts are blocked, then will the ISR run once or N+1 times once whatever is blocking things continues?

In other words, I'm worried about missing pulses. Now Geiger tubes often have at least 100 us dead time, so I'm sure the ISR can easily conclude before the next interrupt arrives. But I'm not so sure how long it takes to drive LCDs or TFTs.

Also, an unrelated question:

I'm seriously considering the 1.8" TFT or the 2.8" TFT touch shield. I'd like to have a left-scrolling line graph of radiation level. Is there any way to have the TFT shift a rectangular block of pixels left? Doing a loop over reading and writing 20k or 76k pixels seems like it could take a lot of time.

Thanks,
Ryan

User avatar
adafruit_support_bill
 
Posts: 88088
Joined: Sat Feb 07, 2009 10:11 am

Re: Advice on Hardware Interrupts

Post by adafruit_support_bill »

Interrupts are disabled upon entry to the ISR and are not queued. In general, you should keep the ISR processing to a minimum. In this case, it can be as simple as incrementing a volatile pulse counter which is processed in the main loop. In no case should you ever try to update a display from within the ISR.

Display processing does not need to happen at interrupt-speed. Neither LCD nor human vision are not capable of keeping up with that. Count the pulses in the ISR and update your display from the main loop.

User avatar
westfw
 
Posts: 2008
Joined: Fri Apr 27, 2007 1:01 pm

Re: Advice on Hardware Interrupts

Post by westfw »

While external interrupts (and internal, for that matter) are not "queued", I'm pretty sure that they ARE "latched." The datasheet says that "pulses more than one clock period will generate an interrupt."

That means that if ONE pulse arrives during your ISR, it should be detected and cause another interrupt invocation after the current ISR returns. But multiple pulses received during the ISR would only generate one additional interrupt. Also, if interrupts are globally disabled, a particular hardware interrupt can still get latched, causing an interrupt immediate after interrupts are re-enabled (this follows directly from the first behavior.)

I'm not sure whether peripherals set their interrupt flags when the peripheral's interrupts are not enabled; that may depend on the specific peripheral.

tldr
 
Posts: 466
Joined: Thu Aug 30, 2012 1:34 am

Re: Advice on Hardware Interrupts

Post by tldr »

westfw wrote:I'm not sure whether peripherals set their interrupt flags when the peripheral's interrupts are not enabled; that may depend on the specific peripheral.
seems like for most interrupts, if the peripheral is enabled, the flag is set whether interrupts are enabled or not. this allows you to poll the interrupt flag if you so desire. if you are using the interrupt flag in this manner you need to write a logic one to it to clear it. why write a one to a bit to clear it? i don't recall, but it's explained in a few places and, if i recall correctly, the explanations made sense to me at the time i read them...

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”