MCP3201 Programming Log - Part 2 (SPI loopback test v08)
# spitestv08_2020jan0102.py tlfong01 2021jan01hkt1224 ***
# *** Configuration Description ***
# Rpi4B Thonny Python 3.7.3 (/usr/bin/python3)
# pi@raspberrypi:~ $ date Fri 01 Jan 2021 12:17:54 PM HKT
# pi@raspberrypi:~ $ uname -a Linux raspberrypi 5.4.79-v7l+ #1373 SMP Mon Nov 23 13:27:40 GMT 2020 armv7l GNU/Linux
# *** Test Description ***
# Test 1 - loopBackTest() - SPI port send and receive one byte.
# Function - Send one byte to MSOI and read it back from MISO.
# Setup - Connet MOSI pin to MISO pin to form a loop.
# Test 2 - repeatSendByte() - SPI port repeatedly send out single bytes.
# Function - Repeat many times sending a byte, pause after each byte.
# *** Import ***
from datetime import datetime
from time import sleep
import spidev
# ** SPI Setup/Config ***
spiPort0 = spidev.SpiDev()
spiPort0.open(0,0)
spiPort0.max_speed_hz = 100000
#*** System Functions ***
timeNowLong = str(datetime.now())
timeNowShort = str(datetime.now())[0:16]
# *** SPI System Functions ***
def spiSendRecvOneByte(spiPort, sendByte):
sendByteArray = [sendByte]
recvByteArray = spiPort.xfer(sendByteArray)
return recvByteArray
def loopBackOneByte(spiPort, sendByte):
recvByteArray = spiSendRecvOneByte(spiPort, sendByte)
recvByte = recvByteArray[0]
print(' Begin testLoopbackOneByte(),....')
#print('')
print(' Note 1 = Connect SPI MOSI pin to MISO pin to loop back!')
print(' Note 2 = Loopback results below:')
print(' sendByte = ', hex(sendByte))
print(' recvByte = ', hex(recvByte))
#print('')
print(' End testLoopbackOneByte(),....')
return
def repeatSendOneByte(spiPort, sendByte, pauseTimeBetweenBytes, repeatCount):
print('\nBegin repeatSendByte(),....')
for i in range(repeatCount):
spiSendRecvOneByte(spiPort, sendByte)
sleep(pauseTimeBetweenBytes)
print('End repeatSendByte().')
return
# *** SPI Test Functions ***
def testRepeatSendOneByte():
repeatSendOneByte(spiPort0, 0x5b, 0.0001, 20000000)
return
def testLoopbackOneByte():
loopBackOneByte(spiPort0, 0x5b)
return
# *** Init/Main Function ***
def init():
pass
return
def main():
#testRepeatSendOneByte()
#testLoopbackOneByte()
print('Begin main(), ... - tlfong01', timeNowShort)
loopBackOneByte(spiPort0, 0x5b)
print('End main().')
return
# *** Main()
if __name__ == '__main__':
main()
# *** End of Program ***
# *** Sample Output ***
# Smple output tlfong 01 2019apr07hkt2047
'''
>>> %Run sptestv08_2021jan0103.py
Begin main(), ... - tlfong01 2021-01-01 12:37
Begin testLoopbackOneByte(),....
Note 1 = Connect SPI MOSI pin to MISO pin to loop back!
Note 2 = Loopback results below:
sendByte = 0x5b
recvByte = 0x5b
End testLoopbackOneByte(),....
End main().
>>>
'''
# *** End of Sample Output ***
No comments:
Post a Comment