Immortalpr1nce
Posts: 6
Joined: Fri Aug 05, 2022 11:35 pm

spi help

Sat Aug 06, 2022 12:06 am

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)

ghp
Posts: 2558
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: spi help

Sat Aug 06, 2022 7:10 am

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.

Immortalpr1nce
Posts: 6
Joined: Fri Aug 05, 2022 11:35 pm

Re: spi help

Mon Aug 08, 2022 4:20 pm

ghp wrote:
Sat Aug 06, 2022 7:10 am
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.
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.

Thanks again

ghp
Posts: 2558
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: spi help

Tue Aug 09, 2022 5:02 am

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.

Immortalpr1nce
Posts: 6
Joined: Fri Aug 05, 2022 11:35 pm

Re: spi help

Tue Aug 09, 2022 7:19 pm

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)

cleverca22
Posts: 6317
Joined: Sat Aug 18, 2012 2:33 pm

Re: spi help

Tue Aug 09, 2022 7:47 pm

what is the name of the file you put this code into?

Immortalpr1nce
Posts: 6
Joined: Fri Aug 05, 2022 11:35 pm

Re: spi help

Tue Aug 09, 2022 8:48 pm

max_test.py

cleverca22
Posts: 6317
Joined: Sat Aug 18, 2012 2:33 pm

Re: spi help

Tue Aug 09, 2022 8:49 pm

not sure what it could be then

could you post more of the error, and use code tags?

ghp
Posts: 2558
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany

Re: spi help

Wed Aug 10, 2022 9:17 am

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

Code: Select all

wget https://raw.githubusercontent.com/muccc/py-max7301/master/max7301/max7301.py
- edit the file max7301.py, in line 49 change
from

Code: Select all

    def _get_conf_register_for_pin(self, pin):
        return (pin-4)/4 + 0x09
to

Code: Select all

    def _get_conf_register_for_pin(self, pin):
        return (pin-4)//4 + 0x09 # note the double //
- 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 ?

Immortalpr1nce
Posts: 6
Joined: Fri Aug 05, 2022 11:35 pm

Re: spi help

Wed Aug 10, 2022 3:32 pm

ghp wrote:
Wed Aug 10, 2022 9:17 am
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

Code: Select all

wget https://raw.githubusercontent.com/muccc/py-max7301/master/max7301/max7301.py
- edit the file max7301.py, in line 49 change
from

Code: Select all

    def _get_conf_register_for_pin(self, pin):
        return (pin-4)/4 + 0x09
to

Code: Select all

    def _get_conf_register_for_pin(self, pin):
        return (pin-4)//4 + 0x09 # note the double //
- 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 ?
awesome i will try that as soon as i can. it this what you where looking for? https://www.maximintegrated.com/en/prod ... X7301.html

Immortalpr1nce
Posts: 6
Joined: Fri Aug 05, 2022 11:35 pm

Re: spi help

Mon Aug 15, 2022 8:13 pm

ghp wrote:
Wed Aug 10, 2022 9:17 am
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

Code: Select all

wget https://raw.githubusercontent.com/muccc/py-max7301/master/max7301/max7301.py
- edit the file max7301.py, in line 49 change
from

Code: Select all

    def _get_conf_register_for_pin(self, pin):
        return (pin-4)/4 + 0x09
to

Code: Select all

    def _get_conf_register_for_pin(self, pin):
        return (pin-4)//4 + 0x09 # note the double //
- 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 ?
Thank you!! this worked.

Return to “Python”