Beispiel #1
0
        private static void WriteByte(Mcp23x1x mcp23x1x)
        {
            // This assumes the device is in default Sequential Operation mode.
            Console.WriteLine("Write Individual Byte");

            Register register = Register.IODIR;

            void IndividualRead(Mcp23xxx mcp, Register registerToRead)
            {
                byte dataRead = mcp23x1x.ReadByte(registerToRead, Port.PortB);

                Console.WriteLine($"\tIODIRB: 0x{dataRead:X2}");
            }

            Console.WriteLine("Before Write");
            IndividualRead(mcp23x1x, register);

            mcp23x1x.WriteByte(register, 0x12, Port.PortB);

            Console.WriteLine("After Write");
            IndividualRead(mcp23x1x, register);

            mcp23x1x.WriteByte(register, 0xFF, Port.PortB);

            Console.WriteLine("After Writing Again");
            IndividualRead(mcp23x1x, register);
        }
Beispiel #2
0
        private static void ReadSwitchesWriteLeds(Mcp23x1x mcp23x1x)
        {
            Console.WriteLine("Read Switches & Write LEDs");

            // Input direction for switches.
            mcp23x1x.WriteByte(Register.IODIR, 0b1111_1111, Port.PortA);
            // Output direction for LEDs.
            mcp23x1x.WriteByte(Register.IODIR, 0b0000_0000, Port.PortB);

            while (true)
            {
                // Read switches.
                byte data = mcp23x1x.ReadByte(Register.GPIO, Port.PortA);
                // Write data to LEDs.
                mcp23x1x.WriteByte(Register.GPIO, data, Port.PortB);
                Console.WriteLine(data);
                Thread.Sleep(500);
            }
        }