From 9d005392012213dc12527b913a9ea4f72b97368c Mon Sep 17 00:00:00 2001 From: Diego Barrios Romero Date: Sat, 17 Nov 2018 07:51:12 +0100 Subject: Add support for setting the full-scale range (PGA) --- src/devices/features/tier2.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) (limited to 'src/devices') diff --git a/src/devices/features/tier2.rs b/src/devices/features/tier2.rs index 25b618a..067139a 100644 --- a/src/devices/features/tier2.rs +++ b/src/devices/features/tier2.rs @@ -2,8 +2,9 @@ //! //! These are the features included only in ADS1x14, ADS1x15 -use { Ads1x1x, Error, interface, ic, ComparatorMode, ComparatorPolarity, - ComparatorLatching, ComparatorQueue, Register, BitFlags, conversion }; +use { Ads1x1x, Error, interface, ic, FullScaleRange, ComparatorMode, + ComparatorPolarity, ComparatorLatching, ComparatorQueue, Register, + BitFlags, conversion }; impl Ads1x1x where @@ -11,6 +12,25 @@ where IC: ic::Tier2Features, CONV: conversion::ConvertThreshold { + /// Set the input voltage measurable range + /// + /// This configures the programmable gain amplifier and determines the measurable input voltage range. + pub fn set_full_scale_range(&mut self, range: FullScaleRange) -> Result<(), Error> { + use BitFlags as BF; + let config; + match range { + FullScaleRange::Within6_144V => config = self.config.with_low( BF::PGA2).with_low( BF::PGA1).with_low( BF::PGA0), + FullScaleRange::Within4_096V => config = self.config.with_low( BF::PGA2).with_low( BF::PGA1).with_high(BF::PGA0), + FullScaleRange::Within2_048V => config = self.config.with_low( BF::PGA2).with_high(BF::PGA1).with_low( BF::PGA0), + FullScaleRange::Within1_024V => config = self.config.with_low( BF::PGA2).with_high(BF::PGA1).with_high(BF::PGA0), + FullScaleRange::Within0_512V => config = self.config.with_high(BF::PGA2).with_low( BF::PGA1).with_low( BF::PGA0), + FullScaleRange::Within0_256V => config = self.config.with_high(BF::PGA2).with_low( BF::PGA1).with_high(BF::PGA0), + } + self.iface.write_register(Register::CONFIG, config.bits)?; + self.config = config; + Ok(()) + } + /// Set comparator lower threshold pub fn set_low_threshold(&mut self, value: i16) -> Result<(), Error> { let register_value = CONV::convert_threshold(value)?; -- cgit v1.2.3-54-g00ecf