Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import iio
- iio_context = iio.Context()
- class Am335xAdc:
- def __init__( self, vrefp=1.8, vrefm=0.0 ):
- self.vrefp = vrefp
- self.vrefm = vrefm
- self._device = iio_context.find_device('TI-am335x-adc.0.auto')
- if self._device is None:
- raise RuntimeError("ADC not enabled");
- self._channels = []
- self._raw = []
- for i in range(8):
- ch = self._device.find_channel( f'voltage{i}' )
- self._channels.append( ch )
- if ch is not None:
- ch = ch.attrs['raw']
- self._raw.append( ch )
- # output range 0 .. 4095
- def raw( self, ch ):
- return int( self._raw[ ch ].value )
- # output range 0.0 .. 1.0
- def value( self, ch ):
- return self.raw( ch ) / 4095
- # output range vrefm .. vrefp
- def voltage( self, ch ):
- return self.value( ch ) * ( self.vrefp - self.vrefm ) + self.vrefm
- adc = Am335xAdc()
- for ch in range(7):
- v = adc.voltage( ch )
- print( f'ain{ch} = {v:.2f}V' )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement