MCP3XXX
An Arduino Library supporting the MCP3XXX series of ADC SPI chips, including MCP3002, MCP3004, MCP3008 and others.
MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength > Class Template Reference

A template class supporting MCP3XXX ADC SPI chips. More...

#include <MCP3XXX.h>

Public Types

enum  {
  ADC_ERROR_INVALID_CHANNEL = -1, ADC_UNSUPPORTED_CONFIGURATION = -2, NUM_BITS = NumBits, BIT_MASK = (1 << NUM_BITS) - 1,
  NUM_CHANNELS = NumChannels, MAX_SPI_CLOCK_SPEED = MaxSPIClockSpeed, SPI_TRANSFER_LEGNTH = SPITransferLength
}
 

Public Member Functions

 MCP3XXX_ ()
 Construct a default MCP3XXX_ device.
 
 ~MCP3XXX_ ()
 Destroy the MCP3XXX_ device.
 
void begin (uint8_t csPin=SS)
 Set up the ADC using default hardware SPI pins. More...
 
void begin (uint8_t csPin, uint8_t mosiPin, uint8_t misoPin, uint8_t sckPin)
 Set up the ADC using custom software SPI pins. More...
 
uint32_t analogRead (uint8_t channel) const
 Read the analog value. More...
 
uint32_t analogReadDifferential (uint8_t inPositiveChannel) const
 Read a differential analog value by specifying the IN+ channel. More...
 
size_t numChannels () const
 
size_t numBits () const
 

Detailed Description

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
class MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength >

A template class supporting MCP3XXX ADC SPI chips.

Template Parameters
NumBitsNumber of ADC bits.
NumChannelsNumber of input channels.
MaxSPIClockSpeedMaximum SPI communication speed rate in Hz.
SPITransferLengthThe number of bytes transferred over SPI.

Member Enumeration Documentation

◆ anonymous enum

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
anonymous enum
Enumerator
ADC_ERROR_INVALID_CHANNEL 

ADC error value.

ADC_UNSUPPORTED_CONFIGURATION 

ADC error value.

NUM_BITS 

Number of ADC bits.

BIT_MASK 

A bit mask based on the number of bits.

NUM_CHANNELS 

Number of input channels.

MAX_SPI_CLOCK_SPEED 

Maximum SPI communication speed rate in Hz.

SPI_TRANSFER_LEGNTH 

The number of bytes transferred over SPI.

Member Function Documentation

◆ analogRead()

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
uint32_t MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength >::analogRead ( uint8_t  channel) const
inline

Read the analog value.

Reads a single-ended analog value using the given channel.

Parameters
channelThe channel (channel < NUM_CHANNELS) to read.
Returns
values [0, MAX_VALUE) on success or an error code on failure.

◆ analogReadDifferential()

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
uint32_t MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength >::analogReadDifferential ( uint8_t  inPositiveChannel) const
inline

Read a differential analog value by specifying the IN+ channel.

Consecutive channel pairs can be differentially read. For instance, if inPositiveChannel == 0, inNegativeChannel will be 1. If inPositiveChannel == 1, then inNegativeChannel will be 0. Thus if inPositiveChannel is odd, inNegativeChannel == (inPositiveChannel - 1). if inPositiveChannel is even, inNegativeChannel == (inPositiveChannel + 1).

Parameters
inPositiveChannelThe channel that should be input positive.
Returns
Differential values. See the data sheet for information on how to interpret these return values.

◆ begin() [1/2]

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
void MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength >::begin ( uint8_t  csPin = SS)
inline

Set up the ADC using default hardware SPI pins.

Hardware SPI pins vary based on the board being used. These default pins are represented by the constants SS, MOSI, MISO and SCK.

See also
https://www.arduino.cc/en/Reference/SPI
Parameters
csPinChip Select Pin. Default value is SS.

◆ begin() [2/2]

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
void MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength >::begin ( uint8_t  csPin,
uint8_t  mosiPin,
uint8_t  misoPin,
uint8_t  sckPin 
)
inline

Set up the ADC using custom software SPI pins.

This method forces the SPI to be accesed via software methods rather than hardware SPI. This is true, even if the default hardware SPI pins are used.

Parameters
csPinChip Select Pin.
mosiPinMOSI pin.
misoPinMISO pin.
sckPinClock pin.

◆ numBits()

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
size_t MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength >::numBits ( ) const
inline
Returns
the number of ADC bits.

◆ numChannels()

template<uint8_t NumBits, uint8_t NumChannels, uint32_t MaxSPIClockSpeed, uint8_t SPITransferLength = 3>
size_t MCP3XXX_< NumBits, NumChannels, MaxSPIClockSpeed, SPITransferLength >::numChannels ( ) const
inline
Returns
the number of ADC channels.

The documentation for this class was generated from the following file: