Friday 01 2021

MCP3201 Programming Log - Part 2 (AD Conversion Function Tested OK)

MCP3201 Programming Log - Part 2 (AD Conversion Function Tested OK)

# mcp3201v1701.py tlfong01 2021jan01hkt2258 ***

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

spiPortDict = \
{'SpiPort00': spiPort00,
     'SpiPort01': spiPort01,
    }

#*** System Functions ***

timeNowLong  = str(datetime.now())
timeNowShort = str(datetime.now())[0:16]

# *** SPI System Functions ***

# *** SPI Send/Receive 1/2/3 Bytes ***

def spiSendRecvOneByte(spiBus, sendByte):
    sendByteArray = [sendByte]
    recvByteArray = spiBus.xfer2(sendByteArray)    
    return recvByteArray

def spiSendRecvTwoBytes(spiBus, sendByte1, sendByte2):
    sendByteArray = [sendByte1, sendByte2]
    recvByteArray = spiBus.xfer2(sendByteArray)    
    return recvByteArray

def spiSendRecvThreeBytes(spiBus, sendByte1, sendByte2, sendByte3):
    sendByteArray = [sendByte1, sendByte2, sendByte3]
    recvByteArray = spiBus.xfer2(sendByteArray)    
    return recvByteArray

# *** MCP3201 Config ***

refVoltName = '3.00V'
dummyByte   = 0x00

refVoltDict = \
{
        '0.00V'  : 0.00,
        '1.68V'  : 1.68,
        '10.24V' : 1.024,
        '2.048V' : 2.048,
        '3.00V'  : 3.00,
        '3.30V'  : 3.30,
        '4.096V' : 4.096,
    }


# *** MCP3201 Functions ***

def getMcp3201BinaryResults(spiPortName, refVoltName):
    spiPort = spiPortDict[spiPortName]
    recvArray = spiSendRecvTwoBytes(spiPort, 0x00, 0x00)
    print('      spiPortName      =', spiPortName)
    print('      recvArray        =', recvArray)
    adcBinaryResults = (((recvArray[0] & 0x3f) << 8) + recvArray[1]) >> 1
    print('      adcBinaryResults =', hex(adcBinaryResults))
    return adcBinaryResults

def getMcp3201DecimalResults(adcBinaryResults):
    adcDecimalResults = (adcBinaryResults / 0xfff) * refVoltDict[refVoltName]
    return adcDecimalResults

# *** ADC Functions ***

def testAdc(spiPortName):
    print('    MCP3201 ADC Results:')
    adcBinaryResults  = getMcp3201BinaryResults(spiPortName, refVoltName)
    print(  'adcBinaryResults =', adcBinaryResults)
    adcDecimalResults = getMcp3201DecimalResults(adcBinaryResults)  
    #print('    MCP3201 Results in 12 bits binary (expect fff/2~= 0x0800) =', convertTwoByteNumToEightCharStr(adcBinaryResults))
    print('      Binary  =', hex(adcBinaryResults))
    print('      Decimal =', "%.2f" % adcDecimalResults, 'V')
    return

# *** Init/Main Function ***

def init():
    pass
    return

def main():
    print('Begin main(), ... tlfong01', timeNowShort)
    print('  Begin testMCP3201Adc(), ...')
    testAdc('SpiPort00')
    print('  End   testMCP3201Adc().')
    print('End   main().')
    return

# *** Main()

if __name__ == '__main__':
    main()

# *** End of Program ***

# *** Sample Output ***

'''
>>> %Run mcp3201v1601.py
Begin main(), ... tlfong01 2021-01-01 22:56
  Begin testMCP3201Adc(), ...
    MCP3201 ADC Results:
      spiPortName      = SpiPort00
      recvArray        = [15, 231]
      adcBinaryResults = 0x7f3
adcBinaryResults = 2035
      Binary  = 0x7f3
      Decimal = 1.49 V
  End   testMCP3201Adc().
End   main().
>>> 

'''

# *** End of Sample output ***

No comments:

Post a Comment

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