V-I Curve Plotting Programs V.24 (DAC, ADC, CSA)
# vip2402.py tlfong01 2021jan06hkt1624
import dac_mcp4725_v2401 as dac
import adc_mcp3201_v2401 as adc
# *** Init/Main Function ***
# *** DAC MCP4725 Test Functions ***
def init():
pass
return
def main():
'''
dac.testMcp4725SetValV01('1.50V')
adc.testAdc('SpiPort00')
adc.testAdc('SpiPort01')
adc.testAdc('SpiPort10')
adc.testAdc('SpiPort11')
'''
dac.testSetAnalogValV01()
adc.testAdc('SpiPort00')
adc.testAdc('SpiPort01')
adc.testAdc('SpiPort10')
adc.testAdc('SpiPort11')
return
if __name__ == '__main__':
main()
# *** End of Program ********************************************************************
# *** Sample Outputs ***
'''
>>> %Run vip2301.py
Begin testMcp4725SetValV01(), ...
digiValName = 1.50V
digiValName = 1.50V
digiValName = 1.50V
digiValName = 1.50V
End testMcp4725SetValV01().
MCP3201 ADC Results:
spiPortName = SpiPort00
recvArray = [15, 167]
Binary = 0x7d3
Decimal = 1.47 V
MCP3201 ADC Results:
spiPortName = SpiPort01
recvArray = [13, 128]
Binary = 0x6c0
Decimal = 1.27 V
MCP3201 ADC Results:
spiPortName = SpiPort10
recvArray = [15, 168]
Binary = 0x7d4
Decimal = 1.47 V
MCP3201 ADC Results:
spiPortName = SpiPort11
recvArray = [15, 170]
Binary = 0x7d5
Decimal = 1.47 V
>>>
'''
# *** End of Sample Output ***
# dacMcp4725v2401.py tlfong01 2021jan06hkt1614
from datetime import datetime
from time import sleep
from signal import pause
import os
import sys
import smbus
# *** Config ***
i2cBus1 = smbus.SMBus(1)
i2cBus3 = smbus.SMBus(3)
i2cBusDict = {'I2cBus1': i2cBus1,
'I2cBus3': i2cBus3,
}
# *** System Functions ***
def pause(pauseTimeName):
sleep(i2cControlByteDict[pauseTimeName])
return
def convertTwoCompNumToDecNum(twoCompNum):
twoCompNumStr = bin(twoCompNum)
numBytes = 2
val = int(twoCompNumStr, numBytes)
b = val.to_bytes(numBytes, byteorder=sys.byteorder, signed = False)
return int.from_bytes(b, byteorder = sys.byteorder, signed = True)
i2cControlByteDict = {
'TenMilliSeconds' : 0.01,
'FourTimes' : 4,
'OneMillionTimes' : 1000000,
'TwentyMilliSeconds' : 0.02,
}
timeNowLong = str(datetime.now())
timeNowShort = str(datetime.now())[0:16]
# *** System Config Functions ***
def getSystemInfo():
print('\n# *** System Info *****************************************************')
print('\n>>>>> ', 'Date', '<<<<<')
os.system('date')
print('\n>>>>> ', 'linux version', 'buster version, Rpi4B model, Rpi4B memory', '<<<<<')
os.system('cat /etc/issue.net')
os.system('uname -a')
os.system('grep Model /proc/cpuinfo')
os.system('grep MemTotal /proc/meminfo')
print('\n>>>>> ', 'i2c baudrate', '<<<<<')
os.system('grep dtparam=i2c /boot/config.txt')
print('\n>>>>> ', 'i2c dtoverlay', '<<<<<')
os.system('grep dtoverlay=i2c /boot/config.txt')
print('\n>>>>> ', 'ls /dev/i2c*', '<<<<<')
os.system('ls /dev/i2c*')
i2cdetectCommand = 'i2cdetect -y 1'
print('\n>>>>> ', i2cdetectCommand, '<<<<<')
os.system(i2cdetectCommand)
#i2cdetectCommand = 'i2cdetect -y 3'
#print('\n>>>>> ', i2cdetectCommand, '<<<<<')
#os.system(i2cdetectCommand)
#i2cdetectCommand = 'i2cdetect -y 4'
#print('\n>>>>> ', i2cdetectCommand, '<<<<<')
#os.system(i2cdetectCommand)
return
def getUartConfigInfo():
print('\n# *** UART BaudRate *****************************************************')
os.system('grep dtparam=i2c /boot/config.txt')
return
# *** Write/Read Device One Byte ***
def quickWriteDevOneByte(i2cBus, devAddr, writeByte):
i2cBus.write_byte(devAddr, writeByte)
return
def quickReadDevOneByte(i2cBus, devAddr):
readByte = i2cBus.read_byte(devAddr)
return readByte
# *** Write/Read/Print Device Two Bytes / Device Register One Byte ***
def writeDevTwoBytes(i2cBus, devAddr, writeByte1, writeByte2):
i2cBus.write_byte_data(devAddr, writeByte1, writeByte2)
return
def writeDevThreeBytes(i2cBus, devAddr, writeByte1, writeByte2, writeByte3):
i2cBus.write_block_data(devAddr, writeByte1, [writeByte2, writeByte3])
return
def writeRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regName, writeByte):
devAddr = devAddrDict[devAddrName]
regAddr = regAddrDict[regName]
writeDevTwoBytes(i2cBus, devAddr, regAddr, writeByte)
return
def readDevOneByte(i2cBus, devAddrAddr, readByteAddr):
readByte = i2cBus.read_byte_data(devAddr, readByteAddr)
return readByte
def readDevReadByteAddrByteOneByte(i2cBus, devAddrAddr, readByteAddr): # !!! Not Tested !!!
readByte = readDevOneByte(i2cBus, devAddrAddr, readByteAddr)
return readByte
def readDevControlByteOneByte(i2cBus, devAddr, controlByte):
readByte = i2cBus.read_byte_data(devAddr, controlByte)
return readByte
def readRegOneByte(i2cBus, devAddrDict, devName, regAddrDict, regName):
devAddr = devAddrDict[devName]
regAddr = regAddrDict[regName]
readByte = i2cBus.read_byte_data(devAddr, regAddr)
return readByte
def printRegOneByte(i2cBus, devAddrDict, devName, regAddrDict, regName):
readByte = readRegOneByte(i2cBusName, devAddrDict, devName, regAddrDict, regName)
print(printTitle, hex(readByte))
return
# *** Device Write / Read / Print Register Functions ***
def readRegister(busName, devAddrDict, devAddrName, regAddrDict, regAddrName):
i2cBus = i2cBusDict[busName]
readByte = readRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regAddrName)
return readByte
def readVerifyRegister(moduleType, moduleNickName, busName, devAddrDict, devAddrName, regAddrDict, regAddrName, controlByteDict, verifyByteName):
i2cBus = i2cBusDict[busName]
verifyByte = controlByteDict[verifyByteName]
readByte = readRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regAddrName)
if readByte == verifyByte:
verifyResultsString = 'Success'
else:
verifyResultsString = 'Failure'
devAddr = devAddrDict[devAddrName]
fprint.printTitleString('Date Time', fprint.indentFormat640, str(datetime.now())[0:16])
fprint.printTitleString('ModuleType', fprint.indentFormat640, moduleType)
fprint.printTitleString('ModuleNickName', fprint.indentFormat640, moduleNickName)
fprint.printTitleString('I2C Bus Name', fprint.indentFormat640, busName)
fprint.printTitleString('Device Addr Name', fprint.indentFormat640, devAddrName)
fprint.printTitleOneByteNum('Device Address', fprint.indentFormat640, devAddr)
fprint.printTitleString('Register Name', fprint.indentFormat640, regAddrName)
fprint.printTitleOneByteNum('VerifyByte', fprint.indentFormat640, verifyByte)
fprint.printTitleOneByteNum('Byte Read from Register', fprint.indentFormat640, readByte)
fprint.printTitleString('Verify Results', fprint.indentFormat640, verifyResultsString)
return verifyResultsString
def writeVerifyRegister(moduleType, moduleNickName, busName, devAddrDict, devAddrName, regAddrDict, regAddrName, controlByteDict, writeByteName):
i2cBus = i2cBusDict[busName]
writeByte = controlByteDict[writeByteName]
writeRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regAddrName, writeByte)
readByte = readRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regAddrName)
if readByte == writeByte:
resultsString = 'Success'
else:
resultsString = 'Failure'
devAddr = devAddrDict[devAddrName]
fprint.printTitleString('Date Time', fprint.indentFormat640, str(datetime.now())[0:16])
fprint.printTitleString('ModuleType', fprint.indentFormat640, moduleType)
fprint.printTitleString('ModuleNickName', fprint.indentFormat640, moduleNickName)
fprint.printTitleString('I2C Bus Name', fprint.indentFormat640, busName)
fprint.printTitleString('Device Addr Name', fprint.indentFormat640, devAddrName)
fprint.printTitleOneByteNum('Device Address', fprint.indentFormat640, devAddr)
fprint.printTitleString('Register Name', fprint.indentFormat640, regAddrName)
fprint.printTitleOneByteNum('Byte Written to Register', fprint.indentFormat640, writeByte)
fprint.printTitleOneByteNum('Byte Read back from Register', fprint.indentFormat640, readByte)
fprint.printTitleString('Verify Results', fprint.indentFormat640, resultsString)
return
def writeVerifyModuleRegister(moduleTypeName, moduleNickName, regAddrName, writeByteName):
moduleDict = moduleDictDict[moduleTypeName]
moduleType = moduleDict['ModuleType']
busName = moduleDict[moduleNickName]['I2cBusName']
controlByteDict = moduleDict['ControlByteDict']
devAddrDict = moduleDict['DevAddrDict']
devAddrName = moduleDict[moduleNickName]['DevAddrName']
regAddrDict = moduleDict['RegAddrDict']
regAddr = regAddrDict[regAddrName]
i2cBus = i2cBusDict[busName]
writeByte = controlByteDict[writeByteName]
writeRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regAddrName, writeByte)
readByte = readRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regAddrName)
if readByte == writeByte:
writeVerifyResultsString = 'Success'
else:
writeVerifyResultsString = 'Failure'
devAddr = devAddrDict[devAddrName]
fprint.printTitleString('Date Time', fprint.indentFormat640, str(datetime.now())[0:16])
fprint.printTitleString('ModuleType', fprint.indentFormat640, moduleType)
fprint.printTitleString('ModuleNickName', fprint.indentFormat640, moduleNickName)
fprint.printTitleString('I2C Bus Name', fprint.indentFormat640, busName)
fprint.printTitleString('Device Addr Name', fprint.indentFormat640, devAddrName)
fprint.printTitleOneByteNum('Device Address Byte', fprint.indentFormat640, devAddr)
fprint.printTitleString('Register Name', fprint.indentFormat640, regAddrName)
fprint.printTitleOneByteNum('Register Address Byte', fprint.indentFormat640, regAddr)
fprint.printTitleString('WriteByteName', fprint.indentFormat640, writeByteName)
fprint.printTitleOneByteNum('WriteByte (Written to Register)', fprint.indentFormat640, writeByte)
fprint.printTitleOneByteNum('ReadByte (Read from Register)', fprint.indentFormat640, readByte)
fprint.printTitleString('Write Verify Results', fprint.indentFormat640, writeVerifyResultsString)
return writeVerifyResultsString
def pingModule(moduleTypeName, moduleNickName):
moduleDict = moduleDictDict[moduleTypeName]
moduleType = moduleDict['ModuleType']
busName = moduleDict[moduleNickName]['I2cBusName']
controlByteDict = moduleDict['ControlByteDict']
devAddrDict = moduleDict['DevAddrDict']
devAddrName = moduleDict[moduleNickName]['DevAddrName']
regAddrDict = moduleDict['RegAddrDict']
regAddrName = moduleDict['PingRegAddrName']
writeByteName = moduleDict['PingWriteByteName']
writeVerifyRegister(moduleType, moduleNickName, busName, devAddrDict, devAddrName, regAddrDict, regAddrName,
controlByteDict, writeByteName)
return
def readModuleRegister(moduleTypeName, moduleNickName, regAddrName):
moduleDict = moduleDictDict[moduleTypeName]
moduleType = moduleDict['ModuleType']
busName = moduleDict[moduleNickName]['I2cBusName']
controlByteDict = moduleDict['ControlByteDict']
devAddrDict = moduleDict['DevAddrDict']
devAddrName = moduleDict[moduleNickName]['DevAddrName']
regAddrDict = moduleDict['RegAddrDict']
readByte = readRegister(busName, devAddrDict, devAddrName, regAddrDict, regAddrName)
return readByte
def readRegister(busName, devAddrDict, devAddrName, regAddrDict, regAddrName):
i2cBus = i2cBusDict[busName]
readByte = readRegOneByte(i2cBus, devAddrDict, devAddrName, regAddrDict, regAddrName)
return readByte
# ========== ========== ========== ========== ========== ========== ========== ==========
# *** MCP4725 ***
# ========== ========== ========== ========== ========== ========== ========== ==========
devAddrDict = \
{
'Mcp4725-0' : 0x60,
'Mcp4725-1' : 0x61,
}
commandByteDict = \
{ 'WriteRegNoEepromPowerDown00' : 0b0000,
}
digiValDict = \
{ 'FullScale' : 0xfff, # 3.29V (no load)
'HalfScale' : 0x800, # 1.65V
'ZeroScale' : 0x000, # 0.00V
'3.00V' : 0xe98,
'1.50V' : 0xe98 >> 1,
'0.75V' : 0xe98 >> 2,
'0.77V' : (0xe98 >> 2) + 0x020,
'0.01V' : 0x010,
'0.02V' : 0x020,
'0.00V' : 0x000,
}
mcp4725ModuleDict = \
{
'ModuleType' : 'MCP4725DAC',
'ModuleData' : \
{
'Mcp4725I2cBus1Module0': \
{
'BusName' : 'I2cBus1',
'DevAddrName' : 'Mcp4725-0',
'WritePowerModeName' : 'WriteRegNoEepromPowerDown00',
},
'Mcp4725I2cBus1Module1': \
{
'BusName' : 'I2cBus1',
'DevAddrName' : 'Mcp4725-1',
'WritePowerModeName' : 'WriteRegNoEepromPowerDown00',
},
'Mcp4725I2cBus3Module0': \
{
'BusName' : 'I2cBus3',
'DevAddrName' : 'Mcp4725-0',
'WritePowerModeName' : 'WriteRegNoEepromPowerDown00',
},
'Mcp4725I2cBus3Module1': \
{
'BusName' : 'I2cBus3',
'DevAddrName' : 'Mcp4725-1',
'WritePowerModeName' : 'WriteRegNoEepromPowerDown00',
},
},
}
# *** Digital and Analog Conversion Functions *
def digiAnaConvertV08(i2cBus, devAddr, writePowerMode, digiVal):
writePowerModeDigiValMsb = (writePowerMode << 4) | ((digiVal & 0xf00) >> 8)
#writePowerModeDigiValMsb = 0x0f
digiValLsb = digiVal & 0x0ff
print(' I2cBusName =', 'I2cBus1')
print(' devAddr =', hex(0x60))
print(' writePowerMode =', hex(writePowerMode))
print(' digiVal =', hex(digiVal))
print(' writePowerModeDigiValMsb =', hex(writePowerModeDigiValMsb))
print(' digiValLsb =', hex(digiValLsb))
writeDevTwoBytes(i2cBus, devAddr, writePowerModeDigiValMsb, digiValLsb)
return
def testDigiAnaConvertV08():
i2cBus = i2cBus1
devAddr = 0x60
writePowerMode = 0x0
digiVal = 0xfff
pauseSeconds = 1
digiVal = 0x0ff
for dacCount in range(15):
digiAnaConvertV08(i2cBus, devAddr, writePowerMode, digiVal)
sleep(0.25)
digiVal = digiVal + 0x100
return
# *** DC Sweep Functions ***
def daConvertV09(i2cBus, devAddr, writePowerMode, digiVal):
#print('Begin daConvertV09(), ...')
writePowerModeDigiValMsb = (writePowerMode << 4) | ((digiVal & 0xf00) >> 8)
digiValLsb = digiVal & 0x0ff
#print(' I2cBusName =', 'I2cBus1')
#print(' devAddr =', hex(0x60))
#print(' writePowerMode =', hex(writePowerMode))
#print(' digiVal =', hex(digiVal))
#print(' writePowerModeDigiValMsb =', hex(writePowerModeDigiValMsb))
#print(' digiValLsb =', hex(digiValLsb))
writeDevTwoBytes(i2cBus, devAddr, writePowerModeDigiValMsb, digiValLsb)
#print('End daConvertV09().')
return
def dcSweepV01():
#print('Begin testDaConvertV09(), ...')
i2cBus = i2cBus1
devAddr = 0x60
writePowerMode = 0b0000
digiValBegin = 0x000
digiValEnd = 0xfff
TenMilliSeconds = 0.01
digiVal = digiValBegin
for convertCount in range(100):
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
sleep(TenMilliSeconds)
digiVal = digiVal + 0x50
#print('End testDaConvertV09().')
return
def testDcSweepV01():
print('Begin testDcSweepV01(), ...')
for testCount in range(100000):
dcSweepV01()
print('End testDcSweepV01(), ...')
return
def testCalibrateV01():
print('Begin testCalibrate(), ...')
i2cBusName = 'I2cBus1'
devAddrName = 'Mcp4725-0'
i2cBus = i2cBusDict[i2cBusName]
devAddr = devAddrDict[devAddrName]
writePowerMode = 0b0000
digiValBegin = 0x000
digiValEnd = 0xfff
digiVal = digiValDict['0.01V']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', '0.01V')
sleep(2)
digiVal = digiValDict['0.02V']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', '0.02V')
sleep(2)
digiVal = digiValDict['0.77V']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', '0.77V')
sleep(2)
print('End testCalibrate().')
return
def testCalibrateV02():
print('Begin testCalibrate(), ...')
i2cBusName = 'I2cBus1'
#devAddrName = 'Mcp4725-0'
devAddrName = 'Mcp4725-1'
writePowerModeName = 'WriteRegisterNoEepromPowerDown00'
i2cBus = i2cBusDict[i2cBusName]
devAddr = devAddrDict[devAddrName]
writePowerMode = commandByteDict[writePowerModeName]
sleepSeconds = 0.5
digiValNameList01 = ['FullScale', 'HalfScale', 'ZeroScale', \
'3.00V', '1.50V', '0.75V', '0.01V', '0.02V', '0.77V', '3.00V',
]
digiValNameList02 = ['3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V']
for digiValName in digiValNameList02:
print(' digiValName =', digiValName)
digiVal = digiValDict[digiValName]
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
sleep(sleepSeconds)
print('End testCalibrate().')
return
#def dcSweep(beginDigiValName, endDigiValName, digiValStepName, pauseTimeName, countTotalName):
# for count in controlByteDict[countTotalName]:
# print('count =',
def testCalibrateV03():
print('Begin testCalibrate(), ...')
i2cBusName = 'I2cBus1'
#devAddrName = 'Mcp4725-0'
devAddrName = 'Mcp4725-1'
writePowerModeName = 'WriteRegisterNoEepromPowerDown00'
i2cBus = i2cBusDict[i2cBusName]
devAddr = devAddrDict[devAddrName]
writePowerMode = commandByteDict[writePowerModeName]
sleepSeconds = 0.5
digiValNameList01 = ['FullScale', 'HalfScale', 'ZeroScale', \
'3.00V', '1.50V', '0.75V', '0.01V', '0.02V', '0.77V', '3.00V',
]
#digiValNameList02 = ['3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V'] ]
digiValNameList02 = ['3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V']
for digiValName in digiValNameList02:
print(' digiValName =', digiValName)
digiVal = digiValDict[digiValName]
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
sleep(sleepSeconds)
print('End testCalibrate().')
return
def testMcp4725BlinkV02():
print('Begin testDacBlinkV01(), ...')
testModuleNameList = ['Mcp4725I2cBus1Module0', 'Mcp4725I2cBus1Module1', 'Mcp4725I2cBus3Module0', 'Mcp4725I2cBus3Module1', ]
digiValNameList01 = ['FullScale', 'HalfScale', 'ZeroScale', \
'3.00V', '1.50V', '0.75V', '0.01V', '0.02V', '0.77V', '3.00V',
]
digiValNameList02Long = ['3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V', '0.00V', '3.00V']
digiValNameList02 = ['0.00V', '3.00V', '0.00V', '3.00V']
sleepSeconds = 0.5
for moduleName in testModuleNameList:
i2cBusName = mcp4725ModuleDict['ModuleData'][moduleName]['BusName']
devAddrName = mcp4725ModuleDict['ModuleData'][moduleName]['DevAddrName']
writePowerModeName = mcp4725ModuleDict['ModuleData'][moduleName]['WritePowerModeName']
i2cBus = i2cBusDict[i2cBusName]
devAddr = devAddrDict[devAddrName]
writePowerMode = commandByteDict[writePowerModeName]
for digiValName in digiValNameList02:
print(' digiValName =', digiValName)
digiVal = digiValDict[digiValName]
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
sleep(sleepSeconds)
print('End testDacBlinkV01().')
return
def testMcp4725SetValV01(digiValName):
print('Begin testMcp4725SetValV01(), ...')
testChannelNameList = ['Mcp4725I2cBus1Module0', 'Mcp4725I2cBus1Module1', 'Mcp4725I2cBus3Module0', 'Mcp4725I2cBus3Module1', ]
#digiValName = '1.50V'
for channelName in testChannelNameList:
i2cBusName = mcp4725ModuleDict['ChannelName'][channelName]['BusName']
devAddrName = mcp4725ModuleDict['ChannelName'][channelName]['DevAddrName']
writePowerModeName = mcp4725ModuleDict['ChannelName'][channelName]['WritePowerModeName']
i2cBus = i2cBusDict[i2cBusName]
devAddr = devAddrDict[devAddrName]
writePowerMode = commandByteDict[writePowerModeName]
print(' digiValName =', digiValName)
digiVal = digiValDict[digiValName]
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('End testMcp4725SetValV01().')
return
def setAnalogVal(moduleName, digiValName):
print(' Begin setAnalogVal(), ...')
print(' moduleName =', moduleName)
print(' digiValName =', digiValName)
i2cBusName = mcp4725ModuleDict['ModuleData'][moduleName]['BusName']
devAddrName = mcp4725ModuleDict['ModuleData'][moduleName]['DevAddrName']
writePowerModeName = mcp4725ModuleDict['ModuleData'][moduleName]['WritePowerModeName']
i2cBus = i2cBusDict[i2cBusName]
devAddr = devAddrDict[devAddrName]
writePowerMode = commandByteDict[writePowerModeName]
digiVal = digiValDict[digiValName]
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print(' End setAnalogVal(), ...')
return
def testSetAnalogValV01():
print('Begin testSetAnalogValV01(), ...')
setAnalogVal('Mcp4725I2cBus1Module0', '0.00V')
setAnalogVal('Mcp4725I2cBus1Module1', '0.75V')
setAnalogVal('Mcp4725I2cBus3Module0', '1.50V')
setAnalogVal('Mcp4725I2cBus3Module1', '3.00V')
print('End testSetAnalogValV01(), ...')
return
# *** Init/Main Function ***
def init():
pass
return
def main():
#testDigiAnaConvertV08()
#testDcSweepV01()
#testCalibrateV01()
#testCalibrateV02()
#testCalibrateV03()
#testMcp4725BlinkV01()
#testMcp4725SetValV01('1.50V')
#testMcp4725BlinkV02()
testSetAnalogValV01()
return
if __name__ == '__main__':
main()
# *** End of Program ********************************************************************
# *** 11. Sample Outputs ***
'''
>>> %Run mcp4725v267.py
Begin testDacBlinkV01(), ...
digiValName = 0.00V
digiValName = 3.00V
digiValName = 0.00V
digiValName = 3.00V
digiValName = 0.00V
digiValName = 3.00V
digiValName = 0.00V
digiValName = 3.00V
digiValName = 0.00V
digiValName = 3.00V
digiValName = 0.00V
digiValName = 3.00V
digiValName = 0.00V
digiValName = 3.00V
digiValName = 0.00V
digiValName = 3.00V
End testDacBlinkV01().
>>>
'''
# *** End of Sample Output ***
# adc_mcp3201_v2401.py tlfong01 2021jan06hkt1622 ***
# *** System Configuration ***
# 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 - test MCP3201 ADC
# Function - Input an anlog signal to the differential input pins of MCP3201 and read the converted digital value from
# SPI MOSI
# Setup -
# *** Import ***
from datetime import datetime
from time import sleep
import spidev
# ** SPI Setup/Config ***
spiPort00 = spidev.SpiDev()
spiPort00.open(0,0)
spiPort00.max_speed_hz = 100000
spiPort01 = spidev.SpiDev()
spiPort01.open(0,1)
spiPort01.max_speed_hz = 100000
spiPort10 = spidev.SpiDev()
spiPort10.open(1,0)
spiPort10.max_speed_hz = 100000
spiPort11 = spidev.SpiDev()
spiPort11.open(1,1)
spiPort11.max_speed_hz = 100000
spiPort12 = spidev.SpiDev()
spiPort12.open(1,2)
spiPort12.max_speed_hz = 100000
spiPortDict = \
{'SpiPort00': spiPort00,
'SpiPort01': spiPort01,
'SpiPort10': spiPort10,
'SpiPort11': spiPort11,
'SpiPort12': spiPort12,
}
#*** System Functions ***
timeNowLong = str(datetime.now())
timeNowShort = str(datetime.now())[0:16]
# *** SPI System Functions ***
# *** SPI Send/Receive 1/2/3 Bytes ***
def spiSendRecvOneByte(spiBus, sendByte):
sendByteArray = [sendByte]
recvByteArray = spiBus.xfer2(sendByteArray)
return recvByteArray
def spiSendRecvTwoBytes(spiBus, sendByte1, sendByte2):
sendByteArray = [sendByte1, sendByte2]
recvByteArray = spiBus.xfer2(sendByteArray)
return recvByteArray
def spiSendRecvThreeBytes(spiBus, sendByte1, sendByte2, sendByte3):
sendByteArray = [sendByte1, sendByte2, sendByte3]
recvByteArray = spiBus.xfer2(sendByteArray)
return recvByteArray
# *** MCP3201 Config ***
refVoltName = '3.00V'
dummyByte = 0x00
refVoltDict = \
{
'0.00V' : 0.00,
'1.68V' : 1.68,
'10.24V' : 1.024,
'2.048V' : 2.048,
'3.00V' : 3.00,
'3.30V' : 3.30,
'4.096V' : 4.096,
}
# *** MCP3201 Functions ***
def getMcp3201BinaryResults(spiPortName, refVoltName):
spiPort = spiPortDict[spiPortName]
recvArray = spiSendRecvTwoBytes(spiPort, 0x00, 0x00)
print(' spiPortName =', spiPortName)
print(' recvArray =', recvArray)
adcBinaryResults = (((recvArray[0] & 0x3f) << 8) + recvArray[1]) >> 1
#print(' adcBinaryResults =', hex(adcBinaryResults))
return adcBinaryResults
def getMcp3201DecimalResults(adcBinaryResults):
adcDecimalResults = (adcBinaryResults / 0xfff) * refVoltDict[refVoltName]
return adcDecimalResults
# *** ADC Functions ***
def testAdc(spiPortName):
print(' MCP3201 ADC Results:')
adcBinaryResults = getMcp3201BinaryResults(spiPortName, refVoltName)
#print( 'adcBinaryResults =', adcBinaryResults)
adcDecimalResults = getMcp3201DecimalResults(adcBinaryResults)
#print(' MCP3201 Results in 12 bits binary (expect fff/2~= 0x0800) =', convertTwoByteNumToEightCharStr(adcBinaryResults))
print(' Binary =', hex(adcBinaryResults))
print(' Decimal =', "%.2f" % adcDecimalResults, 'V')
return
# *** Init/Main Function ***
def init():
pass
return
def main():
print('Begin main(), ... tlfong01', timeNowShort)
print(' Begin testMCP3201Adc(), ...')
testAdc('SpiPort00')
testAdc('SpiPort01')
testAdc('SpiPort10')
testAdc('SpiPort11')
#testAdc('SpiPort12')
print(' End testMCP3201Adc().')
print('End main().')
return
# *** Main()
if __name__ == '__main__':
main()
# *** End of Program ***
# *** Sample Output ***
'''
>>> %Run mcp3201v1601.py
Begin main(), ... tlfong01 2021-01-01 22:56
Begin testMCP3201Adc(), ...
MCP3201 ADC Results:
spiPortName = SpiPort00
recvArray = [15, 231]
adcBinaryResults = 0x7f3
adcBinaryResults = 2035
Binary = 0x7f3
Decimal = 1.49 V
End testMCP3201Adc().
End main().
>>>
'''
# *** End of Sample output ***
No comments:
Post a Comment