ads111x

package module
v0.0.0-...-0d3bf80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 11, 2017 License: MIT Imports: 5 Imported by: 0

README

GoDoc

ads111x

Go package for interfacing with ADS111x 16-bit analog to digital converters from Adafruit.

NOTE: Never apply more than VDD volts to the analog inputs. See the datasheet for full details on device operation.

Installation

go get github.com/dgnorton/ads111x

Getting started

Here's an example program that opens the device, sets a couple config settings, and reads voltages from the device's analog inputs.

func main() {
	// Open the device.
	adc, err := ads111x.Open("/dev/i2c-1", ads111x.Addr48)
	if err != nil {
		log.Fatal(err)
	}
	// Set the mode to continuous acquisition.
	if err := adc.SetMode(ads111x.Continuous); err != nil {
		log.Fatal(err)
	}
	// Set the scale to +/- 6.144 volts. (again, see note above & datasheet)
	if err := adc.SetScale(ads111x.Scale_6_144V); err != nil {
		log.Fatal(err)
	}

	for {
		// Read the voltage across analog inputs 0 & 1.
		v, err := adc.ReadVolts(ads111x.AIN_0_1)
		if err != nil {
			log.Fatal(err)
		}
		// Print the voltage and take a short nap.
		fmt.Printf("%f v\n", v)
		time.Sleep(time.Second)
	}
}

Compiling

To build for an RPi 2:

GOARM=7 GOARCH=arm GOOS=linux go build

Compatibility

This library has only been tested on an RPi 2 running Linux.

Documentation

Index

Constants

View Source
const (
	Addr48 I2CAddress = 0x48
	Addr49            = 0x49
	Addr4A            = 0x4A
	Addr4B            = 0x4B
)
View Source
const (
	// ConversionReg is the address of the conversion register.
	ConversionReg byte = iota
	// ConfigReg is the address of the config register.
	ConfigReg
	// LoThresReg is the address of the Lo_thresh register.
	LoThreshReg
	// HiThreshReg is the address of the Hi_thresh register.
	HiThreshReg
)
View Source
const (
	Status_LSB  uint8  = 15
	Status_Mask uint16 = uint16(1 << Status_LSB)
)
View Source
const (
	// Busy means a conversion is currently being performed.
	Busy = iota << Status_LSB
	// Idle means a conversion is not currently being performed.
	Idle
)
View Source
const (
	AIN_LSB  uint8  = 12
	AIN_Mask uint16 = uint16(7 << AIN_LSB)
)
View Source
const (
	Scale_LSB  uint8  = 9
	Scale_Mask uint16 = uint16(7 << Scale_LSB)
)
View Source
const (
	Mode_LSB  uint8  = 8
	Mode_Mask uint16 = uint16(1 << Mode_LSB)
)
View Source
const (
	DataRate_LSB  uint8  = 5
	DataRate_Mask uint16 = uint16(7 << DataRate_LSB)
)
View Source
const (
	ComparatorMode_LSB  uint8  = 4
	ComparatorMode_Mask uint16 = uint16(1 << ComparatorMode_LSB)
)
View Source
const (
	ComparatorPolarity_LSB  uint8  = 3
	ComparatorPolarity_Mask uint16 = uint16(1 << ComparatorPolarity_LSB)
)
View Source
const (
	ComparatorLatching_LSB  uint8  = 2
	ComparatorLatching_Mask uint16 = uint16(1 << ComparatorLatching_LSB)
)
View Source
const (
	ComparatorQueue_LSB  uint8  = 0
	ComparatorQueue_Mask uint16 = uint16(3 << ComparatorQueue_LSB)
)
View Source
const DefaultConfig = uint16(0x8583)

DefaultConfig is the configuration the device boots with.

View Source
const Resolution = 1 << 16

Resolution is the resolution of the ADC.

Variables

This section is empty.

Functions

func ScaleMinMax

func ScaleMinMax(fs Scale) (min, max float64)

ScaleMinMax returns the min and max voltages for the given full scale value.

func ScaleRange

