Skip to content

Latest commit

 

History

History
142 lines (130 loc) · 2.66 KB

README.md

File metadata and controls

142 lines (130 loc) · 2.66 KB

MCP342x

A Rust crate for interfacing with the MCP342x ADC.

dependences

  • log
  • esp_idf_hal::i2c
  • esp_idf_hal::delay::BLOCK

Usage

1. declare use mcp342x

use mcp342x::MCP342X;

2. decalre i2c

let i2c = peripherals.i2c0;
let sda = peripherals.pins.gpio0;//declare sda pin
let scl = peripherals.pins.gpio1;//declare scl pin
let config = i2c::I2cConfig::new().baudrate(100.kHz().into());
let mut i2c = i2c::I2cDriver::new(i2c, sda, scl, &config)?;

3. instantiate

Argument of new() is device address. 7bits device address is 0b110_1xxx. xxx is selectable. datasheet(P.21,Table5-3) or table below. For example, if adr0 is high and adr1 is low, slave address is "0b1101100".

let adc =MCP342X::new(0b1101000);
I²C Device Address Bits Logic Status of Address Selection Pins
A2 A1 A0 Adr0 pin Adr1 pin
0 0 0 Low Low
0 0 1 Low Float
0 1 0 Low High
1 0 0 High Low
1 0 1 High Float
1 1 0 HIgh High
0 1 1 Float Low
1 1 1 Float High
0 0 0 Float Float

4. Get voltage

After instantiate, you can get voltage data as f64 using read_and_convert_adc. You can select channel, samplerate, pga.

Argument-num:

  1. I2C
  2. channel
    • 1, 2, 3(MCP3424 only), 4(MCP3424 only)
  3. sample rate
    • 12, 14, 16, 18 bit
  4. pga
    • x1, x2, x4, x8
let voltage=adc.read_and_convert_mcp342x(&mut i2c,1,16,1)?;  //chanel=1,16bitmode,pga=x1

Advenced use

You can get raw data(2 or 3 bytes) from mcp342x using read_mcp3424() and convert raw data using read_and_convert_adc(). See script if you know details.

Testing environment(2023/10/12)

software

  • WSL2 (Ubuntu 20.04)
  • esp-idf-sys = { version = "=0.32", features = ["binstart"] }
  • esp-idf-svc = { version="=0.45", features = ["experimental", "alloc"] }
  • embedded-svc = "0.24"
  • log = "0.4"
  • anyhow = "1"
  • embedded-hal = "=1.0.0-alpha.9"
  • esp-idf-hal = "0.40.1"

hardware

  • M5Stamp c3 Mate
  • mcp3424