Raspberry pi 如何计算具有一定间隔时间的脉冲输入?

Raspberry pi 如何计算具有一定间隔时间的脉冲输入?,raspberry-pi,iot,windows-10-iot-core,Raspberry Pi,Iot,Windows 10 Iot Core,我正在用raspberry pi和windows 10 iot内核开发一个脉冲计数器(硬币计数器),我需要计算间隔时间为25毫秒的脉冲,如下所示: 0.05欧元-1个脉冲 0,10欧元-2个脉冲 0,20欧元-4个脉冲 0.50欧元-10个脉冲 1欧元-20个脉冲 2-40次脉冲 如下图所示: 当间隔时间不同于25毫秒时,我需要打印脉冲数(插入值) 我有以下代码: public MainPage() { this.InitializeComponent();

我正在用raspberry pi和windows 10 iot内核开发一个脉冲计数器(硬币计数器),我需要计算间隔时间为25毫秒的脉冲,如下所示:

0.05欧元-1个脉冲 0,10欧元-2个脉冲 0,20欧元-4个脉冲 0.50欧元-10个脉冲 1欧元-20个脉冲 2-40次脉冲

如下图所示:

当间隔时间不同于25毫秒时,我需要打印脉冲数(插入值)

我有以下代码:

public MainPage()
    {
        this.InitializeComponent();
        InitGPIO();
    }

    private void InitGPIO()
    {
        var gpio = GpioController.GetDefault();

        if (gpio == null)
        {
            GpioStatus.Text = "There is no GPIO controller on this device.";
        }

        coinPin = gpio.OpenPin(coin_Pin);

        if (coinPin.IsDriveModeSupported(GpioPinDriveMode.InputPullUp))
        {
            coinPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
        } else
        {
            coinPin.SetDriveMode(GpioPinDriveMode.Input);
        }

        coinPin.DebounceTimeout = TimeSpan.FromMilliseconds(25);

        coinPin.ValueChanged += coinPin_ValueChanged;

        GpioStatus.Text = "GPIO pins initialized correctly.";
    }

    private void coinPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
    {
        if (e.Edge == GpioPinEdge.FallingEdge)
        {
            counter++;
        }

        var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            if (e.Edge == GpioPinEdge.FallingEdge)
            {
                //counter++;
                var time = PulseIn(coinPin, e.Edge);
                value = (counter * 5);
                value100 = value / 100;
                //money.Text = "Eur: " + (decimal)value100 + " €";

                if (time != 25)
                {
                    money.Text = "Eur: " + (decimal)value100 + " €";
                    GpioStatus.Text = "" + time;
                } else
                {
                    GpioStatus.IsEnabled = false;
                }
                //GpioStatus.Text = "" + time + "";
            } else
            {
                ///GpioStatus.Text = "" + coinPin.DebounceTimeout;
            }


        });
    }

    private double PulseIn(GpioPin pin, GpioPinEdge edge)
    {
        var sw = new Stopwatch();

        while (edge.Equals(GpioPinEdge.RisingEdge))
        {
            //sw.Start();
        }

        sw.Start();

        if (!edge.Equals(GpioPinEdge.RisingEdge))
        {
            //sw.Stop();
        }

        sw.Stop();

        return sw.Elapsed.TotalMilliseconds;
    }


    private const int coin_Pin = 24;
    private int counter = 0;
    private double value = 0;
    private double value100 = 0;
    private GpioPin coinPin;
你能帮我吗? 非常感谢。

从您的代码中

    sw.Start();

    if (!edge.Equals(GpioPinEdge.RisingEdge))
    {
        //sw.Stop();
    }

    sw.Stop();
它实际上测量
if
语句在
sw.Start()
sw.Stop()之间的执行时间。这是没有道理的。记录下降沿到达时的
watch.appeased.totalmicrosides
,然后重新启动秒表以测量脉冲间隔时间。为此,我在
counter++
下添加了以下两行代码,删除
var-time=PulseIn(coinPin,e.Edge)
并在
GpioStatus.Text=“+timeinterval
中使用
timeinterval

            timeinterval = watch.Elapsed.TotalMilliseconds;
            watch.Restart();
以下是完整的代码段:

    private Stopwatch watch = new Stopwatch();
    private void coinPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs e)
    {
        double timeinterval = 0;
        if (e.Edge == GpioPinEdge.FallingEdge)
        {
            counter++;
            timeinterval = watch.Elapsed.TotalMilliseconds;
            watch.Restart();
        }

        var task = Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
        {
            if (e.Edge == GpioPinEdge.FallingEdge)
            {
                //var time = PulseIn(coinPin, e.Edge);
                value = (counter * 5);
                value100 = value / 100;

                if (timeinterval != 25)
                {
                    money.Text = "Eur: " + (decimal)value100 + " €";
                    GpioStatus.Text = "" + timeinterval;
                }
                //...
            }
        });
    }
您可以尝试上面的代码片段,看看它是否满足您的精度要求


注:不适合在软件级测量硬件脉冲间隔,因为软件抖动总是不可预测的

我用2枚硬币(0,2欧元和0,05欧元)的示例输入更新了问题,结果应该是0,20欧元和0,25欧元,现在显示“0,05 0,1 0,15 0,20 0,25”。。。我只需要显示总价值。如何输入0,2欧元?它不是有四个脉冲吗?