BeaglebBoneBlack MCP23017 code no longer working.

Thread Starter

dendad

Joined Feb 20, 2016
3,924
Hi All.
I had a test program to exercise a board that plugs onto a BeagleBone Black.
Unfortunately, I swapped a couple of plugs over and fed 15V into the 5V :(
Now, I have not been able to get the code running again as it looks like all the libraries have changed. As I've mentioned on this forum before, "My favorite programming language is solder" and so I can't figure out how to fix this.
Originally, the BBB was programmed a couple of years ago and this code took me a week or so to get working. I was so happy to get it going as it is my first Python attempt.
But now it does not work with the latest official BBB Debian download.
Here is the code...
(I just noticed pasting it got rid of all the spacing. The indenting was not the problem)
EDIT: Yay! I just figured out how to add the "CODE" tags :)
Now just some of the indenting is missing, but maybe it is ok.


Python:
# run as unbuffered...
# python -u OMCB_IO_9.py
# same as version 5 but much longer loop.
# also added counter reset to each loop so I can see the FET switching.
# try to read input and compare it to expected result.
# add error counter

import Adafruit_BBIO.ADC as ADC
ADC.setup()
import Adafruit_BBIO.PWM as PWM
import time
import smbus
from mcp23017 import MCP23017
mcp=MCP23017()

mcp.set_iodira(0x3f) # 8-13 inputs, 14 & 15 outputs
mcp.set_iodirb(0xff) # 0-7 inputs
mcp.set_gpioa(0x3f)  # 8-13 high, 14 & 15 low
mcp.set_gpiob(0xff)  # 0-7 high
mcp.set_gppua(0x3f)  # pullups 8-13
mcp.set_gppub(0xff)  # pullups 0-7

mcp.set_olata(0)  # both FETs off
mcp.set_olata(64)  # FET2 on = reset counter
time.sleep(.01)
mcp.set_olata(0)  # both FETs off

# enable PWM but with motor off.
#optionally, you can set the frequency as well as the polarity from their defaults:
PWM.start("P9_14", 0, 2000, 1)
PWM.start("P9_16", 0, 2000, 1)

error_count ==0
for j in range(1000000):  # a loop sounter
  if (j&31) == 0:  # wink the reset?
  mcp.set_olata(64)  # FET2 on = reset counter
  time.sleep(.01)
  mcp.set_olata(0)  # both FETs off.
  Expected = 0x3ffe  # expected read back of inputs.


  print('\r'),

  PA = (mcp.get_gpioa())
  PB = (mcp.get_gpiob())
  PX = PB + (PA * 256)
  if (PX == Expected):

  print("PortA="),
  print'{:06b}'.format (PA),  # print PortA inputs
  print("  PortB="),
  print'{:08b}'.format (PB),  # printb PortB inputs

  print("  Anl1="),
  print'{:4.2f}'.format (23.8*(ADC.read("AIN0"))),  #print Analog 1 input
  print("  Anl2="),
  print'{:4.2f}'.format (23.8*(ADC.read("AIN1"))),  #print Analog 2 input
  print("  Supply="),
  print'{:2.2f}'.format (35.9*(ADC.read("AIN2"))),  #print Volts
  print("V "),
  print("  Motor_I="),
  print'{:4.2f}'.format (3.125*(ADC.read("AIN3"))), #print AUX cutrrent
  else:
  error_count = error_count + 1
  print
  print("Expected="),
  print'{:14b}'.format (Expected),
  print("  Read="),
  print'{:14b}'.format (PX),
  print(" Errors="),
  print error_count

  # ramp the AUX PWM up and down, switching the o/p FETS as indicators.
  if (j&192) == 0:
  PWM.start("P9_14",((j&63)*1.56),2000,1)  # Motor fwd, ramp up
  PWM.start("P9_16",0,2000,1)
  if (j&192) == 64:  # FET1 on
  PWM.start("P9_14",((63-(j&63))*1.56),2000,1) #Motor fwd, ramp down
  PWM.start("P9_16",0,2000,1)
  if (j&192) == 128:  # FET2 on
  PWM.start("P9_14",0,2000,1)  # Motor rev, ramp up
  PWM.start("P9_16",((j&63)*1.56),2000,1)
  if (j&192) == 192:
  PWM.start("P9_14",0,2000,1)  # Motor rewv, ramp down.
  PWM.start("P9_16",((63-(j&63))*1.56),2000,1)

  if (j&1) == 1:  # inc input sounter every 2 loops
  mcp.set_olata(128)  # FET1 on = pulse the counter
  time.sleep(.01)
  mcp.set_olata(0)  # both FETs off
  Expected = (((Expected * 2) +1)& 0x3fff)
  time.sleep(.1)
PWM.stop("P9_14")  # PWM off.
PWM.stop("P9_16")


Getting...

debian@beaglebone:~$ python -u OMCB_IO_9.py
Traceback (most recent call last):
File "OMCB_IO_9.py", line 13, in <module>
from mcp23017 import MCP23017
ImportError: No module named mcp23017
debian@beaglebone:~$

I cannot find a module named mcp23017 to figure out how to load it anyway. And I don't remember how I built the code in the first place.

This board has 14 inputs and 2 outputs.
As well as 4 analog inputs and a motor driver.
It is plugged into a protoboard setup with a 14bit counter and a one off 14 output to hook back to the inputs.
OMCDB_TestRig.jpg
The test code pulses one o/p to reset the counters on the protoboard.
The other o/p is used to clock the counter.
The counter o/ps are decoded to give a single o/p that operates each input on the tested board in turn.
Then the code compares the count with the expected input and displays the result and a line feed if there is an error. No line feed if all is ok.
Also, it reads and displays the 4 analog inputs....
Batt V, Motor I and 2 inputs.
The motor is ramped up and down both directions.
This worked very well to test my boards until I blew the BBB up. The code was on the internal flash. In future, it will be on the external SD card and I will image it for a backup first !!!!

I've spent another week trying to fix this but with no success.
It does not help much that I don't really understand the import and setup stuff at all. A friend of mine had a look too and he says all the old libs have been broken.
I cannot find how to drive the MCP23017 anymore.
I'll be interested to see if anyone has a solution I can understand and implement :)
 

Attachments

Last edited:
Top