I had posted this topic in the SitCore forum when originally concerned if the hardware could handle pulse counting pulses in the 10nS width region. The answer is yes, but I am concerned about the tinyclr driver digitalSignal Abort function, so I am posting in the TinyCLR forum.
My object is to build a pulse counter that will work between 1kHz and 10MHz and return a valid reading every 125mS. Using the examples from the CHI website I tested this program and find that as the signal frequency decreases, the number of times that the OnReadPulseFinished fails and returns 0 for both the count, and the duration, increases.
Of course, as the frequency approaches 0, I would expect some readings to be count=0, but the duration to be >0, depending on when abort was called.
As a suggestion to making the digitalSignal ReadPulse function easier to use, being able to specify the integration time instead of the count would be nice.
using System.Collections;
using System.Text;
using System.Threading;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Native;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Devices.Signals;
using System.Diagnostics;
using System;
namespace PulseCounter20260
{
internal class Program
{
private Timer readPulseTimer;
private TimeSpan elapsed;
private Timer abortTimer;
private DateTime startTime;
private Boolean pulsesRead;
private AutoResetEvent pulseReadFinishedEvent;
static void Main()
{
var program = new Program();
program. Run();
}
private void Run()
{
pulseReadFinishedEvent = new AutoResetEvent(false);
elapsed = new TimeSpan();
startTime = DateTime.Now;
var digitalSignalPin = GpioController.GetDefault().OpenPin(SC20260.Timer.DigitalSignal.Controller2.PB3);
var digitalSignal = new DigitalSignal(digitalSignalPin);
var waitForEdge = true;
Debug.WriteLine("Program started");
digitalSignal.OnReadPulseFinished += new DigitalSignal.PulseReadEventHandler(this.Digital_OnReadPulseFinished);
pulsesRead = true;
while (true)
{
digitalSignal.ReadPulse(1000000, GpioPinEdge.RisingEdge, waitForEdge);
Thread.Sleep(125);
digitalSignal.Abort();
}
}
private void Digital_OnReadPulseFinished(DigitalSignal sender, TimeSpan duration, uint count, GpioPinValue pinValue)
{
var ticks = duration.Ticks;
if (ticks > 0)
{
var microsecond = ((double)duration.Ticks) / 10;
var freq = (count / microsecond) * 1000000;
Debug.WriteLine((DateTime.Now - this.startTime).TotalMilliseconds.ToString() + "s, count " + count.ToString() + ", freq = " + (freq / 1000.0).ToString("F3") + " kHz");
}
else
{
Debug.WriteLine("No clock found. Event failed. Ticks = " + ticks.ToString() + ", count = " + count.ToString());
}
pulsesRead = true;
}
}
}
Here is an example of the output at 15kHz, followed a higher frequency.
type or paste code
No clock found. Event failed. Ticks = 0, count = 0
628116.6317s, count 1119, freq = 15.177 kHz
No clock found. Event failed. Ticks = 0, count = 0
No clock found. Event failed. Ticks = 0, count = 0
628493.4175s, count 72, freq = 12.313 kHz
No clock found. Event failed. Ticks = 0, count = 0
628744.6264s, count 1131, freq = 14.525 kHz
Source at ~55kHz follows. Note, the pulse train temporal distribution is random as generated by a radiation detector. At the higher frequency, the results give excellent agreement with both a logic analyzer adn a Tek counter/timer/analyzer. There is no change in rise time between the two frequencies.
5958.7437s, count 5402, freq = 54.394 kHz
6084.742s, count 5416, freq = 55.499 kHz
6210.7421s, count 5804, freq = 54.831 kHz
6336.7449s, count 4673, freq = 56.098 kHz
6462.744s, count 5371, freq = 54.915 kHz
6588.7434s, count 5482, freq = 55.507 kHz
6714.7436s, count 4904, freq = 55.403 kHz
6840.7415s, count 5216, freq = 56.944 kHz
6966.741s, count 5645, freq = 54.874 kHz
All suggestions appreciated!