Hey,
i am not 100% sure what you are thinking of.
I am writing and reading every bit of the MOSI/MISO message by myself.
Here is my code, maybe it helps you.
Code: Select all
import time
import RPi.GPIO as GPIO
import math
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
def read(msgtype, SCLKPin, MOSIPin, MISOPin, CSPin, DELAY):
if(msgtype == NOP):
message = [0x00, 0x00, 0xFF, 0x00, 0x00, 0x00, 0xD0, -141]
else:
message = [0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x93, -247]
#Fallende Flanke
GPIO.output (CSPin, GPIO.HIGH)
GPIO.output (CSPin, GPIO.LOW)
GPIO.output (SCLKPin, GPIO.LOW)
readValue = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
for k in range(8):
sendbyte = message[k] #jeweiliges byte der message
#1 Byte Senden fuer Sensor
for i in range(8):
if(sendbyte & 0x80): #Bit nr8 pruefen
GPIO.output(MOSIPin, GPIO.HIGH) #1schreiben
#print(1)
else:
GPIO.output(MOSIPin, GPIO.LOW)
GPIO.output(SCLKPin, GPIO.HIGH)
sendbyte <<= 1 #Bitfolge verschieben damit naechstes Bit geprueft werden kann
readValue[k] <<=1
if(GPIO.input(MISOPin) == GPIO.HIGH):
readValue[k] |= 0x01
GPIO.output(SCLKPin, GPIO.HIGH)
GPIO.output(SCLKPin, GPIO.LOW)
GPIO.output(CSPin, GPIO.HIGH)
time.sleep(DELAY)
return readValue
def setupGPIO(SCLKPin, MOSIPin, MISOPin, CSPin):
GPIO.setup(SCLKPin, GPIO.OUT)
GPIO.setup(MOSIPin, GPIO.OUT)
GPIO.setup(MISOPin, GPIO.IN)
GPIO.setup(CSPin, GPIO.OUT)
SCLK = 11
MOSI = 10
MISO = 9
CS = 8
DELAY = 0
NOP = 1
XYZ = 2
setupGPIO(SCLK, MOSI, MISO, CS)
GPIO.output(SCLK, GPIO.HIGH)
nopmessage = read(NOP, SCLK, MOSI, MISO, CS, DELAY)
print(nopmessage)
try:
while True:
GPIO.output(SCLK, GPIO.HIGH)
XYZMessage = read(XYZ, SCLK, MOSI, MISO, CS, DELAY)
except KeyboardInterrupt:
print 'Beendet'