Advertisement
zmatt

beaglebone adc iio.py

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