简体   繁体   English

GPIO 输入的延长脉冲 C# UWP

[英]prolonged impulse for GPIO input C# UWP

A digital acoustic sensor is connected to my raspberry.一个数字声学传感器连接到我的覆盆子。
Everytime it detects sound, it sets the input HIGH.每次检测到声音时,都会将输入设置为高电平。 But it only appears to be active for a couple of milliseconds.但它似乎只活跃了几毫秒。
How can i prolong the signal within my program that it stays up for 500ms?如何延长程序中的信号保持 500 毫秒?
It is a functionality which I know from PLC contollers.这是我从 PLC 控制器中了解到的一项功能。

Diagram图表
Start: sensor input开始:传感器输入
Q: prolonged signal Q:信号延长

图表
, ,

Here my approach with the answer of Michael:这是我对迈克尔的回答的方法:
But still, it doesn't hold for Task.Delay.但是,它仍然不适用于 Task.Delay。 It goes off directly.它直接熄灭。

    public MainPage()
    {
        this.InitializeComponent();
        GPIOinit();
        Serial();
    }


    private void GPIOinit()
    {
        const int PIN_AKUSTIK = 19;

        GpioController gpio = GpioController.GetDefault();  //GPIO-Controller mit Default belegen
        pin_akustik = gpio.OpenPin(PIN_AKUSTIK);
    }


    public async void Serial() 
    {
      //serial configuration

        while (true)
        {
            //frequency of constant while loop 300ms
            Sensor_Akustik();
        }
    } 




    public void Sensor_Akustik()
    {
        pin_akustik.ValueChanged += Pin_ValueChanged;
        pin_akustik.SetDriveMode(GpioPinDriveMode.Input);

         status_akustik = pin_akustik.Read().ToString();
        textblock_DebugAkustik_live.Text = status_akustik;
        Messenger.Default.Send<string, Page_Akustik>(status_akustik);
    }

    private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
    {
        if (args.Edge == GpioPinEdge.RisingEdge)
        {
            sender.Write(GpioPinValue.High);

            Task.Delay(3000).Wait();
        }
    }

You might refer to following code.您可以参考以下代码。 You can try to update the latched output value for the pin if the pin is configured as an input.如果引脚配置为输入,您可以尝试更新引脚的锁存 output 值。

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

        // Show an error if there is no GPIO controller
        if (gpio == null)
        {
            pin = null;
            GpioStatus.Text = "There is no GPIO controller on this device.";
            return;
        }

        pin = gpio.OpenPin(LED_PIN);
        pin.ValueChanged += Pin_ValueChanged;
        pin.SetDriveMode(GpioPinDriveMode.Input);

    }


    private void Pin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
    {
        if(args.Edge == GpioPinEdge.RisingEdge)
        {
            sender.Write(GpioPinValue.High);
            Task.Delay(500).Wait();
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM