class ADC16(): def __init__(self): self.spi = SPI(1) self.SCK=Pin('PA5',Pin.OUT) self.DO = Pin('PA6',Pin.OUT) self.DI = Pin('PA7',Pin.OUT) self.CS = Pin('PA4',Pin.OUT) self.RESET = Pin('PC5',Pin.OUT) self.DRDY = Pin('PC4',Pin.OUT) self.bsp_DelayMS(10) # self.TM7705_ResetHard() #/* 硬件复位 */ # #/* # # 在接口序列丢失的情况下,如果在DIN 高电平的写操作持续了足够长的时间(至少 32个串行时钟周期), # # TM7705 将会回到默认状态。 # #*/ # self.bsp_DelayMS(5) # self.TM7705_SyncSPI() #/* 同步SPI接口时序 */ # self.bsp_DelayMS(5) # # /* 配置时钟寄存器 */ # self.TM7705_WriteByte(REG_CLOCK | WRITE | CH_1) # /* 先写通信寄存器,下一步是写时钟寄存器 */ # self.TM7705_WriteByte(CLKDIS_0 | CLK_4_9152M | FS_50HZ) #/* 刷新速率50Hz */ # #//TM7705_WriteByte(CLKDIS_0 | CLK_4_9152M | FS_500HZ);# /* 刷新速率500Hz */ # # /* 每次上电进行一次自校准 */ # self.TM7705_CalibSelf(1) #/* 内部自校准 CH1 */ # self.bsp_DelayMS(5) def bsp_DelayMS(self,t): time.sleep_ms(t) def TM7705_ResetHard(self): self.RESET.on() self.bsp_DelayMS(1) self.RESET.off() self.bsp_DelayMS(2) self.RESET.on() self.bsp_DelayMS(1) def TM7705_SyncSPI(self): self.CS.off() self.TM7705_Send8Bit(0xFF) self.TM7705_Send8Bit(0xFF) self.TM7705_Send8Bit(0xFF) self.TM7705_Send8Bit(0xFF) self.CS.on() def TM7705_Send8Bit(self,data): data = data%256 self.spi.write(struct.pack('B',data)) def TM7705_WriteByte(self,data): self.CS.off() self.TM7705_Send8Bit(data) self.CS.on() def TM7705_Write3Byte(self,data): self.CS.off() self.TM7705_Send8Bit(data>>16) self.TM7705_Send8Bit(data>>8) self.TM7705_Send8Bit(data) self.CS.on() # def TM7705_ReadByte(self): # read = 0 # self.CS.off() # read = self.spi.read() # print(read) # self.CS.on() # return read # def TM7705_Read2Byte(self): # self.TM7705_ReadByte() # def TM7705_Read3Byte(self): # self.TM7705_ReadByte() # ##################################待更改 def TM7705_WaitDRDY(self): while (self.DRDY.value()): time.sleep_ms(200) #等待校准 def TM7705_WriteReg(self,_RegID,_RegValue): bits = 0 if _RegID == REG_COMM or _RegID ==REG_SETUP or _RegID == REG_CLOCK: bits = 8 elif _RegID ==REG_ZERO_CH1 or _RegID ==REG_FULL_CH1 or _RegID ==REG_ZERO_CH2 or _RegID ==REG_FULL_CH2 : bits=24 self.TM7705_WriteByte(_RegID | WRITE) if bits==8: self.TM7705_WriteByte(_RegValue) else : self.TM7705_Write3Byte(_RegValue) def TM7705_CalibSelf(self,_ch): if _ch==1: # /* 自校准CH1 */ self.TM7705_WriteByte(REG_SETUP | WRITE | CH_1)#; #/* 写通信寄存器,下一步是写设置寄存器,通道1 */ self.TM7705_WriteByte(MD_CAL_SELF | __CH1_GAIN_BIPOLAR_BUF | FSYNC_0)#;#/* 启动自校准 */ self.TM7705_WaitDRDY()#; /* 等待内部操作完成 --- 时间较长,约180ms */ elif _ch == 2: # /* 自校准CH2 */ self.TM7705_WriteByte(REG_SETUP | WRITE | CH_2)#; /* 写通信寄存器,下一步是写设置寄存器,通道2 */ self.TM7705_WriteByte(MD_CAL_SELF | __CH2_GAIN_BIPOLAR_BUF | FSYNC_0)# /* 启动自校准 */ self.TM7705_WaitDRDY() #####read adc def TM7705_ReadAdc(self,_ch): read = 0 for ii in range(2): self.TM7705_WaitDRDY() if _ch == 1: self.TM7705_WriteByte(0x38) elif _ch == 2: self.TM7705_WriteByte(0x39) #read2byte read = self.spi.read() return read
print(spi) spi = SPI(0, SPI.MASTER, baudrate=5000000, bits=32, polarity=1, phase=0) print(spi) spi = SPI(0, SPI.MASTER, baudrate=10000000, polarity=1, phase=1) print(spi) spi.init(baudrate=20000000, polarity=0, phase=0) print(spi) spi=SPI() print(spi) SPI(mode=SPI.MASTER) SPI(mode=SPI.MASTER, pins=spi_pins) SPI(id=0, mode=SPI.MASTER, polarity=0, phase=0, pins=('GP14', 'GP16', 'GP15')) SPI(0, SPI.MASTER, polarity=0, phase=0, pins=('GP31', 'GP16', 'GP15')) spi = SPI(0, SPI.MASTER, baudrate=10000000, polarity=0, phase=0, pins=spi_pins) print(spi.write('123456') == 6) buffer_r = bytearray(10) print(spi.readinto(buffer_r) == 10) print(spi.readinto(buffer_r, write=0x55) == 10) read = spi.read(10) print(len(read) == 10) read = spi.read(10, write=0xFF) print(len(read) == 10) buffer_w = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) print(spi.write_readinto(buffer_w, buffer_r) == 10) print(buffer_w == buffer_r) # test all polaritiy and phase combinations spi.init(polarity=1, phase=0, pins=None) buffer_r = bytearray(10) spi.write_readinto(buffer_w, buffer_r)
print(spi) spi = SPI(0, SPI.MASTER, baudrate=5000000, bits=32, polarity=1, phase=0) print(spi) spi = SPI(0, SPI.MASTER, baudrate=10000000, polarity=1, phase=1) print(spi) spi.init(baudrate=20000000, polarity=0, phase=0) print(spi) spi = SPI() print(spi) SPI(mode=SPI.MASTER) SPI(mode=SPI.MASTER, pins=spi_pins) SPI(id=0, mode=SPI.MASTER, polarity=0, phase=0, pins=('GP14', 'GP16', 'GP15')) SPI(0, SPI.MASTER, polarity=0, phase=0, pins=('GP31', 'GP16', 'GP15')) spi = SPI(0, SPI.MASTER, baudrate=10000000, polarity=0, phase=0, pins=spi_pins) print(spi.write('123456') == 6) buffer_r = bytearray(10) print(spi.readinto(buffer_r) == 10) print(spi.readinto(buffer_r, write=0x55) == 10) read = spi.read(10) print(len(read) == 10) read = spi.read(10, write=0xFF) print(len(read) == 10) buffer_w = bytearray([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) print(spi.write_readinto(buffer_w, buffer_r) == 10) print(buffer_w == buffer_r) # test all polaritiy and phase combinations spi.init(polarity=1, phase=0, pins=None) buffer_r = bytearray(10) spi.write_readinto(buffer_w, buffer_r)
import sensor, image, time import pyb from pyb import SPI # Camera/Hardware Objects: fmt = sensor.RGB565 res = sensor.QVGA led1 = pyb.LED(1) led2 = pyb.LED(2) # SPI bus for lighting spi = SPI(2, SPI.MASTER, 500000, polarity=1, phase=0, crc=None) ledreset = bytearray(24) # Reset the LED strip spi.write(ledreset) # Turn on Green lighting: ledgreen = bytearray(24) for ii in range(0, len(ledgreen)): ledgreen[ii] = 128 intensity = 110 for ii in range(0, 8): ledgreen[ii * 3] = 128 + intensity spi.write(ledgreen) spi.write(ledreset) # Get Camera ID: