how to get mcp23017 input in binary shell 1

how to get mcp23017 input in binary shell

# assuming chip is addressed to 0x20 
# to setup
sudo i2cset -y 1 0x20 0x00 0x00
# replace 1 with zero on rev1 pi
# iodira is 0x00 and iodirb is 0x01
# setting all pins on gpa to input
# echo as binary                  address GPIOA is 0x12 and GPIOB is 0x13
echo "ibase=16;obase=2;$(i2cget -y 1 0x20 0x12 b | cut -c3-4)"|bc | BC_LINE_LENGTH=9999 bc | awk '{ printf "%08d\n", $0 }' 
# replace 1 with zero on rev1 pi
# Registers are shown on page 16 Table 3-3 of the data sheet https://ww1.microchip.com/downloads/en/devicedoc/20001952c.pdf

Here is what the above code is Doing:
1. i2cset -y 1 0x20 0x00 0x00
-y 1 is the bus number, 0x20 is the address of the chip, 0x00 is the register to write to, 0x00 is the value to write
2. i2cget -y 1 0x20 0x12 b | cut -c3-4
-y 1 is the bus number, 0x20 is the address of the chip, 0x12 is the register to read from, b is the format of the data, cut -c3-4 is to remove the 0x from the output
3. echo “ibase=16;obase=2;$(i2cget -y 1 0x20 0x12 b | cut -c3-4)”|bc | BC_LINE_LENGTH=9999 bc | awk ‘{ printf “%08d\n”, $0 }’
– echo “ibase=16;obase=2;$(i2cget -y 1 0x20 0x12 b | cut -c3-4)”|bc is to convert the hex value to binary
– BC_LINE_LENGTH=9999 bc is to make sure the output is not split into multiple lines
– awk ‘{ printf “%08d\n”, $0 }’ is to pad the output with zeros to make it 8 digits long

Similar Posts