Thursday 14 2021

Diode 1N4148 I-V Plot Program - Part 1

Diode 1N4148 I-V Plot Program - Part 1

# *** Sample Outputs ***


'''

>>> %Run vip3503.py

Begin diodeIvPlot08(), ... tlfong01 2021-01-14 22:06

  dacModuleName0   = Mcp4725I2cBus1Module0

  adcModuleName0   = Mcp3201SpiPort0Module0

  adcModuleName1   = Mcp3201SpiPort0Module1

  adcModuleName2   = Mcp3201SpiPort1Module0

  dcvSweepList     = [0, 512, 1024, 1536, 2048, 2560, 3072, 3584]

  stepPauseSeconds = 0.001

  totalCount       = 1

  sweepStep             = 1

    digiVal   (Vcc)     = 512

    digiVal 0 (Dv)      = 0.39 V

    digiVal 1 (Rv)      = 0.35 V

    digiVal 2 (Ri)      = 0.38 V


  sweepStep             = 2

    digiVal   (Vcc)     = 1024

    digiVal 0 (Dv)      = 0.79 V

    digiVal 1 (Rv)      = 0.74 V

    digiVal 2 (Ri)      = 0.53 V


  sweepStep             = 3

    digiVal   (Vcc)     = 1536

    digiVal 0 (Dv)      = 1.19 V

    digiVal 1 (Rv)      = 1.13 V

    digiVal 2 (Ri)      = 0.56 V


  sweepStep             = 4

    digiVal   (Vcc)     = 2048

    digiVal 0 (Dv)      = 1.59 V

    digiVal 1 (Rv)      = 1.54 V

    digiVal 2 (Ri)      = 0.58 V


  sweepStep             = 5

    digiVal   (Vcc)     = 2560

    digiVal 0 (Dv)      = 1.98 V

    digiVal 1 (Rv)      = 1.93 V

    digiVal 2 (Ri)      = 0.60 V


  sweepStep             = 6

    digiVal   (Vcc)     = 3072

    digiVal 0 (Dv)      = 2.39 V

    digiVal 1 (Rv)      = 2.30 V

    digiVal 2 (Ri)      = 0.61 V


  sweepStep             = 7

    digiVal   (Vcc)     = 3584

    digiVal 0 (Dv)      = 2.79 V

    digiVal 1 (Rv)      = 2.74 V

    digiVal 2 (Ri)      = 0.62 V

End   diodePlot08().

>>> 


'''

===

# 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 ***







# *** Sample Outputs ***


'''

>>> %Run vip3503.py

Begin diodeIvPlot08(), ... tlfong01 2021-01-14 22:06

  dacModuleName0   = Mcp4725I2cBus1Module0

  adcModuleName0   = Mcp3201SpiPort0Module0

  adcModuleName1   = Mcp3201SpiPort0Module1

  adcModuleName2   = Mcp3201SpiPort1Module0

  dcvSweepList     = [0, 512, 1024, 1536, 2048, 2560, 3072, 3584]

  stepPauseSeconds = 0.001

  totalCount       = 1


  sweepStep             = 0

    digiVal   (Vcc)     = 0

    digiVal 0 (Dv)      = 0.00 V

    digiVal 1 (Rv)      = 0.00 V

    digiVal 2 (Ri)      = 0.00 V


  sweepStep             = 1

    digiVal   (Vcc)     = 512

    digiVal 0 (Dv)      = 0.39 V

    digiVal 1 (Rv)      = 0.35 V

    digiVal 2 (Ri)      = 0.38 V


  sweepStep             = 2

    digiVal   (Vcc)     = 1024

    digiVal 0 (Dv)      = 0.79 V

    digiVal 1 (Rv)      = 0.74 V

    digiVal 2 (Ri)      = 0.53 V


  sweepStep             = 3

    digiVal   (Vcc)     = 1536

    digiVal 0 (Dv)      = 1.19 V

    digiVal 1 (Rv)      = 1.13 V

    digiVal 2 (Ri)      = 0.56 V


  sweepStep             = 4

    digiVal   (Vcc)     = 2048

    digiVal 0 (Dv)      = 1.59 V

    digiVal 1 (Rv)      = 1.54 V

    digiVal 2 (Ri)      = 0.58 V


  sweepStep             = 5

    digiVal   (Vcc)     = 2560

    digiVal 0 (Dv)      = 1.98 V

    digiVal 1 (Rv)      = 1.93 V

    digiVal 2 (Ri)      = 0.60 V


  sweepStep             = 6

    digiVal   (Vcc)     = 3072

    digiVal 0 (Dv)      = 2.39 V

    digiVal 1 (Rv)      = 2.30 V

    digiVal 2 (Ri)      = 0.61 V


  sweepStep             = 7

    digiVal   (Vcc)     = 3584

    digiVal 0 (Dv)      = 2.79 V

    digiVal 1 (Rv)      = 2.74 V

    digiVal 2 (Ri)      = 0.62 V

End   diodePlot08().

>>> 


'''



