Strategy for measuring ADC input and using result in interrupt

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi guys!

 

MCU: Xmega128D4

 

I'm working on a product where I have to toggle a pin whenever a ADC value is below a set value. This toggling must have a response time of less than 100ms, so right now I'm having a timer interrupt firing every 10ms in which I measure the ADC input and assert/deasserts the pin. My main loop MAY take more than 100ms, so I can't do the measurements there.

 

The ADC is by far the most time consuming thing going on in this interrupt, so I was hoping to solve this differently. I use the ADC for other inputs as well, but they are not that time critical. Any tips for making the ISR less time consuming? 

 

Thanks
 

Henrik

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, if you have a spare ADC input, use that for the once every 10 mSec ADC.

Note that if one of the other ADC's in the Main Loop is reading the same input signal, you can still have two ADC's connected to the same signal, if needed.

In your ISR read the value of the ADC, and then trigger the next ADC sampling.

In this manner every time you enter the ISR you already have an ADC value with which to make your comparison and set/clear the I/O pin.

Your ADC value will lag 10 mSec behind "real time", but that isn't a problem when you have a 100 mSec response time.

 

I don't recall if the D4 model has a DAC and Analog Comparator or not.

Another option would be to feed the input signal into the Analog comparator and then just read its state during your 10 mSec ISR.

In this case there would also be no delay once entering the ISR, as again the value would be ready for you.

 

If the Threashold is known, and fixed, you can set it up with a resistive divider, (and cap).

You could also set it with the DAC, if the D4 has one.

 

Finally, you could eliminate the once every 10 mSec interrupt and have the analog comparator trigger its own interrupt when the input signal falls below the threshold.

The ISR would then set/clear the I/O pins to flag the condition.

When and how the I/O pin is reset depends upon the rest of the program.

 

JC

Last Edited: Mon. Nov 17, 2014 - 01:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would have the ADC running free and check the level in the IRQ.

 

Then assert the IO pin and set a flag, and start a timer.

in your main service loop, if you see the flag wait for the timeout and reset the IO pin

 

zero lag ( oKoK not ZERO, but fast )

Keith Vasilakes

Firmware engineer

Minnesota

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use a timer and the event system to trigger the ADC, then just interrupt off it's conversion complete vector. That way your interrupt code only has to read the result out of the register and process it. If you don't have a timer free, as Keith says free-running mode should work.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks people! I will be looking into every suggestion here. I do have a couple of available timers, so I guess it is possible to find a good solution. :)