MCP4725 DAC Learning Notes - Part 8 (Quad DAC Board Setup)
https://youtu.be/G6fMo1TroXw
# mcp4725v267.py tlfong01 2020dec30hkt1608
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,
}
Mcp4725ChannelDict = \
{
'ModuleType' : 'MCP4725DAC',
'ChannelName' : \
{
'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 testMcp4725BlinkV01():
print('Begin testDacBlinkV01(), ...')
testChannelNameList = ['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 channelName in testChannelNameList:
i2cBusName = Mcp4725ChannelDict['ChannelName'][channelName]['BusName']
devAddrName = Mcp4725ChannelDict['ChannelName'][channelName]['DevAddrName']
writePowerModeName = Mcp4725ChannelDict['ChannelName'][channelName]['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
# *** Init/Main Function ***
def init():
pass
return
def main():
#testDigiAnaConvertV08()
#testDcSweepV01()
#testCalibrateV01()
#testCalibrateV02()
#testCalibrateV03()
testMcp4725BlinkV01()
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 ***
.END
No comments:
Post a Comment