Diode 1N4148 I-V Plot Program - Part 3
# vip3517.py tlfong01 2021jan17hkt2153
from datetime import datetime
from time import sleep
import dac3601 as dac
import adc3601 as adc
# *** Config ***
# *** DevDict ***
devDict = \
{
# *** DAC Devices ***
'DacDev0' : { 'DevName' : 'Mcp4725I2cBus1Module0'},
'DacDev1' : { 'DevName' : 'Mcp4725I2cBus1Module1'},
'DacDev2' : { 'DevName' : 'Mcp4725I2cBus3Module0'},
'DacDev3' : { 'DevName' : 'Mcp4725I2cBus3Module1'},
# *** ADC Devices ***
'AdcDev0' : { 'DevName' : 'Mcp3201SpiPort0Module0'},
'AdcDev1' : { 'DevName' : 'Mcp3201SpiPort0Module1'},
'AdcDev2' : { 'DevName' : 'Mcp3201SpiPort1Module0'},
'AdcDev3' : { 'DevName' : 'Mcp3201SpiPort1Module1'},
}
# *** Dev Devices Name ***
dacDev0Name = devDict['DacDev0']['DevName']
dacDev1Name = devDict['DacDev1']['DevName']
dacDev2Name = devDict['DacDev2']['DevName']
dacDev3Name = devDict['DacDev3']['DevName']
adcDev0Name = devDict['AdcDev0']['DevName']
adcDev1Name = devDict['AdcDev1']['DevName']
adcDev2Name = devDict['AdcDev2']['DevName']
adcDev3Name = devDict['AdcDev3']['DevName']
# *** System Config/Functions ***
timeDict = {'OneSecond' : 1}
dateTimeNowLong = str(datetime.now())
dateTimeNowShort = str(datetime.now())[0:16]
def pauseSeconds(secondsName):
sleep(timeDict[secondsName])
return
# *** Config / Test Functions ***
devDict = \
{
# *** DAC Devices ***
'DacDev0' : { 'DevName' : 'Mcp4725I2cBus1Module0'},
'DacDev1' : { 'DevName' : 'Mcp4725I2cBus1Module1'},
'DacDev2' : { 'DevName' : 'Mcp4725I2cBus3Module0'},
'DacDev3' : { 'DevName' : 'Mcp4725I2cBus3Module1'},
# *** ADC Devices ***
'AdcDev0' : { 'DevName' : 'Mcp3201SpiPort0Module0'},
'AdcDev1' : { 'DevName' : 'Mcp3201SpiPort0Module1'},
'AdcDev2' : { 'DevName' : 'Mcp3201SpiPort1Module0'},
'AdcDev3' : { 'DevName' : 'Mcp3201SpiPort1Module1'},
}
timeDict = \
{
'OneMilliSecond' : 0.001,
'OneSecond' : 1,
}
countDict = \
{
'OneTime' : 1,
'OneMillionTimes' : 1000000,
#'OneMillionTimes' : 1,
}
listDict = {'List13': [x * 512 for x in range(8)]}
diodePlotDictDict = \
{
'1BS4' : {'Reserved'},
'1N4001' : {'Reserved'},
'1N4148' : {'DiodePlotDict12' : {'Reserved'},
'DiodePlotDict13' :
{
'VccDacName' : 'DacDev0',
'VccAdcName' : 'AdcDev0',
'ResistorAdcName' : 'AdcDev1',
'DiodeAdcName' : 'AdcDev2',
'ListName' : 'List13',
'PauseTimeName' : 'OneMilliSecond',
#'TotalCountName' : 'OneMillionTimes',
'TotalCountName' : 'OneTime',
}
}
}
def diodePlot13(diodeName, diodePlotDictName):
print('Begin diodePlot13(), ...', 'tlfong01', dateTimeNowShort, '\n')
vccDacName = diodePlotDictDict[diodeName][diodePlotDictName]['VccDacName']
vccAdcName = diodePlotDictDict[diodeName][diodePlotDictName]['VccAdcName']
resistorAdcName = diodePlotDictDict[diodeName][diodePlotDictName]['ResistorAdcName']
diodeAdcName = diodePlotDictDict[diodeName][diodePlotDictName]['DiodeAdcName']
sweepListName = diodePlotDictDict[diodeName][diodePlotDictName]['ListName']
pauseTimeName = diodePlotDictDict[diodeName][diodePlotDictName]['PauseTimeName']
totalCountName = diodePlotDictDict[diodeName][diodePlotDictName]['TotalCountName']
print(' diodeName =', diodeName)
print(' diodePlotDictName =', diodePlotDictName)
print(' vccDacName =', vccDacName)
print(' vccAdcName =', vccAdcName)
print(' resistorAdcName =', resistorAdcName)
print(' diodeAdcName =', diodeAdcName)
print(' sweepListName =', sweepListName)
print(' pauseTimeName =', pauseTimeName)
print(' totalCountName =', totalCountName)
dcvSweep13(vccDacName, vccAdcName, resistorAdcName, diodeAdcName, sweepListName, pauseTimeName, totalCountName)
print('\nEnd diodePlot13().')
return
def dcvSweep13(vccDacName, vccAdcName, resistorAdcName, diodeAdcName, sweepListName, pauseTimeName, totalCountName):
dacModuleName0 = devDict[vccDacName]['DevName']
adcModuleName0 = devDict[vccAdcName]['DevName']
adcModuleName1 = devDict[resistorAdcName]['DevName']
adcModuleName2 = devDict[diodeAdcName]['DevName']
sweepList = listDict[sweepListName]
pauseTime = timeDict[pauseTimeName]
totalCount = countDict[totalCountName]
print('')
print(' dacModuleName0 =', dacModuleName0)
print(' adcModuleName0 =', adcModuleName0)
print(' adcModuleName1 =', adcModuleName1)
print(' adcModuleName2 =', adcModuleName2)
print(' sweepList =', sweepList)
print(' pauseTime =', pauseTime)
print(' totalCount =', totalCount)
print('')
print(' Begin sweep testing, ...')
print(' ... <Ctrl>C to abort.')
'''
for count in range(totalCount):
for sweepStep in range(len(sweepList)):
#print('\n sweepStep =', sweepStep)
digiVal = sweepList[sweepStep]
print(sweepList)
print(' digiVal (Vcc) =', digiVal)
#dac.setDigiValToAnalogValNoPrint(dacModuleName0, digiVal)
dac.testSetAnalogValV01NoPrint()
#dac.testSweepV04()
#dac.testSweepV04('Mcp4725I2cBus1Module0', [x * 16 for x in range(1024)], 0.001, 1)
#adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName0)
#print(' digiVal 0 (Dv) =', '{:.2f}'.format(adcDecimalResults), 'V')
#adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName1)
#print(' digiVal 1 (Rv) =', '{:.2f}'.format(adcDecimalResults), 'V')
#adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName2)
#print(' digiVal 2 (Ri) =', '{:.2f}'.format(adcDecimalResults), 'V')
sleep(pauseTime)
#print('')
'''
print(' Begin sweep testing.')
return
# *** Init/Main Function ***
def init():
pass
return
if __name__ == '__main__':
diodePlot13('1N4148', 'DiodePlotDict13')
# *** End of Program ********************************************************************
# *** Sample Outputs ***
'''
>>> %Run vip3506.py
Begin diodePlot09(), ... tlfong01 2021-01-15 16:00
sweepList = [0, 512, 1024, 1536, 2048, 2560, 3072, 3584]
pauseTime = 0.001
totalCount = 1000000
Vcc Analog dacModuleName0 = Mcp4725I2cBus1Module0
Vcc Digital adcModuleName0 = Mcp3201SpiPort0Module0
Vr AdcModuleName1 = Mcp3201SpiPort0Module1
Vd AdcModuleName2 = Mcp3201SpiPort1Module0
dcvSweepList = [0, 512, 1024, 1536, 2048, 2560, 3072, 3584]
pauseTime = 0.001
totalCount = 1000000
Begin sweep testing, ...
... <Ctrl>C to abort.
'''
# *** 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,
}
digiValDictOld = \
{ '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,
}
digiValDict = \
{ '3.00V' : 0xfff,
'1.50V' : 0xfff >> 1,
'0.75V' : 0xfff >> 2,
'0.375V' : 0xfff >> 3,
'0.1875V' : 0xfff >> 4,
'0.09375V' : 0xfff >> 5,
'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['ModuleData'][moduleName]['BusName']
devAddrName = mcp4725ModuleDict['ModuleData'][moduleName]['DevAddrName']
writePowerModeName = mcp4725ModuleDict['ModuleData'][moduleName]['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 setDigiValToAnalogValNoPrint(moduleName, digiVal):
#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 setAnalogValNoPrint(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
def testSetAnalogValV01NoPrint():
print('Begin testSetAnalogValV01NoPrint(), ...')
setAnalogValNoPrint('Mcp4725I2cBus1Module0', '0.00V')
setAnalogValNoPrint('Mcp4725I2cBus1Module1', '0.75V')
setAnalogValNoPrint('Mcp4725I2cBus3Module0', '1.50V')
setAnalogValNoPrint('Mcp4725I2cBus3Module1', '3.00V')
print('End testSetAnalogValV01NoPrint(), ...')
return
# *** Sweep Functions ***
# program notes
# sweep generator patterns: - (linear or logarithmic) stepped, glided, timed, table
# frequency sweep, (DC voltage) amplitude sweep,
minAnalogVal = 0x0
maxAnalogVal = 0xfff
def testList():
print('minAnalogVal =', minAnalogVal)
print('maxAnalogVal =', maxAnalogVal)
analogValList = []
#analogValList = [0x0, 0xfff]
#analogValList = [x * 2 for x in range(32) if x % 16 == 0]
analogValList = [x * 16 for x in range(16)]
print('analogValList =', analogValList)
return
def dcSweep(dacModuleName, dcVoltSweepList, stepPauseSeconds):
print('Begin dcSweep(), ...')
pass
print('End dcSweep(), ...')
return
def testSweepV01():
dacModuleName = 'Mcp4725I2cBus1Module0'
dcVoltSweepList = ['0.00V', '0.75V', '1.50V', '3.00V']
stepPauseSeconds = 1
print('dacModuleName =', dacModuleName)
print('dcVoltSweepList =', dcVoltSweepList)
print('stepPauseSeconds =', stepPauseSeconds)
for sweepStep in range(len(dcVoltSweepList)):
print('sweepStep =', sweepStep, ' ', end = '')
analogValName = dcVoltSweepList[sweepStep]
print('analogValName =', analogValName)
setAnalogValNoPrint(dacModuleName, analogValName)
return
def testSweepV02(dacModuleName, dcVoltSweepList, stepPauseSeconds):
#dacModuleName = 'Mcp4725I2cBus1Module0'
#dcVoltSweepList = ['0.00V', '0.75V', '1.50V', '3.00V']
#dcVoltSweepList = [x * 16 for x in range(16)]
#stepPauseSeconds = 1
print('dacModuleName =', dacModuleName)
print('dcVoltSweepList =', dcVoltSweepList)
print('stepPauseSeconds =', stepPauseSeconds)
for sweepStep in range(len(dcVoltSweepList)):
#analogValName = dcVoltSweepList[sweepStep]
#print('analogValName =', analogValName)
digiVal = dcVoltSweepList[sweepStep]
print('sweepStep =', sweepStep, ' ', 'digiVal =', digiVal)
setDigiValToAnalogValNoPrint(dacModuleName, digiVal)
sleep(1)
print('')
return
def testSweepV03(dacModuleName, dcVoltSweepList, stepPauseSeconds):
print('dacModuleName =', dacModuleName)
print('dcVoltSweepList =', dcVoltSweepList)
print('stepPauseSeconds =', stepPauseSeconds)
for sweepStep in range(len(dcVoltSweepList)):
digiVal = dcVoltSweepList[sweepStep]
print('sweepStep =', sweepStep, ' ', 'digiVal =', digiVal)
setDigiValToAnalogValNoPrint(dacModuleName, digiVal)
sleep(stepPauseSeconds)
print('')
return
def testSweepV04(dacModuleName, dcVoltSweepList, stepPauseSeconds, totalRepeatCount):
#print('dacModuleName =', dacModuleName)
#print('dcVoltSweepList =', dcVoltSweepList)
#print('stepPauseSeconds =', stepPauseSeconds)
for count in range(totalRepeatCount):
for sweepStep in range(len(dcVoltSweepList)):
digiVal = dcVoltSweepList[sweepStep]
#print('sweepStep =', sweepStep, ' ', 'digiVal =', digiVal)
setDigiValToAnalogValNoPrint(dacModuleName, digiVal)
sleep(stepPauseSeconds)
#print('')
return
# *** Init/Main Function ***
def init():
pass
return
def main():
#testDigiAnaConvertV08()
#testDcSweepV01()
#testCalibrateV01()
#testCalibrateV02()
#testCalibrateV03()
#testMcp4725BlinkV01()
#testMcp4725SetValV01('1.50V')
#testMcp4725BlinkV02()
#testSetAnalogValV01()
#testSetAnalogValV01NoPrint()
#testSweepV01()
#testList()
#testSweepV03('Mcp4725I2cBus1Module0', [x * 256 for x in range(16)], 1)
#testSweepV04('Mcp4725I2cBus1Module0', [x * 16 for x in range(1024)], 0.001, 200000)
#testSweepV04('Mcp4725I2cBus1Module0', [x * 16 for x in range(1024)], 0.001, 1)
testSetAnalogValV01NoPrint()
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_v2404.py tlfong01 2021jan06hkt2215 ***
# *** 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 ***
mcp3201ModuleDict = \
{
'ModuleType' : 'MCP3201ADC',
'ModuleData' : \
{
'Mcp3201SpiPort0Module0': \
{
'SpiPortName' : 'SpiPort00',
'RefVoltName' : '3.00V',
},
'Mcp3201SpiPort0Module1': \
{
'SpiPortName' : 'SpiPort01',
'RefVoltName' : '3.00V',
},
'Mcp3201SpiPort1Module0': \
{
'SpiPortName' : 'SpiPort10',
'RefVoltName' : '3.00V',
},
'Mcp3201SpiPort1Module1': \
{
'SpiPortName' : 'SpiPort11',
'RefVoltName' : '3.00V',
},
'Mcp3201SpiPort1Module2': \
{
'SpiPortName' : 'SpiPort12',
'RefVoltName' : '3.00V',
},
},
}
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,
}
# refVoltName = '3.00V'
# dummyByte = 0x00
# *** MCP3201 Functions ***
def getMcp3201BinaryResults(moduleName):
spiPortName = mcp3201ModuleDict['ModuleData'][moduleName]['SpiPortName']
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 getMcp3201BinaryResultsNoPrint(moduleName):
spiPortName = mcp3201ModuleDict['ModuleData'][moduleName]['SpiPortName']
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(moduleName):
adcBinaryResults = getMcp3201BinaryResults(moduleName)
refVoltName = mcp3201ModuleDict['ModuleData'][moduleName]['RefVoltName']
refVolt = refVoltDict[refVoltName]
adcDecimalResults = (adcBinaryResults / 0xfff) * refVolt
return adcDecimalResults
def getMcp3201DecimalResultsNoPrint(moduleName):
adcBinaryResults = getMcp3201BinaryResultsNoPrint(moduleName)
refVoltName = mcp3201ModuleDict['ModuleData'][moduleName]['RefVoltName']
refVolt = refVoltDict[refVoltName]
adcDecimalResults = (adcBinaryResults / 0xfff) * refVolt
return adcDecimalResults
# *** ADC Functions ***
def testAdc(moduleName):
print(' Begin testAdc()')
adcBinaryResults = getMcp3201BinaryResults(moduleName)
#print( 'adcBinaryResults =', adcBinaryResults)
adcDecimalResults = getMcp3201DecimalResults(moduleName)
#print(' MCP3201 Results in 12 bits binary (expect fff/2~= 0x0800) =', convertTwoByteNumToEightCharStr(adcBinaryResults))
print(' Binary =', hex(adcBinaryResults))
print(' Decimal =', "%.2f" % adcDecimalResults, 'V')
print(' Begin testAdc().')
return
def testAdcNoPrint(moduleName):
#print(' Begin testAdc()')
adcBinaryResults = getMcp3201BinaryResultsNoPrint(moduleName)
#print( 'adcBinaryResults =', adcBinaryResults)
adcDecimalResults = getMcp3201DecimalResultsNoPrint(moduleName)
#print(' MCP3201 Results in 12 bits binary (expect fff/2~= 0x0800) =', convertTwoByteNumToEightCharStr(adcBinaryResults))
#print(' Binary =', hex(adcBinaryResults))
#print(' Decimal =', "%.2f" % adcDecimalResults, 'V')
#print(' Begin testAdc().')
print(' moduleName =', moduleName, 'adcDecimalResults (V) =', '{:.2f}'.format(adcDecimalResults))
return adcDecimalResults
# *** Init/Main Function ***
def init():
pass
return
def main01():
print('Begin main(), ... tlfong01', timeNowShort)
print(' Begin testMCP3201Adc(), ...')
#testAdc('SpiPort00')
#testAdc('SpiPort01')
#testAdc('SpiPort10')
#testAdc('SpiPort11')
#testAdc('SpiPort12')
testAdc('Mcp3201SpiPort0Module0')
testAdc('Mcp3201SpiPort0Module1')
testAdc('Mcp3201SpiPort1Module0')
testAdc('Mcp3201SpiPort1Module1')
print(' End testMCP3201Adc().')
print('End main().')
return
def main02():
print('Begin main(), ... tlfong01', timeNowShort)
print(' Begin testMCP3201Adc(), ...')
#testAdc('SpiPort00')
#testAdc('SpiPort01')
#testAdc('SpiPort10')
#testAdc('SpiPort11')
#testAdc('SpiPort12')
testAdcNoPrint('Mcp3201SpiPort0Module0')
testAdcNoPrint('Mcp3201SpiPort0Module1')
testAdcNoPrint('Mcp3201SpiPort1Module0')
testAdcNoPrint('Mcp3201SpiPort1Module1')
print(' End testMCP3201Adc().')
print('End main().')
return
# *** Main()
if __name__ == '__main__':
#main01()
main02()
# *** 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 ***