forked from M-Labs/thermostat
ad7172: fix calibration
This commit is contained in:
parent
ba860e52ac
commit
34c2da4484
|
@ -43,7 +43,7 @@ impl<SPI: Transfer<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS>
|
||||||
|
|
||||||
let mut adc_mode = <regs::AdcMode as Register>::Data::empty();
|
let mut adc_mode = <regs::AdcMode as Register>::Data::empty();
|
||||||
adc_mode.set_ref_en(true);
|
adc_mode.set_ref_en(true);
|
||||||
adc_mode.set_mode(Mode::ContinuousConversion);
|
adc_mode.set_mode(Mode::Standby);
|
||||||
adc.write_reg(®s::AdcMode, &mut adc_mode)?;
|
adc.write_reg(®s::AdcMode, &mut adc_mode)?;
|
||||||
|
|
||||||
Ok(adc)
|
Ok(adc)
|
||||||
|
@ -96,16 +96,52 @@ impl<SPI: Transfer<u8, Error = E>, NSS: OutputPin, E: fmt::Debug> Adc<SPI, NSS>
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn disable_channel(
|
||||||
|
&mut self, index: u8
|
||||||
|
) -> Result<(), SPI::Error> {
|
||||||
|
self.update_reg(®s::Channel { index }, |data| {
|
||||||
|
data.set_enabled(false);
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn disable_all_channels(&mut self) -> Result<(), SPI::Error> {
|
||||||
|
for index in 0..4 {
|
||||||
|
self.update_reg(®s::Channel { index }, |data| {
|
||||||
|
data.set_enabled(false);
|
||||||
|
})?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
/// Calibrates offset registers
|
/// Calibrates offset registers
|
||||||
pub fn calibrate_offset(&mut self) -> Result<(), SPI::Error> {
|
pub fn calibrate(&mut self) -> Result<(), SPI::Error> {
|
||||||
|
// internal offset calibration
|
||||||
|
self.update_reg(®s::AdcMode, |adc_mode| {
|
||||||
|
adc_mode.set_mode(Mode::InternalOffsetCalibration);
|
||||||
|
})?;
|
||||||
|
while ! self.read_reg(®s::Status)?.ready() {}
|
||||||
|
|
||||||
|
// system offset calibration
|
||||||
self.update_reg(®s::AdcMode, |adc_mode| {
|
self.update_reg(®s::AdcMode, |adc_mode| {
|
||||||
adc_mode.set_mode(Mode::SystemOffsetCalibration);
|
adc_mode.set_mode(Mode::SystemOffsetCalibration);
|
||||||
})?;
|
})?;
|
||||||
while ! self.read_reg(®s::Status)?.ready() {}
|
while ! self.read_reg(®s::Status)?.ready() {}
|
||||||
|
|
||||||
|
// system gain calibration
|
||||||
self.update_reg(®s::AdcMode, |adc_mode| {
|
self.update_reg(®s::AdcMode, |adc_mode| {
|
||||||
adc_mode.set_mode(Mode::ContinuousConversion);
|
adc_mode.set_mode(Mode::SystemGainCalibration);
|
||||||
})?;
|
})?;
|
||||||
|
while ! self.read_reg(®s::Status)?.ready() {}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start_continuous_conversion(&mut self) -> Result<(), SPI::Error> {
|
||||||
|
let mut adc_mode = <regs::AdcMode as Register>::Data::empty();
|
||||||
|
adc_mode.set_ref_en(true);
|
||||||
|
adc_mode.set_mode(Mode::ContinuousConversion);
|
||||||
|
self.write_reg(®s::AdcMode, &mut adc_mode)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,10 +26,22 @@ impl Channels {
|
||||||
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
|
let mut adc = ad7172::Adc::new(pins.adc_spi, pins.adc_nss).unwrap();
|
||||||
// Feature not used
|
// Feature not used
|
||||||
adc.set_sync_enable(false).unwrap();
|
adc.set_sync_enable(false).unwrap();
|
||||||
// Setup channels
|
|
||||||
|
// Calibrate ADC channels individually
|
||||||
|
adc.disable_all_channels().unwrap();
|
||||||
|
|
||||||
|
adc.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1).unwrap();
|
||||||
|
adc.calibrate().unwrap();
|
||||||
|
adc.disable_channel(0).unwrap();
|
||||||
|
|
||||||
|
adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap();
|
||||||
|
adc.calibrate().unwrap();
|
||||||
|
adc.disable_channel(1).unwrap();
|
||||||
|
|
||||||
|
// Setup channels and start ADC
|
||||||
adc.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1).unwrap();
|
adc.setup_channel(0, ad7172::Input::Ain0, ad7172::Input::Ain1).unwrap();
|
||||||
adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap();
|
adc.setup_channel(1, ad7172::Input::Ain2, ad7172::Input::Ain3).unwrap();
|
||||||
adc.calibrate_offset().unwrap();
|
adc.start_continuous_conversion().unwrap();
|
||||||
|
|
||||||
Channels { channel0, channel1, adc, pwm }
|
Channels { channel0, channel1, adc, pwm }
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue