MCP4725 DAC Learning Notes - Part 7 (Calibration OK)
# mcp4725v259.py tlfong01 2020dec28hkt1551
from datetime import datetime
from time import sleep
from signal import pause
import os
import sys
import smbus
# *** Config ***
i2cBus1 = smbus.SMBus(1)
i2cBusDict = {'I2cBus1': i2cBus1,
}
# *** 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 ***
# ========== ========== ========== ========== ========== ========== ========== ==========
mcp4725I2cDevAddr = 0x60
mcp4725DevAddrDict = {
'DevAddr0': 0x60,
'DevAddr1': 0x61,
}
mcp4725ControlByteDict = {
'ChannelNum0': 0x00,
'ChannelNum1': 0x01,
}
mcp4725ModuleDict = {
'ModuleType' : 'MCP4725',
'GreenTea': {'SignalName' : 'GreenTea',
'I2cBusName' : 'I2cBus4',
'DevAddrName' : 'DevAddr0',
'ChannelNumName' : 'ChannelNum0',
},
}
# *** 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
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,
}
devAddrDict = \
{
'Mcp4725' : 0x60
}
def testCalibrate():
print('Begin testCalibrate(), ...')
i2cBusName = 'I2cBus1'
devAddrName = 'Mcp4725'
i2cBus = i2cBusDict[i2cBusName]
devAddr = devAddrDict[devAddrName]
writePowerMode = 0b0000
digiValBegin = 0x000
digiValEnd = 0xfff
'''
digiVal = digiValDict['FullScale']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', 'FullScale')
print('End testCalibrate().')
sleep(2)
digiVal = digiValDict['HalfScale']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', 'HalfScale')
print('End testCalibrate().')
sleep(2)
digiVal = digiValDict['ZeroScale']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', 'ZeroScale')
print('End testCalibrate().')
sleep(2)
'''
'''
digiVal = digiValDict['3.00V']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', '3.00V')
sleep(2)
digiVal = digiValDict['1.50V']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', '1.50')
sleep(2)
digiVal = digiValDict['0.75V']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', '0.75V')
sleep(2)
'''
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)
'''
digiVal = digiValDict['0.00V']
daConvertV09(i2cBus, devAddr, writePowerMode, digiVal)
print('DigiVal =', '0.00V')
sleep(2)
'''
print('End testCalibrate().')
return
# *** Init/Main Function ***
def init():
pass
return
def main():
#testDigiAnaConvertV08()
#testDcSweepV01()
testCalibrate()
return
if __name__ == '__main__':
main()
# *** End of Program ********************************************************************
# *** 11. Sample Outputs ***
'''
'''
# *** End of Sample Output ***
No comments:
Post a Comment