I would be cool if the was a way to create a sort analog interrupt port.
With a trashhold for a parameter.. if it changes more than the trashhold it will create an interrupt.
Is this a good idea for future firmware? Or is this technically impossible?
Analog interrupt port
Started by Flores, Aug 18 2010 09:38 PM
3 replies to this topic
#1
Posted 18 August 2010 - 09:38 PM
#2
Posted 18 August 2010 - 09:43 PM
That could definitely be done, although it would be easiest in managed code...and it would have limits as to how often the measurements happened.
GPIOs can drive interrupts at a low level, but analog inputs are measured on request (with setup time, measurement time, etc.). You could create a class which checked the value every ###ms using a timer...and then fired an event when the value passed the threshold. This would be great for room temperature sensors (perhaps check every ten seconds) and even to a limited extent for things like volume knobs (check every 50ms-100ms perhaps).
Chris
#3
Posted 24 August 2010 - 05:54 AM
I just wrote a class to do just that. I hope that it is helpful...
To use:
Let me know if you need help...
using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace AJB { public delegate void AnalogEventHandler(); class AjbAnalogInterupt { public event NativeEventHandler AnalogInterrupt; private const double SINGLE_ANALOG_VALUE = 0.00322265625; private ExtendedTimer _timer; private AnalogInput _an1; private int _analogHigh; private int _analogLow; private int _timerDelay; private InterruptCause _interruptCause; private int _readAnalogValue = 0; public enum InterruptCause { InterruptOnVoltageHighOrLow, InterruptOnVoltageHigh, InterruptOnVoltageLow, InterruptOnVoltageRead } public int CurrentAnalogValue { get { return _readAnalogValue; } } public double ConvertAnalogValueToVoltage(int anVal) { if (anVal > 0) return (SINGLE_ANALOG_VALUE * anVal); else return 0; } public AjbAnalogInterupt(Cpu.Pin anPin, int CheckAnalogMsInterval, InterruptCause interruptCause, int AnalogHighValue, int AnalogLowValue) { _timerDelay = CheckAnalogMsInterval; _interruptCause = interruptCause; _analogHigh = AnalogHighValue; _analogLow = AnalogLowValue; _timer = new ExtendedTimer(new System.Threading.TimerCallback(CheckAnalog), null, _timerDelay, _timerDelay); _an1 = new AnalogInput(anPin); } private void CheckAnalog(object o) { _readAnalogValue = _an1.Read(); switch (_interruptCause) { case InterruptCause.InterruptOnVoltageHigh: if (_readAnalogValue > _analogHigh) AnalogInterrupt(new uint(), new uint(), new DateTime()); break; case InterruptCause.InterruptOnVoltageLow: if (_readAnalogValue < _analogLow) AnalogInterrupt(new uint(), new uint(), new DateTime()); break; case InterruptCause.InterruptOnVoltageHighOrLow: if ((_readAnalogValue > _analogHigh) || (_readAnalogValue < _analogLow)) AnalogInterrupt(new uint(), new uint(), new DateTime()); break; case InterruptCause.InterruptOnVoltageRead: AnalogInterrupt(new uint(), new uint(), new DateTime()); break; } } } }
To use:
using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; using AJB; namespace NetduinoApplication1 { public class Program { static AjbAnalogInterupt anInt; public static void Main() { anInt = new AjbAnalogInterupt(Pins.GPIO_PIN_A0, 1000, AjbAnalogInterupt.InterruptCause.InterruptOnVoltageHighOrLow, 300, 30); anInt.AnalogInterrupt += new AJB.NativeEventHandler(anInt_AnalogInterrupt); while (true) {} } static void anInt_AnalogInterrupt(uint data1, uint data2, DateTime time) { Debug.Print("Returned voltage: " + anInt.ConvertAnalogValueToVoltage(anInt.CurrentAnalogValue).ToString("N4") + " volts - Raw value: " + anInt.CurrentAnalogValue.ToString()); } } }
Let me know if you need help...
#4
Posted 24 August 2010 - 09:22 AM
Gonna try this, thanks.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users