Пример #1
0
        private void Send4Bits(byte control, byte d)
        {
            byte portB = control;

            portB |= FlipAndShift(d);
            portB |= ENABLE_BIT;

            if (register == Register.GPIOA)
            {
                expander.Write(MCP23017.Register.GPIOA, portB);
            }
            else
            {
                expander.Write(MCP23017.Register.GPIOB, portB);
            }

            portB &= 0xDF;
            if (register == Register.GPIOA)
            {
                expander.Write(MCP23017.Register.GPIOA, portB);
            }
            else
            {
                expander.Write(MCP23017.Register.GPIOB, portB);
            }
        }
Пример #2
0
        public HD44780U(MCP23017 exp, Register reg)
        {
            expander = exp;
            register = reg;

            if (register == Register.GPIOA)
            {
                expander.Write(MCP23017.Register.IODIRA, 0x00);
                expander.Write(MCP23017.Register.GPIOA, 0x00);
            }
            else
            {
                expander.Write(MCP23017.Register.IODIRB, 0x00);
                expander.Write(MCP23017.Register.GPIOB, 0x00);
            }
        }
Пример #3
0
        public double GetDistance(Units toUnits)
        {
            double   results = 0;
            TimeSpan elapsedTime;

            Stopwatch sw  = new Stopwatch();
            Stopwatch sw2 = new Stopwatch();

            sw2.Reset();
            sw2.Start();
            sw.Reset();
            long startTicks     = 0;
            long ticksPerSecond = Stopwatch.Frequency;

            //Although the majority of the code between the Raspberry Pi GPIO
            //and the expander I/O will be the same I am separating them to
            //reduce any additional operations which could alter the timings
            if (expander == null)
            {
                rpTriggerPin.SetDriveMode(GpioPinDriveMode.Output);
                rpEchoPin.SetDriveMode(GpioPinDriveMode.Input);

                //Turn off the trigger
                rpTriggerPin.Write(GpioPinValue.Low);

                // wait for the sensor to settle
                Task.Delay(TimeSpan.FromMilliseconds(500)).Wait();

                // turn on the pulse
                rpTriggerPin.Write(GpioPinValue.High);

                // let the pulse run for 10 microseconds
                Task.Delay(TimeSpan.FromMilliseconds(.01)).Wait();

                // turn off the pulse
                rpTriggerPin.Write(GpioPinValue.Low);

                // start the stopwatch just as the echo starts
                startTicks = sw2.ElapsedTicks;
                while (rpEchoPin.Read() == GpioPinValue.Low && sw2.ElapsedTicks - startTicks < ticksPerSecond)
                {
                    ;
                }

                sw.Start();

                // stop the stopwatch when the echo stops
                startTicks = sw2.ElapsedTicks;
                while (rpEchoPin.Read() == GpioPinValue.High && sw2.ElapsedTicks - startTicks < ticksPerSecond)
                {
                    ;
                }
                sw.Stop();

                // the duration of the echo is equal to the pulse's roundtrip time
                elapsedTime = sw.Elapsed;
            }
            else
            {
                expander.SetDriveMode(ioTriggerPin, MCP23017.PinMode.Ouput);
                expander.SetDriveMode(ioEchoPin, MCP23017.PinMode.Input);

                // turn off the trigger
                expander.Write(ioTriggerPin, MCP23017.PinValue.Low);

                // wait for the sensor to settle
                Task.Delay(TimeSpan.FromMilliseconds(500)).Wait();

                // turn on the pulse
                expander.Write(ioTriggerPin, MCP23017.PinValue.High);

                // let the pulse run for 10 microseconds
                Task.Delay(TimeSpan.FromMilliseconds(.01)).Wait();

                // turn off the pulse
                expander.Write(ioTriggerPin, MCP23017.PinValue.Low);
                startTicks = sw2.ElapsedTicks;

                // start the stopwatch just as the echo starts
                while (expander.Read(ioEchoPin) == false && sw2.ElapsedTicks - startTicks < ticksPerSecond)
                {
                    ;
                }

                sw.Start();
                // stop the stopwatch when the echo stops

                startTicks = sw2.ElapsedTicks;
                while (expander.Read(ioEchoPin) == true && sw2.ElapsedTicks - startTicks < ticksPerSecond)
                {
                    ;
                }
                sw.Stop();

                // the duration of the echo is equal to the pulse's roundtrip time
                elapsedTime = sw.Elapsed;
            }

            /*Convert value to proper units
             *  Speed of sound at ground level in air
             *  13.51 inches per second
             *  1.126 ft per second
             *  34.32 centimeters per second
             *  343.2 millimeters per second
             */

            //Since the sound is sent out and bounces back we need
            //to take the elapsed time in milliseconds and divide it by 2
            results = elapsedTime.TotalMilliseconds / 2;

            switch (toUnits)
            {
            case Units.Centimeters:
                results = results * 34.32;
                break;

            case Units.Millimeters:
                results = results * 343.2;
                break;

            case Units.Feet:
                results = results * 1.126;
                break;

            case Units.Inches:
                results = results * 13.51;
                break;
            }

            return(results);
        }