func ScaleRange(fs Scale) float64

ScaleRange returns the difference between max and min for the full scale value.

Types

type ADC

type ADC struct {
	// contains filtered or unexported fields
}

ADC represents an ADS1113, ADS1114, or ADS1115 analog to digital converter.

func Open

func Open(dev string, addr I2CAddress) (*ADC, error)

Open returns a new ADC initialized and ready for use. dev is the I2C bus device, e.g., /dev/i2c-1

func (*ADC) Close

func (adc *ADC) Close() error

Close closes the ADC connection.

func (*ADC) ComparatorLatching

func (adc *ADC) ComparatorLatching() (ComparatorLatching, error)

ComparatorLatching returns the comparator latching.

func (*ADC) ComparatorMode

func (adc *ADC) ComparatorMode() (ComparatorMode, error)

ComparatorMode returns the comparator mode.

func (*ADC) ComparatorPolarity

func (adc *ADC) ComparatorPolarity() (ComparatorPolarity, error)

ComparatorPolarity returns the comparator polarity.

func (*ADC) ComparatorQueue

func (adc *ADC) ComparatorQueue() (ComparatorQueue, error)

ComparatorQueue returns the comparator queuing mode.

func (*ADC) Config

func (adc *ADC) Config() (uint16, error)

Config returns the device config.

func (*ADC) DataRate

func (adc *ADC) DataRate() (DataRate, error)

DataRate returns the data rate (samples/second).

func (*ADC) Mode

func (adc *ADC) Mode() (Mode, error)

Mode returns the mode config setting.

func (*ADC) Read

func (adc *ADC) Read(buf []byte) error

Read reads from the device.

func (*ADC) ReadAIN

func (adc *ADC) ReadAIN(input AIN) (uint16, error)

ReadAIN reads the value from the specified input.

func (*ADC) ReadReg

func (adc *ADC) ReadReg(reg byte, buf []byte) error

ReadReg reads a register.

func (*ADC) ReadRegUint16

func (adc *ADC) ReadRegUint16(reg byte) (uint16, error)

ReadRegUint16 reads a register and returns the result as a uint16.

func (*ADC) ReadVolts

func (adc *ADC) ReadVolts(input AIN) (float64, error)

ReadVolts reads the voltage from the specified input.

func (*ADC) Scale

func (adc *ADC) Scale() (Scale, error)

Scale returns the full scale config setting.

func (*ADC) SetComparatorLatching

func (adc *ADC) SetComparatorLatching(cl ComparatorLatching) error

SetComparatorLatching sets the comparator latching.

func (*ADC) SetComparatorMode

func (adc *ADC) SetComparatorMode(cm ComparatorMode) error

SetComparatorMode sets the comparator mode to Traditional or Window.

func (*ADC) SetComparatorPolarity

func (adc *ADC) SetComparatorPolarity(cp ComparatorPolarity) error

SetComparatorPolarity sets the comparator polarity.

func (*ADC) SetComparatorQueue

func (adc *ADC) SetComparatorQueue(cq ComparatorQueue) error

SetComparatorQueue sets the comparator queuing mode.

func (*ADC) SetDataRate

func (adc *ADC) SetDataRate(dr DataRate) error

SetDataRate sets the number of samples per second.

func (*ADC) SetMode

func (adc *ADC) SetMode(m Mode) error

SetMode sets the mode of operation (continuous or single).

func (*ADC) SetScale

func (adc *ADC) SetScale(fs Scale) error

SetScale sets the full scale range.

func (*ADC) Status

func (adc *ADC) Status() (Status, error)

Status returns the current status. A Busy status indicates that it is currently performing a conversion and Idle means it's not.

func (*ADC) Write

func (adc *ADC) Write(buf []byte) error

Write writes bytes to the device.

func (*ADC) WriteConfig

func (adc *ADC) WriteConfig(cfg uint16) error

WriteConfig writes a new config to the device.

func (*ADC) WriteReg

func (adc *ADC) WriteReg(reg byte, data interface{}) error

WriteReg writes a value to a register on the device.

type AIN