# vip3504.py tlfong01 2021jan14hkt1900


from datetime      import datetime

from time          import sleep

import dac3501 as dac

import adc3501 as adc


# *** Config ***


# *** DevDict ***


devDict = \

    {

        # *** DAC Devices ***

        'DacDev0'    : {    'DevName' : 'Mcp4725I2cBus1Module0', 'AnalogValName'  : '1.50V'},

        'DacDev1'    : {    'DevName' : 'Mcp4725I2cBus1Module1', 'AnalogValName'  : '1.50V'},

        'DacDev2'    : {    'DevName' : 'Mcp4725I2cBus3Module0', 'AnalogValName'  : '1.50V'},

        'DacDev3'    : {    'DevName' : 'Mcp4725I2cBus3Module1', 'AnalogValName'  : '1.50V'},

 

         # *** 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


# *** Old Test Functions ***


'''

def vipTest2502():

    dac.setAnalogVal('Mcp4725I2cBus1Module0', '0.00V')

    dac.setAnalogVal('Mcp4725I2cBus1Module1', '0.75V')

    dac.setAnalogVal('Mcp4725I2cBus3Module0', '1.50V')

    dac.setAnalogVal('Mcp4725I2cBus3Module1', '3.00V')

    

    adc.testAdc('Mcp3201SpiPort0Module0')

    adc.testAdc('Mcp3201SpiPort0Module1')

    adc.testAdc('Mcp3201SpiPort1Module0')

    adc.testAdc('Mcp3201SpiPort1Module1')

    

    return


def dacTest2503(dacDevNameList):

    for dacDevName in dacDevNameList:

        devName       = devDict[dacDevName]['DevName']

        analogValName = devDict[dacDevName]['AnalogValName']

        dac.setAnalogVal(devName, analogValName)

    return


def adcTest2503(adcDevNameList):

    for adcDevName in adcDevNameList:

        devName       = devDict[adcDevName]['DevName']

        adc.testAdc(devName)

    return


def dacTest2504(dacDevNameList):

    for dacDevName in dacDevNameList:

        devName       = devDict[dacDevName]['DevName']

        analogValName = devDict[dacDevName]['AnalogValName']

        print('   moduleName =', devName, ' AnalogValName         =', analogValName)

        dac.setAnalogValNoPrint(devName, analogValName)

    return


def adcTest2504(adcDevNameList):

    for adcDevName in adcDevNameList:

        devName       = devDict[adcDevName]['DevName']

        #print('devName =', devName)

        adc.testAdcNoPrint(devName)

    return


def dacTest3101(dacDevNameList, analogValName):

    

    for dacDevName in dacDevNameList:

        devName       = devDict[dacDevName]['DevName']

        #analogValName = devDict[dacDevName]['AnalogValName']

        #analogValName = '1.50V'

        print('    moduleName =', devName, ' AnalogValName         =', analogValName)

        dac.setAnalogValNoPrint(devName, analogValName)

    return


def adcTest3101(adcDevNameList):

    for adcDevName in adcDevNameList:

        devName       = devDict[adcDevName]['DevName']

        #print('devName =', devName)

        adcDecimalResults = adc.testAdcNoPrint(devName)

    return adcDecimalResults

    

    def main01():

    dacTest2503(['DacDev0'])

    dacTest2503(['DacDev1'])

    dacTest2503(['DacDev2'])

    dacTest2503(['DacDev3'])

    return


def main02():

    dacTest2503(['DacDev0', 'DacDev1', 'DacDev2', 'DacDev3'])

    adcTest2503(['AdcDev0', 'AdcDev1', 'AdcDev2', 'AdcDev3'])

    return


def main03():

    dacTest2504(['DacDev0', 'DacDev1', 'DacDev2', 'DacDev3'])

    #adcTest2503(['AdcDev0', 'AdcDev1', 'AdcDev2', 'AdcDev3'])

    return


def main04():

    dacTest2504(['DacDev0', 'DacDev1', 'DacDev2', 'DacDev3'])

    print(' ')

    adcTest2504(['AdcDev0', 'AdcDev1', 'AdcDev2', 'AdcDev3'])

    return


def main05():

    dacTest2504(['DacDev0'])

    print(' ')

    adcTest2504(['AdcDev0', 'AdcDev1'])

    return


def vipMain06():

    print('Begin vipMain06(), ...')

    print('  Vcc Voltage, ...')

    dacTest3101(['DacDev0'], '3.00V')

    

    print(' ')

    print('  Load Voltage, ...')

    adcTest3101(['AdcDev0'])

    

    print('')

    print('  CSA Output Voltage, ...')

    adcDecimalResults = adcTest3101(['AdcDev1'])

    

    print('')

    print('  Load Current,  ...')

    print('    CSA output voltge =', '{:.2f}'.format(adcDecimalResults))

    print('')

    

    print('End   vipMain06(), ...')    

    return


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,

    }


def vipMain07():

    print('Begin vipMain06(), ...')

    print('  Vcc Voltage Setting, ...')

    dacTest3101(['DacDev0'], '3.00V')

    

    print(' ')

    print('  Vcc Voltage Reading, ...')

    adcDecimalResults0 = adcTest3101(['AdcDev0'])

    print('    Vcc Voltage Reading=', '{:.2f}'.format(adcDecimalResults0))

    

    print('')

    print('  Load Voltage 1 Reading, ...')

    adcDecimalResults1 = adcTest3101(['AdcDev1'])

    print('    Load Voltage 1 Reading =', '{:.2f}'.format(adcDecimalResults1))

    print('')


    print('  Load Voltage 2 Reading, ...')

    adcDecimalResults2 = adcTest3101(['AdcDev2'])

    print('    Load Voltage 2 Reading =', '{:.2f}'.format(adcDecimalResults2))

    print('')

    

    print('End   vipMain06(), ...')    

    return


def vipMain08():

    #dac.testSweepV02('Mcp4725I2cBus1Module0', [x * 16 for x in range(16)], 1)

    #dac.testSweepV02('Mcp4725I2cBus1Module0', [x * 512 for x in range(8)], 2)

    #dac.testSweepV03('Mcp4725I2cBus1Module0', [x * 256 for x in range(16)], 1)

    #dac.testSweepV04('Mcp4725I2cBus1Module0', [x * 16 for x in range(1024)], 0.0001, 2000)    

    return


def vipSweepV05(dacModuleName0, adcModuleName0, adcModuleName1, adcModuleName2, dcVoltSweepList, stepPauseSeconds, totalRepeatCount):

    print('dacModuleName0   =', dacModuleName0)

    print('adcModuleName0   =', adcModuleName0)

    print('adcModuleName0   =', adcModuleName1)

    print('dcVoltSweepList  =', dcVoltSweepList)

    print('stepPauseSeconds =', stepPauseSeconds)

    print('totalRepeatCount =', totalRepeatCount)

    

    for count in range(totalRepeatCount):

        for sweepStep in range(len(dcVoltSweepList)):

            print('\nsweepStep        =', sweepStep)

            digiVal = dcVoltSweepList[sweepStep]

            print('  digiVal        =', digiVal)

            dac.setDigiValToAnalogValNoPrint(dacModuleName0, digiVal)

            

            adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName0)  

            print('  anaVal 0       =', '{:.2f}'.format(adcDecimalResults), 'V')

 

            adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName1)  

            print('  anaVal 1       =', '{:.2f}'.format(adcDecimalResults), 'V')

            

            adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName2)  

            print('  anaVal 2       =', '{:.2f}'.format(adcDecimalResults), 'V')  


            

            sleep(stepPauseSeconds)

            #print('')

    return 

    return 


def vipMain09():

    vipSweepV05('Mcp4725I2cBus1Module0', 'Mcp3201SpiPort0Module0', 'Mcp3201SpiPort0Module1', 'Mcp3201SpiPort1Module0', \

                [x * 512 for x in range(8)], 0.001, 1)

    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

    

    #main01() 

    #main02()

    #main03()

    #main04()

    #main05()

    #vipMain06()

    #vipMain07()

    #vipMain08()

    #vipMain09()       


>>> %Run vip3201.py

dacModuleName0   = Mcp4725I2cBus1Module0

adcModuleName0   = Mcp3201SpiPort0Module0

adcModuleName0   = Mcp3201SpiPort0Module1

dcVoltSweepList  = [0, 512, 1024, 1536, 2048, 2560, 3072, 3584]

stepPauseSeconds = 0.001

totalRepeatCount = 1


sweepStep        = 0

  digiVal        = 0

  anaVal 0       = 0.00 V

  anaVal 1       = 0.00 V

  anaVal 2       = 0.00 V


sweepStep        = 1

  digiVal        = 512

  anaVal 0       = 0.37 V

  anaVal 1       = 0.00 V

  anaVal 2       = 0.19 V


sweepStep        = 2

  digiVal        = 1024

  anaVal 0       = 0.77 V

  anaVal 1       = 0.00 V

  anaVal 2       = 0.39 V


sweepStep        = 3

  digiVal        = 1536

  anaVal 0       = 1.16 V

  anaVal 1       = 0.00 V

  anaVal 2       = 0.59 V


sweepStep        = 4

  digiVal        = 2048

  anaVal 0       = 1.56 V

  anaVal 1       = 0.15 V

  anaVal 2       = 0.79 V


sweepStep        = 5

  digiVal        = 2560

  anaVal 0       = 1.96 V

  anaVal 1       = 0.37 V

  anaVal 2       = 0.98 V


sweepStep        = 6

  digiVal        = 3072

  anaVal 0       = 2.36 V

  anaVal 1       = 0.61 V

  anaVal 2       = 1.18 V


sweepStep        = 7

  digiVal        = 3584

  anaVal 0       = 2.76 V

  anaVal 1       = 0.83 V

  anaVal 2       = 1.38 V

>>>


def vipSweepV05(dacModuleName0, adcModuleName0, adcModuleName1, adcModuleName2, dcVoltSweepList, stepPauseSeconds, totalRepeatCount):

    print('dacModuleName0   =', dacModuleName0)

    print('adcModuleName0   =', adcModuleName0)

    print('adcModuleName0   =', adcModuleName1)

    print('dcVoltSweepList  =', dcVoltSweepList)

    print('stepPauseSeconds =', stepPauseSeconds)

    print('totalRepeatCount =', totalRepeatCount)

    

    for count in range(totalRepeatCount):

        for sweepStep in range(len(dcVoltSweepList)):

            print('\nsweepStep        =', sweepStep)

            digiVal = dcVoltSweepList[sweepStep]

            print('  digiVal        =', digiVal)

            dac.setDigiValToAnalogValNoPrint(dacModuleName0, digiVal)

            

            adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName0)  

            print('  anaVal 0       =', '{:.2f}'.format(adcDecimalResults), 'V')

 

            adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName1)  

            print('  anaVal 1       =', '{:.2f}'.format(adcDecimalResults), 'V')

            

            adcDecimalResults = adc.getMcp3201DecimalResultsNoPrint(adcModuleName2)  

            print('  anaVal 2       =', '{:.2f}'.format(adcDecimalResults), 'V')  

            

            sleep(stepPauseSeconds)

            #print('')

    return

    

def vipMain11():

    print('Begin vipMain10(), ...', 'tlfong01', dateTimeNowShort)

    #vipSweepV05('Mcp4725I2cBus1Module0', 'Mcp3201SpiPort0Module0', 'Mcp3201SpiPort0Module1', 'Mcp3201SpiPort1Module0', \

    #           [x * 512 for x in range(8)], 0.001, 1)

    dcVoltSweepList = [x * 512 for x in range(8)]

    stepPauseSeconds = 0.001

    totalRepeatCount = 1

    

    vipSweepV05(dacDev0Name, adcDev0Name, adcDev1Name, adcDev2Name, dcVoltSweepList, stepPauseSeconds, totalRepeatCount)    

    pauseSeconds('OneSecond')

    print('End   vipMain10().')

    return


def tunnelDiodePlotV01(tunnelDiodeName, plotName):

    print('Begin tunnelDiodePlotV01(), ...', 'tlfong01', dateTimeNowShort)

    #vipSweepV05('Mcp4725I2cBus1Module0', 'Mcp3201SpiPort0Module0', 'Mcp3201SpiPort0Module1', 'Mcp3201SpiPort1Module0', \

    #           [x * 512 for x in range(8)], 0.001, 1)

    dcVoltSweepList = [x * 512 for x in range(8)]

    stepPauseSeconds = 0.001

    totalRepeatCount = 1

    

    vipSweepV05(dacDev0Name, adcDev0Name, adcDev1Name, adcDev2Name, dcVoltSweepList, stepPauseSeconds, totalRepeatCount)    

    pauseSeconds('OneSecond')

    print('End   tunnelDiodePlotV01().')

    return


def tunnelDiodePlotV06(tunnelDiodeName, plotName):

    print('Begin tunnelDiodePlotV06(), ...', 'tlfong01', dateTimeNowShort)

    

    #vipSweepV05('Mcp4725I2cBus1Module0', 'Mcp3201SpiPort0Module0', 'Mcp3201SpiPort0Module1', 'Mcp3201SpiPort1Module0', \

    #           [x * 512 for x in range(8)], 0.001, 1)

    dcVoltSweepList = [x * 512 for x in range(8)]

    stepPauseSeconds = 0.001

    totalCount = 1

    

    dcvSweepV06(dacDev0Name, adcDev0Name, adcDev1Name, adcDev2Name, dcVoltSweepList, stepPauseSeconds, totalCount)    

    pauseSeconds('OneSecond')

    

    print('End   tunnelDiodePlotV06().')

    return

    

    def tunnelDiodePlotV07(tunnelDiodeName, dcvSweepListName, stepPauseSecondsName, totalCountName):

    print('Begin tunnelDiodePlotV06(), ...', 'tlfong01', dateTimeNowShort)

    

    dcvSweepList = dcvSweepListDict[dcvSweepListName]

    stepPauseSeconds = timeDict[stepPauseSecondsName]

    totalCount = countDict[totalCountName]

    

    dcvSweepV07(dacDev0Name, adcDev0Name, adcDev1Name, adcDev2Name, dcvSweepList, stepPauseSeconds, totalCount)    

    # pauseSeconds('OneSecond')

    

    print('End   tunnelDiodePlotV06().')

    return

    

    def tunnelDiodePlotV07(tunnelDiodeName, dcvSweepListName, stepPauseSecondsName, totalCountName):

    print('Begin tunnelDiodePlotV06(), ...', 'tlfong01', dateTimeNowShort)

    

    dcvSweepList = dcvSweepListDict[dcvSweepListName]

    stepPauseSeconds = timeDict[stepPauseSecondsName]

    totalCount = countDict[totalCountName]

    

    dcvSweepV07(dacDev0Name, adcDev0Name, adcDev1Name, adcDev2Name, dcvSweepList, stepPauseSeconds, totalCount)    

    # pauseSeconds('OneSecond')

    

    print('End   tunnelDiodePlotV06().')

    return

    

'''


# *** New Test Functions ***


dcvSweepListDict = \

    {        

        'DcvSweepList01' : [x * 512 for x in range(8)],

    }


timeDict = \

    {

        'OneMilliSecond' : 0.001,

        'OneSecond'      : 1,

    }


countDict = \

    {

        'OneTime' : 1,

    }


def dcvSweep08(dacModuleName0, adcModuleName0, adcModuleName1, adcModuleName2, dcvSweepList, stepPauseSeconds, totalCount):

    print('  dacModuleName0   =', dacModuleName0)

    print('  adcModuleName0   =', adcModuleName0)

    print('  adcModuleName1   =', adcModuleName1)

    print('  adcModuleName2   =', adcModuleName2)

    print('  dcvSweepList     =', dcvSweepList)

    print('  stepPauseSeconds =', stepPauseSeconds)

    print('  totalCount       =', totalCount)

    

    for count in range(totalCount):

        for sweepStep in range(len(dcvSweepList)):

            print('\n  sweepStep             =', sweepStep)

            digiVal = dcvSweepList[sweepStep]

            print('    digiVal   (Vcc)     =', digiVal)

            dac.setDigiValToAnalogValNoPrint(dacModuleName0, digiVal)

            

            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(stepPauseSeconds)

            #print('')

    return


def diodeIvPlot08(diodeName, dcvSweepListName, stepPauseSecondsName, totalCountName):

    print('Begin diodeIvPlot08(), ...', 'tlfong01', dateTimeNowShort)

    

    dcvSweepList = dcvSweepListDict[dcvSweepListName]

    stepPauseSeconds = timeDict[stepPauseSecondsName]

    totalCount = countDict[totalCountName]

    

    dcvSweep08(dacDev0Name, adcDev0Name, adcDev1Name, adcDev2Name, dcvSweepList, stepPauseSeconds, totalCount)    

    

    print('End   diodePlot08().')

    return


# *** Init/Main Function ***


def init():

    pass

    return


if __name__ == '__main__':

    diodeIvPlot08('1N4148', 'DcvSweepList01', 'OneMilliSecond', 'OneTime')

     

# *** End of Program ********************************************************************


# *** Sample Outputs ***


'''

>>> %Run vip3503.py

Begin diodeIvPlot08(), ... tlfong01 2021-01-14 22:06

  dacModuleName0   = Mcp4725I2cBus1Module0

  adcModuleName0   = Mcp3201SpiPort0Module0

  adcModuleName1   = Mcp3201SpiPort0Module1

  adcModuleName2   = Mcp3201SpiPort1Module0

  dcvSweepList     = [0, 512, 1024, 1536, 2048, 2560, 3072, 3584]

  stepPauseSeconds = 0.001

  totalCount       = 1


  sweepStep             = 0

    digiVal   (Vcc)     = 0

    digiVal 0 (Dv)      = 0.00 V

    digiVal 1 (Rv)      = 0.00 V

    digiVal 2 (Ri)      = 0.00 V


  sweepStep             = 1

    digiVal   (Vcc)     = 512

    digiVal 0 (Dv)      = 0.39 V

    digiVal 1 (Rv)      = 0.35 V

    digiVal 2 (Ri)      = 0.38 V


  sweepStep             = 2

    digiVal   (Vcc)     = 1024

    digiVal 0 (Dv)      = 0.79 V

    digiVal 1 (Rv)      = 0.74 V

    digiVal 2 (Ri)      = 0.53 V


  sweepStep             = 3

    digiVal   (Vcc)     = 1536

    digiVal 0 (Dv)      = 1.19 V

    digiVal 1 (Rv)      = 1.13 V

    digiVal 2 (Ri)      = 0.56 V


  sweepStep             = 4

    digiVal   (Vcc)     = 2048

    digiVal 0 (Dv)      = 1.59 V

    digiVal 1 (Rv)      = 1.54 V

    digiVal 2 (Ri)      = 0.58 V


  sweepStep             = 5

    digiVal   (Vcc)     = 2560

    digiVal 0 (Dv)      = 1.98 V

    digiVal 1 (Rv)      = 1.93 V

    digiVal 2 (Ri)      = 0.60 V


  sweepStep             = 6

    digiVal   (Vcc)     = 3072

    digiVal 0 (Dv)      = 2.39 V

    digiVal 1 (Rv)      = 2.30 V

    digiVal 2 (Ri)      = 0.61 V


  sweepStep             = 7

    digiVal   (Vcc)     = 3584

    digiVal 0 (Dv)      = 2.79 V

    digiVal 1 (Rv)      = 2.74 V

    digiVal 2 (Ri)      = 0.62 V

End   diodePlot08().

>>> 


'''

# *** 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)

    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 ***


















No comments:

Post a Comment

Joe Biden

RCA Tunnel Diode Manual - RCA 1966

 RCA Tunnel Diode Manual - RCA 1966 https://www.yumpu.com/en/document/read/23901571/rca-tunnel-diode-manual