Page 8 of 16
Page 160
from machine import Pin, SPI
spi=SPI(1,sck=Pin(14),miso=Pin(12),mosi=Pin(13))
spi.init(baudrate=500_000,bits=8, polarity=0,phase=0,firstbit=SPI.MSB )
read = bytearray(3)
write = bytearray([0xAA,0xAA,0xAA])
spi.write_readinto(write,read)
print(read,write)
spi.deinit()
Page 168
from utime import sleep_ms
from machine import Pin, SPI
spi=SPI(1,sck=Pin(14),miso=Pin(12),mosi=Pin(13))
spi.init(baudrate=500_000, bits=8, polarity=0, phase=0,firstbit=SPI.MSB)
CS = Pin(15, Pin.OUT)
CS.on()
sleep_ms(1)
CS.off()
write=bytearray([0x01, 0x80, 0x00])
read=bytearray(3)
spi.write_readinto(write,read)
CS.on()
data = (read[1] & 0x03) << 8 | read[2]
volts = data * 3.3 / 1023.0
print(volts)
spi.deinit()
Page 169
from utime import sleep_ms
from machine import Pin, SPI
class spiADC:
def __init__(self,spi,sckNo,misoNo,mosiNo,CSNo):
self.spi = SPI(spi, sck=Pin(sckNo), miso=Pin(misoNo), mosi=Pin(mosiNo))
self.spi.init(baudrate=500_000, bits=8, polarity=0,phase=0, firstbit=SPI.MSB)
self.CS = Pin(CSNo, Pin.OUT)
self.CS.on()
sleep_ms(1)
def read(self,chan):
write=bytearray([0x01, (0x08 | chan) << 4 , 0x00])
self.CS.off()
read=bytearray(3)
self.spi.write_readinto(write,read)
self.CS.on()
data = (read[1] & 0x03) << 8 | read[2]
volts = data * 3.3 / 1023.0
return volts
adc=spiADC(1,14,12,13,15)
volts=adc.read(1)
print(volts)