2

WOOHOO! MY first official foray into micro controllers!

Its a lot of fun sofar but I am a bit stuck at the following:

I'd like to read the status of PINs configured as Output, in other words, Did I set the pin to High or Low?

I've built my own source in python 2.7 and used the examples I got from ControlEverything.com (bought the entire board from them). I'm using smbus and in short I've set the entire Port-A bank to Output, then riased them to High. I can see my LED turn on at this point.

import smbus

bus = smbus.SMBus(1)

bus.write_byte_data(0x20, 0x00, 0x00) #set Port-A as output
bus.write_byte_data(0x20, 0x12, 0xFF) #set Port-A to High
#LED turns on

status = bus.read_byte_data(0x20,0x12) # read the bus I just set to 0XFF
print "%s return value: %d"  %("0x12",status)

At this point I always get 0 as my return val, although my LED is burning brightly.

Am I reading the wrong register or is there more at play here?

2 Answers 2

1

For future reference (for people starting out, like myself) the problem was solved when I properly set the addresses on the A0 - A2 pins. Once that was set up I could see a change on the GPPU pins from 1 1 1 1 1 1 1 1 to 1 0 1 1 1 1 1 1 when there is an input.

Do'h

0

There is a difference between reading GPIOA and OLATA. This is just an observation, I don't know the precise rules.

I have LEDs connected to each output of port A.

Result of writing 0 to 255 to GPIOA and reading back GPIOA.

GPIOA (0-255)
 0  0  0  2  0  4  4  6  0  8  8  a  8  c  c  e 
10 10 10 12 10 14 14 16 10 18 18 1a 18 18 18 18 
20 20 20 22 20 24 24 26 20 28 28 2a 28 28 28 28 
20 20 20 22 20 20 20 20 20 28 28 28 28 28 28 28 
40 40 40 42 40 44 44 46 40 48 48 4a 48 4c 4c 4c 
40 40 40 42 40 40 40 40 40 48 48 48 48 48 48 48 
40 40 40 42 40 40 40 40 40 48 48 48 48 48 48 48 
40 40 40 40 40 40 40 40 40 48 48 48 48 48 48 48 
80 80 80 82 80 84 84 86 80 88 88 8a 88 88 88 88 
80 80 80 82 80 80 80 80 80 88 88 88 88 88 88 88 
80 80 80 82 80 84 84 84 80 88 88 88 88 88 88 88 
80 80 80 80 80 80 80 80 80 88 88 88 88 88 88 88 
c0 c0 c0 c2 c0 c4 c4 c4 c0 c8 c8 ca c8 c8 c8 c8 
c0 c0 c0 c0 c0 c0 c0 c0 c0 c8 c8 c8 c8 c8 c8 c8 
c0 c0 c0 c2 c0 c0 c0 c0 c0 c8 c8 ca c8 c8 c8 c8 
c0 c0 c0 c0 c0 c0 c0 c0 c0 c8 c8 c8 c8 c8 c8 ca 

Result of writing 0 to 255 to GPIOA and reading back OLATA.

OLATA (0-255)
 0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 
10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 
20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 
30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 
40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 
50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 
60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 
70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 
80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 
90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f 
a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af 
b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf 
c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf 
d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df 
e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef 
f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff 

Code used.

#!/usr/bin/env python

# MCP23017-readback.py

import pigpio

IODIRA=0
GPIOA=18
OLATA=20

pi = pigpio.pi()

if not pi.connected:
   exit()

h = pi.i2c_open(1, 0x27) # Get handle to MCP23017 on I2C bus 1 at 0x27.

pi.i2c_write_byte_data(h, IODIRA, 0) # Set port A as output.

str_GPIOA = ""
str_OLATA = ""

for i in range(256):

   pi.i2c_write_byte_data(h, GPIOA, i) # Write i to port A.

   _GPIOA = pi.i2c_read_byte_data(h, GPIOA)
   _OLATA = pi.i2c_read_byte_data(h, OLATA)

   str_GPIOA += "{:2x} ".format(_GPIOA)
   str_OLATA += "{:2x} ".format(_OLATA)

   if (i % 16) == 15:
      str_GPIOA += "\n"
      str_OLATA += "\n"

print("GPIOA (0-255)\n"+str_GPIOA)
print("OLATA (0-255)\n"+str_OLATA)

pi.i2c_close(h)

pi.stop()
1
  • Gave it a bash: The results from my program is is still 0 MCP27013 A Register IODIR set to [0 0 0 0 0 0 0 0] MCP27013 A Register GPIO set to [1 1 1 1 1 1 1 1] MCP27013 A Register OLAT raw value: 0 MCP27013 A Register OLAT read as [0 0 0 0 0 0 0 0] I've just wrtten a bit to convert the returned values to list[] for my own sanity.
    – ShadowedR
    Commented Oct 1, 2016 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.