예제 #1
0
class AD345:
    def __init__(self):
        self.cs_pin = Pin('X5', Pin.OUT_PP, Pin.PULL_NONE)
        self.cs_pin.high()
        self.spi = SPI(1, SPI.MASTER, baudrate=2000000, polarity=0, phase=1, bits=8)

        self.devid = self.read_id()

        if self.devid == ADXL345_DEVID_VAL:
            self.write_bytes(ADXL345_POWER_CTL_ADDR, bytearray([ADXL345_POWER_CTL_CONF]))
            self.write_bytes(ADXL345_BW_RATE_ADDR, bytearray([ADXL345_BW_RATE_CONF]))
            self.write_bytes(ADXL345_DATA_FORMAT_ADDR, bytearray([ADXL345_DATA_FORMAT_CONF]))
            self.write_bytes(ADXL345_ENABLE_MSRM_ADDR, bytearray([ADXL345_ENABLE_MSRM_CONF]))
            self.sensitivity = 32
        else:
            raise Exception('ADXL345 accelerometer not present')

    def convert_raw_to_g(self, x):
        if x & 0x80:
            
        return x * self.sensitivity / 1000

    def read_bytes(self, addr, nbytes):
        if nbytes > 1:
            addr |= READWRITE_CMD | MULTIPLEBYTE_CMD
        else:
            addr |= READWRITE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        #buf = self.spi.send_recv(bytearray(nbytes * [0])) # read data, MSB first
        buf = self.spi.recv(nbytes)
        self.cs_pin.high()
        return buf

    def write_bytes(self, addr, buf):
        if len(buf) > 1:
            addr |= MULTIPLEBYTE_CMD
        self.cs_pin.low()
        self.spi.send(addr)
        for b in buf:
            self.spi.send(b)
        self.cs_pin.high()

    def read_id(self):
        return self.read_bytes(DEVID_ADDR, 1)[0]

    def x(self):
        return self.convert_raw_to_g(self.read_bytes(DATAX1_ADDR, 1)[0])

    def y(self):
        return self.convert_raw_to_g(self.read_bytes(DATAY1_ADDR, 1)[0])

    def z(self):
        return self.convert_raw_to_g(self.read_bytes(DATAZ1_ADDR, 1)[0])

    def xyz(self):
        return (self.x(), self.y(), self.z())
예제 #2
0
from machine import Pin, SPI
from time import sleep_ms

# UEXT wiring on the Pyboard.
# https://github.com/mchobby/pyboard-driver/tree/master/UEXT
spi = SPI(2)  # MOSI=Y8, MISO=Y7, SCK=Y6, SS=Y5
spi.init(SPI.MASTER, baudrate=5000000, phase=0, polarity=0)
# We must manage the SS signal ourself
ss = Pin(Pin.board.Y5, Pin.OUT)

# Start a new SPI transaction
ss.value(1)
sleep_ms(10)
ss.value(0)

print("Write a A on the screen")
spi.send(bytes([0x80, 0x00, 0x41]))

sleep_ms(500)

# Now, read back the memory
# Start a new SPI transaction
ss.value(1)
sleep_ms(10)
ss.value(0)
spi.send(bytes([0x00, 0x00]))
v = spi.recv(1)  # read one byte
print("Response:")
print(v)  # bytes
print("0x%02x" % v[0])  # print as Hexa
예제 #3
0
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from machine import Pin, SPI
from time import sleep_ms

# UEXT wiring on the Pyboard.
# https://github.com/mchobby/pyboard-driver/tree/master/UEXT
spi = SPI(2)  # MOSI=Y8, MISO=Y7, SCK=Y6, SS=Y5
spi.init(SPI.MASTER, baudrate=5000000, phase=0, polarity=0)
# We must manage the SS signal ourself
ss = Pin(Pin.board.Y5, Pin.OUT)

# Start a new SPI transaction
ss.value(1)
sleep_ms(10)
ss.value(0)

print("Read IDCode from GameDuino (0x6d expected)")
spi.send(bytes([0x28, 0x00]))

buf = bytearray(1)
spi.recv(buf)  # read 1 byte

print("Response:")
print(buf)  # bytes
print("0x%02x" % buf[0])  # print as Hexa
예제 #4
0
from time import sleep_ms

# UEXT wiring on the Pyboard.
# https://github.com/mchobby/pyboard-driver/tree/master/UEXT
spi = SPI(2) # MOSI=Y8, MISO=Y7, SCK=Y6, SS=Y5
spi.init( baudrate=5000000, phase=0, polarity=0 )
# We must manage the SS signal ourself
ss = Pin( Pin.board.Y5, Pin.OUT )

# Start a new SPI transaction
ss.value( 1 )
sleep_ms( 10 )
ss.value( 0 )


print( "Write a A on the screen" )
spi.send( bytes([0x80,0x00,0x41]) )

sleep_ms( 500 )

# Now, read back the memory
# Start a new SPI transaction
ss.value( 1 )
sleep_ms( 10 )
ss.value( 0 )
spi.send( bytes([0x00,0x00]) )
v = spi.recv( 1 ) # read one byte
print( "Response:" )
print( v ) # bytes
print( "0x%02x" % v[0] ) # print as Hexa
예제 #5
0
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from machine import Pin, SPI
from time import sleep_ms

# UEXT wiring on the Pyboard.
# https://github.com/mchobby/pyboard-driver/tree/master/UEXT
spi = SPI(2) # MOSI=Y8, MISO=Y7, SCK=Y6, SS=Y5
spi.init( baudrate=5000000, phase=0, polarity=0 )
# We must manage the SS signal ourself
ss = Pin( Pin.board.Y5, Pin.OUT )

# Start a new SPI transaction
ss.value( 1 )
sleep_ms( 10 )
ss.value( 0 )

print( "Read IDCode from GameDuino (0x6d expected)" )
spi.send( bytes([0x28,0x00]) )

buf = bytearray(1)
spi.recv( buf ) # read 1 byte

print( "Response:" )
print( buf ) # bytes
print( "0x%02x" % buf[0] ) # print as Hexa