python中spidev用法

spidev

用法(Usage):

import spidev #导入spidev模块
spi=spidev.SpiDev() #面向对象编程
spi.open(bus,device) #打开spi中对应的设备
to_send=[0x01,0x02,0x03]
spi.xfer(to_send) #设置串口波特率·

设置(Settings):

import spidev #导入spidev模块
spi=spidev.SpiDev() #面向对象编程
spi.open(bus,device)#打开对应的模块

设置(比如)

spi.max_speed_hz=5000 #设置spi串口最大频率为5000Hz
spi.mode=0b01 #设置spi模块的地址
。。。
位每字
cs片选给高电
LOOP-设置为SPI-LOOP标识为enable回环模式(loopback mode)
'no_cs’设置‘SPI_NO_CS’标志位设为了disable(同样也有可能模块存在延时(pin)
‘lsbfirst’
max_speed_hz
‘mode’-SPI模块作为两位模式在时钟突变(上升沿和下降沿)和阶段(CPOL或者CPHA),min:oboo=0,max=0b11=3
三线:SI/SO 信号共享

方法:

open(bus,device)#打开设备
Connects to specified SPI device,opening’/dev/spidev.’
连接特定的SPI模块,打开’dev/spidev/.
readbytes(n)
#Read n bytes from SPI device.
#从SPI模块中读取n字节的数据
writebytes(list of values)
#Writes a list of values to SPI device.
#向SPI模块中写入数据(按照以列表的形式的数据)
writebytes2(list of values)
#similar to 'writebytes’but accepts arbitrary large lists.
#类似上面的用法,但是却支持任意大小的数据列表
#if list size exceeds buffer size (which is read from ‘/sys/module/spidev/parameters/bufsiz’),data will be spilt into smaller chunks and sent in multiple operations.
#如果数据宽度超出了数据缓冲区(无论是哪一个读取)数据将被分割成小的部分并且送到多重操作
Also ,‘writebytes2’understands[buffer protoco](http://docs.python.org/3/c-api/buffer.html) so it can accept numpy bytes arrays for example without need to convert them with 'tolist()'first.This offer much better performance where you need to transfer frames to SPI-connect displays for instance.
#当然,这个语法不能理解【缓冲协议】导致了支持麻烦的数组。同样无疑需要召集他们你将需要被提供他们更多的性能同时转移他们也还需要框架处理这些数据
x.fer(list of values[,speed_hz,delay_usec,bit_per_word])
Performs an SPI transaction.Chip-select should released and reactivated between blocks.Delay specifies the delay in usec between blocks.
执行一个SPI业务。芯片选择应该释放并且要在阻碍之间重新激活。延时指定的时间(单位:纳秒)在阻碍之间。
xfer2(list of values[,speed_hz,delay_usec,bit_per_word])
perform an spi transaction.chip -select should be held active between block.
#执行一个spi业务,芯片选择应该活动在阻碍之间不断掉
xfer3(list of values[,speed_hz,delay_usec,bit_per_word])
Similar to ‘xfer2’ but accepts arbitrary large lists if list size exceeds buffer size (which is read from ‘/sys/module/spidev/parameters/bufsiz’),data will be spilt into smaller chunks and sent in multiple operations.
#像xfer2一样 但是却支持任意大的列表,如果数据宽度超出了数据缓冲区(无论是哪一个读取)数据将被分割成小的部分并且送到多重操作

close()
Disconnects from the SPI device.
#关闭与SPI设备的连接

总结

你需要做到一下的步骤:

导入库函数

import spidev

配置spi配置(使用面向对象编程)

spi=spidev.SpiDev()

打开spi中的模块

spi.open(bus,device)

配置相应的设置

spi.xfer([<频率>,<延时us>,<位每字节>])

读取或者发送数据

spi.readbytes(<字节>)
spi.writebytes(<字节>)

关闭对话

你可能感兴趣的:(python)