轮询多个 GPIO

物联网 传感器 微软视窗物联网 GPIO
2021-06-26 12:58:11

我正在尝试使用 Dragonboard 410c 上的 Windows 10 IoT 从连接到温度传感器和光电二极管的微芯片板上每秒提取几个字节。当我尝试一次从多个 GPIO 读取时,似乎存在干扰。如何在不弄乱时钟信号的情况下从这些 GPIO 获取输入?

namespace DashWall
{
    public sealed partial class MainPage : Page
    {
        private const int CLOCK_PIN = 12;
        private const int TEMP_PIN = 34;
        private const int PHOTO_PIN = 33;
        private const int START_PIN = 36;

        private GpioPin clockPin;
        private GpioPinValue clockPinValue;
        private GpioPin tempPin;
        private GpioPinValue tempPinValue;
        private GpioPin photoPin;
        private GpioPinValue photoPinValue;
        private GpioPin startPin;
        private GpioPinValue startPinValue;

        private DispatcherTimer timer;

        private GpioController gpio;

        public MainPage()
        {
            InitializeComponent();

            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(10);
            timer.Tick += Timer_Tick;

            InitGpio();
            timer.Start();
        }

        private void InitGpio()
        {
            gpio = GpioController.GetDefault();

            clockPin = gpio.OpenPin(CLOCK_PIN);
            tempPin = gpio.OpenPin(TEMP_PIN);
            photoPin = gpio.OpenPin(PHOTO_PIN);
            startPin = gpio.OpenPin(START_PIN);

            clockPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            tempPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            photoPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
            startPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
        }

        bool isReading = false;
        bool bitRead = false;
        uint curPhoto;
        uint curTemp;
        uint bitCount = 0;
        bool start;
        bool clock;
        bool photo;
        bool temp;
        private void Timer_Tick(object sender, object e)
        {
            handleInput();
            Time.Text = "" + clock;
            Temp.Text = "" + temp;

            if (start && clock && !isReading)
            {
                Temp.Text = "" + start;
                isReading = true;
                curPhoto = 0;
                curTemp = 0;
            }

            if (isReading)
            {
                if (clock && !bitRead)
                {
                    ReadBit();
                    bitRead = true;
                }
                if (!clock)
                {
                    bitRead = false;
                }
            }
        }

        private void ReadBit()
        {
            uint photoBit = 0;
            uint tempBit = 0;
            if (photo) { photoBit = 1; }
            if (temp) { tempBit = 1; }
            curPhoto = curPhoto << 1;
            curPhoto = curPhoto + photoBit;

            curTemp = curTemp << 1;
            curTemp = curTemp + tempBit;

            bitCount++;

            if (bitCount >= 8)
            {
                isReading = false;
                bitCount = 0;

                //Time.Text = "" + Convert.ToString(curPhoto, 2);
                //Temp.Text = "" + curTemp;
            }
        }

        private void handleInput()
        {
            clockPinValue = clockPin.Read();
            clock = clockPinValue == GpioPinValue.High;

            startPinValue = startPin.Read();
            start = startPinValue == GpioPinValue.High;

            photoPinValue = photoPin.Read();
            photo = photoPinValue == GpioPinValue.High;

            tempPinValue = tempPin.Read();
            temp = tempPinValue == GpioPinValue.High;
        }

    }
}
1个回答

结果发现两个微控制器之间的地线已经脱落。把它放回原处解决了我的问题。