Advertisement
zmatt

beaglebone adc iio.py

Jun 22nd, 2022 (edited)
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import iio
  4.  
  5. iio_context = iio.Context()
  6.  
  7. class Am335xAdc:
  8.     def __init__( self, vrefp=1.8, vrefm=0.0 ):
  9.         self.vrefp = vrefp
  10.         self.vrefm = vrefm
  11.         self._device = iio_context.find_device('TI-am335x-adc.0.auto')
  12.         if self._device is None:
  13.             raise RuntimeError("ADC not enabled");
  14.         self._channels = []
  15.         self._raw = []
  16.         for i in range(8):
  17.             ch = self._device.find_channel( f'voltage{i}' )
  18.             self._channels.append( ch )
  19.             if ch is not None:
  20.                 ch = ch.attrs['raw']
  21.             self._raw.append( ch )
  22.  
  23.     # output range 0 .. 4095
  24.     def raw( self, ch ):
  25.         return int( self._raw[ ch ].value )
  26.  
  27.     # output range 0.0 .. 1.0
  28.     def value( self, ch ):
  29.         return self.raw( ch ) / 4095
  30.  
  31.     # output range vrefm .. vrefp
  32.     def voltage( self, ch ):
  33.         return self.value( ch ) * ( self.vrefp - self.vrefm ) + self.vrefm
  34.  
  35. adc = Am335xAdc()
  36.  
  37. for ch in range(7):
  38.     v = adc.voltage( ch )
  39.     print( f'ain{ch} = {v:.2f}V' )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement