Hello, I'm new to pi and programming in general. I have a pi 3 b+ I have successfully got an OS on it as well as successfully turned on spi and installed spidev. I tried to do a little programming where I can communicate with a GPIO specifically an MAX7301. If possible I want to have a program where i run the program and it connects to the gpio and have the pi ask what i want to send to the chip so i can send different addresses to try different functions of the chip. is anyone able to help me with this or able to point me in the proper direction. below is what i have so far from the web(Takaitra). i was thinking maybe input commands or msg but can not find how to use/incorperate them properly. any help would be appreciated thanks
#!/usr/bin/python
import spidev
import time
spi = spidev.SpiDev()
spi.open(0, 0)
spi.max_speed_hz = 15600000
spi2 = spidev.SpiDev()
spi2.open(0, 1)
spi2.max_speed_hz = 15600000
# Split an integer input into a two byte array to send via SPI
def write_pot(input):
msb = input >> 8
lsb = input & 0xFF
spi.xfer([msb, lsb])
# Repeatedly switch a MCP4151 digital pot off then on
while True:
write_pot(0x1FF)
time.sleep(0.5)
write_pot(0x00)
time.sleep(0.5)
-
- Posts: 6
- Joined: Fri Aug 05, 2022 11:35 pm
Re: spi help
For the max7301 there is a package available.
https://github.com/muccc/py-max7301
Consult the datasheet https://datasheets.maximintegrated.com/ ... AX7301.pdf for the details on how to use the registers.
https://github.com/muccc/py-max7301
Consult the datasheet https://datasheets.maximintegrated.com/ ... AX7301.pdf for the details on how to use the registers.
-
- Posts: 6
- Joined: Fri Aug 05, 2022 11:35 pm
Re: spi help
Thanks looking at it (again not alot of python experience) does it test all the pins? require input? i see the registers on the max datasheet.ghp wrote: ↑Sat Aug 06, 2022 7:10 amFor the max7301 there is a package available.
https://github.com/muccc/py-max7301
Consult the datasheet https://datasheets.maximintegrated.com/ ... AX7301.pdf for the details on how to use the registers.
Thanks again
Re: spi help
There is a sample program available on the github page. It explains how to read or write GPIO for this chip. If you have a look at the max7301.py file, there are commands for reading registers and writing registers. You can use these methods for your own purpose.
-
- Posts: 6
- Joined: Fri Aug 05, 2022 11:35 pm
Re: spi help
ok one last question when i try to run the sample file below it says " cannot import name 'MAX7301' from partially initialized module 'max7301' (most likely due to a circular import) any idea how i can fix that? thanks!
import max7301
import time
device = max7301.MAX7301()
device.set_pin_as_output(30)
device.set_pin_as_input(31, pullup = False)
while 1:
device.set_pin(30, 1)
time.sleep(0.1)
print "Pin 31 =", device.get_pin(31)
device.set_pin(30, 0)
time.sleep(0.1)
print "Pin 31 =", device.get_pin(31)
import max7301
import time
device = max7301.MAX7301()
device.set_pin_as_output(30)
device.set_pin_as_input(31, pullup = False)
while 1:
device.set_pin(30, 1)
time.sleep(0.1)
print "Pin 31 =", device.get_pin(31)
device.set_pin(30, 0)
time.sleep(0.1)
print "Pin 31 =", device.get_pin(31)
-
- Posts: 6317
- Joined: Sat Aug 18, 2012 2:33 pm
Re: spi help
what is the name of the file you put this code into?
-
- Posts: 6
- Joined: Fri Aug 05, 2022 11:35 pm
Re: spi help
max_test.py
-
- Posts: 6317
- Joined: Sat Aug 18, 2012 2:33 pm
Re: spi help
not sure what it could be then
could you post more of the error, and use code tags?
could you post more of the error, and use code tags?
Re: spi help
Looks like an installation problem and I guess this module is somewhat outdated.
Let me propose a different procedure which makes this module available:
- uninstall mcp7301 using "pip uninstall max7301"
- navigate to the directory where your test program is located
- execute
- edit the file max7301.py, in line 49 change
from
to
- in the sample code from the web page, change the print statements from python2 syntax to python3 syntax
from: print "Pin 31 =", device.get_pin(31)
to: print ("Pin 31 =", device.get_pin(31)) # note the added open, close brackets
There are two print statements, change both
- execute the test code: python3 yourtestcode.py (insert your file name here)
- perhaps it is needed to enable SPI in raspi-config
Could not find a breakout for this chip. Could you provide a link to a vendor ?
Let me propose a different procedure which makes this module available:
- uninstall mcp7301 using "pip uninstall max7301"
- navigate to the directory where your test program is located
- execute
Code: Select all
wget https://raw.githubusercontent.com/muccc/py-max7301/master/max7301/max7301.py
from
Code: Select all
def _get_conf_register_for_pin(self, pin):
return (pin-4)/4 + 0x09
Code: Select all
def _get_conf_register_for_pin(self, pin):
return (pin-4)//4 + 0x09 # note the double //
from: print "Pin 31 =", device.get_pin(31)
to: print ("Pin 31 =", device.get_pin(31)) # note the added open, close brackets
There are two print statements, change both
- execute the test code: python3 yourtestcode.py (insert your file name here)
- perhaps it is needed to enable SPI in raspi-config
Could not find a breakout for this chip. Could you provide a link to a vendor ?
-
- Posts: 6
- Joined: Fri Aug 05, 2022 11:35 pm
Re: spi help
awesome i will try that as soon as i can. it this what you where looking for? https://www.maximintegrated.com/en/prod ... X7301.htmlghp wrote: ↑Wed Aug 10, 2022 9:17 amLooks like an installation problem and I guess this module is somewhat outdated.
Let me propose a different procedure which makes this module available:
- uninstall mcp7301 using "pip uninstall max7301"
- navigate to the directory where your test program is located
- execute- edit the file max7301.py, in line 49 changeCode: Select all
wget https://raw.githubusercontent.com/muccc/py-max7301/master/max7301/max7301.py
fromtoCode: Select all
def _get_conf_register_for_pin(self, pin): return (pin-4)/4 + 0x09
- in the sample code from the web page, change the print statements from python2 syntax to python3 syntaxCode: Select all
def _get_conf_register_for_pin(self, pin): return (pin-4)//4 + 0x09 # note the double //
from: print "Pin 31 =", device.get_pin(31)
to: print ("Pin 31 =", device.get_pin(31)) # note the added open, close brackets
There are two print statements, change both
- execute the test code: python3 yourtestcode.py (insert your file name here)
- perhaps it is needed to enable SPI in raspi-config
Could not find a breakout for this chip. Could you provide a link to a vendor ?
-
- Posts: 6
- Joined: Fri Aug 05, 2022 11:35 pm
Re: spi help
Thank you!! this worked.ghp wrote: ↑Wed Aug 10, 2022 9:17 amLooks like an installation problem and I guess this module is somewhat outdated.
Let me propose a different procedure which makes this module available:
- uninstall mcp7301 using "pip uninstall max7301"
- navigate to the directory where your test program is located
- execute- edit the file max7301.py, in line 49 changeCode: Select all
wget https://raw.githubusercontent.com/muccc/py-max7301/master/max7301/max7301.py
fromtoCode: Select all
def _get_conf_register_for_pin(self, pin): return (pin-4)/4 + 0x09
- in the sample code from the web page, change the print statements from python2 syntax to python3 syntaxCode: Select all
def _get_conf_register_for_pin(self, pin): return (pin-4)//4 + 0x09 # note the double //
from: print "Pin 31 =", device.get_pin(31)
to: print ("Pin 31 =", device.get_pin(31)) # note the added open, close brackets
There are two print statements, change both
- execute the test code: python3 yourtestcode.py (insert your file name here)
- perhaps it is needed to enable SPI in raspi-config
Could not find a breakout for this chip. Could you provide a link to a vendor ?