MCP320x is an Arduino library (for IDE version 1.6.5 and later) that provides an SPI interface to the Microchip 3204 and 3208 12-bit analog to digital converter (ADC). This is a rewrite of an older, similar library (see http://arduino.cc/forum/index.php?topic=53082.0 and http://arduino.cc/playground/Code/MCP320).
This new version conforms to the SPI library SDK in IDE 1.6.5 and allows both 5V and 3.3V operation (voltage support is transparent and does not need to be specified by the user). Types and functions have been redefined so this library is not directly compatible with the previous versions. However, the main concept in the older library of allowing for both SPI as well as direct pin manipulation modes is preserved. While the pin manipulation mode may be useful if SPI is not an option, it is 7-8 times slower than the SPI interface.
MCP320x(uint8_t CS, uint8_t DIN, uint8_t DOUT, uint8_t CLK);
Pin mode constructor.
Parameter | Purpose |
---|---|
CS | chip select |
DIN | MCP data in |
DOUT | MCP data out |
CLK | clock |
MCP320x(uint8_t CS);
SPI mode constructor (uses standard SPI pins MISO, MOSI, and SCK (pin numbers depend on your MCU))
Parameter | Purpose |
---|---|
CS | chip select |
uint16_t readChannel(uint8_t channel);
Read the selected MCP channel and return the value. Returns MCP_CHANNEL_ERROR for channel values out of range.
bool readAllChannels(uint16_t channelValue[], uint8_t channelCount);
Read all channels up to channelCount into the given array (channelValue). Returns true if successful, else false.
MCPMode getMCPConfig(uint8_t channel);
Get configuration (single-ended(MCP_SINGLE) or differential(MCP_DIFFERENTIAL)) for the given channel. Returns MCP_RANGE_ERROR for values out of range.
bool setMCPConfig(MCPMode mode, uint8_t channel);
Sets channel configuration to single-ended(MCP_SINGLE) or differential(MCP_DIFFERENTIAL). Use MCP_ALL_PORTS as the channel parameter to set all channels. Returns true if successful, else false.
float rawToVoltage(float VREF, uint16_t ADCRawValue);
Convert output from readChannel()
, ADCRawValue, to volts using given reference voltage VREF. This must be the same as the voltage supplied to the MCP VREF pin.
- MCP320x_array.ino: Read the values of all ADC ports (with a single function call) and display raw values and voltage conversions.
- MCP320x_channel.ino: Read the values of all MCP ports (channel by channel) and display raw values and voltage conversions.
- MCP320x_config.ino: Demonstrate MCP configuration interface.
Go to Releases and download the latest release .zip archive. Install by using the "Sketch > Include Library > Add .ZIP Library..." menu commands in the Arduino IDE.
Of course, you may also clone this repository and manually install the files if you prefer.
Copyright 2016 Rob Redford This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit BY-NC-SA.