type AIN uint16
const (
	// AIN_0_1 is used to select AIN0 (pos) & AIN1 (neg) inputs (default).
	AIN_0_1 AIN = iota << AIN_LSB
	// AIN_0_3 is used to select AIN0 (pos) & AIN3 (neg) inputs.
	AIN_0_3
	// AIN_1_3 is used to select AIN1 (pos) & AIN3 (neg) inputs.
	AIN_1_3
	// AIN_2_3 is used to select AIN2 (pos) & AIN3 (neg) inputs.
	AIN_2_3
	// AIN_0_GND is used to select AIN0 (pos) & GND (neg) inputs.
	AIN_0_GND
	// AIN_1_GND is used to select AIN1 (pos) & GND (neg) inputs.
	AIN_1_GND
	// AIN_2_GND is used to select AIN2 (pos) & GND (neg) inputs.
	AIN_2_GND
	// AIN_3_GND is used to select AIN3 (pos) & GND (neg) inputs.
	AIN_3_GND
)

type ComparatorLatching

type ComparatorLatching uint16
const (
	// Off is used to set the comparator to non-latching (default).
	Off ComparatorLatching = iota << ComparatorLatching_LSB
	//On is used to set the comparator to latching.
	On
)

type ComparatorMode

type ComparatorMode uint16
const (
	// Traditional is used to set traditional comparator with histeresis (default).
	Traditional ComparatorMode = iota << ComparatorMode_LSB
	// Window is used to set window comparator mode.
	Window
)

type ComparatorPolarity

type ComparatorPolarity uint16
const (
	// ActiveLow is used to set polarity of ALERT/RDY pin to active low (default).
	ActiveLow ComparatorPolarity = iota << ComparatorPolarity_LSB
	// ActiveHigh is used to set polarity of ALERT/RDY pin to active high.
	ActiveHigh
)

type ComparatorQueue

type ComparatorQueue uint16
const (
	// CQ_AfterOne is used to set number of successive conversions exceeding upper or lower
	// thresholds before asserting ALERT/RDY pin.
	AfterOne ComparatorQueue = iota << ComparatorQueue_LSB
	AfterTwo
	AfterFour
	Disable // (default)
)

type DataRate

type DataRate uint16
const (
	// DR_8SPS is used to set the data rate to 8 samples per second.
	DR_8SPS DataRate = iota << DataRate_LSB
	// DR_16SPS is used to set the data rate to 16 samples per second.
	DR_16SPS
	// DR_32SPS is used to set the data rate to 32 samples per second.
	DR_32SPS
	// DR_64SPS is used to set the data rate to 64 samples per second.
	DR_64SPS
	// DR_128SPS is used to set the data rate to 128 samples per second (default).
	DR_128SPS
	// DR_250SPS is used to set the data rate to 250 samples per second.
	DR_250SPS
	// DR_475SPS is used to set the data rate to 475 samples per second.
	DR_475SPS
	// DR_860SPS is used to set the data rate to 860 samples per second.
	DR_860SPS
)

type I2CAddress

type I2CAddress uint8

I2CAddress represents one of the possible I2C bus addresses.

type Mode

type Mode uint16
const (
	// Continuous is used to set continuous conversion mode.
	Continuous Mode = iota << Mode_LSB
	// Single is used to set Power-down single-shot mode (default).
	Single
)

type Scale

type Scale uint16
const (
	// Scale_6_144V is used to set full scale range to +/- 6.144V.
	Scale_6_144V Scale = iota << Scale_LSB
	// Scale_4_096V is used to set full scale range to +/- 4.096V.
	Scale_4_096V
	// Scale_2_048V is used to set full scale range to +/- 2.048V (default).
	Scale_2_048V
	// Scale_1_024V is used to set full scale range to +/- 1.024V.
	Scale_1_024V
	// Scale_0_512V is used to set full scale range to +/- 0.512V.
	Scale_0_512V
	// Scale_0_256V is used to set full scale range to +/- 0.256V.
	Scale_0_256V
)

type Status

type Status uint16

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL