|
Edited by nopnop2002 at 2017-11-7 10:38
You can use software SPI(Bit Banging SPI) like this.
#!/usr/bin/python
#-*- encoding: utf-8 -*-
from periphery import GPIO
mosi = 4 # Pin#19
miso = 3 # Pin#21
sclk = 2 # Pin#23
cs = 5 # Pin#24
gpio_mosi = GPIO(mosi, "out")
gpio_miso = GPIO(miso, "in")
gpio_sclk = GPIO(sclk, "out")
gpio_cs = GPIO(cs, "out")
def SpiWrite(data):
mask = 0x80
for x in range(8):
gpio_sclk.write(False)
bit = data & mask
if (bit != 0):
gpio_mosi.write(True)
if (bit == 0):
gpio_mosi.write(False)
gpio_sclk.write(True)
mask = mask >> 1
def SpiRead():
r_data = 0;
mask = 0x80
gpio_mosi.write(False)
for x in range(8):
r_data = r_data << 1
gpio_sclk.write(False)
gpio_sclk.write(True)
bit = gpio_miso.read()
if (bit == True):
r_data = r_data + 1
return r_data;
|
|