├── .gitignore ├── LICENSE.md ├── README.md ├── examples ├── Arduino │ └── ADCDemo │ │ └── ADCDemo.ino └── chipKIT │ └── ADCDemo │ └── ADCDemo.ino ├── library.properties ├── src ├── MCP3208.cpp └── MCP3208.h └── uecide.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | 15 | # Compiler output 16 | *.hex 17 | *.lss 18 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2018, Majenko Technologies 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without modification, 5 | are permitted provided that the following conditions are met: 6 | 7 | * Redistributions of source code must retain the above copyright notice, this 8 | list of conditions and the following disclaimer. 9 | 10 | * Redistributions in binary form must reproduce the above copyright notice, this 11 | list of conditions and the following disclaimer in the documentation and/or 12 | other materials provided with the distribution. 13 | 14 | * Neither the name of Majenko Technologies nor the names of its 15 | contributors may be used to endorse or promote products derived from 16 | this software without specific prior written permission. 17 | 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 25 | ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MCP3208 2 | ======= 3 | 4 | Arduino library for MCP3208 8 channel, 12 bit, ADC chip 5 | 6 | Usage 7 | ----- 8 | 9 | The library provides a constructor which takes a chip select pin number. 10 | To use the library you must first create a new MCP3208 object with the 11 | pin number. On the chipKIT boards you also get the option of passing a 12 | DSPI object to use a different SPI bus than the default: 13 | 14 | Arduino or chipKIT: 15 | 16 | MCP3208 myADC(10); 17 | 18 | chipKIT only: 19 | 20 | DSPI1 mySPI; 21 | MCP3208 myADC(&mySPI, 10); 22 | 23 | You can also miss out all parameters and the default chip select pin 24 | of 10 will be used. 25 | 26 | Reading the ADC values can be done in two ways. The chip supports both 27 | normal linear analog reading (as the on-board ADC on Arduino and chipKIT 28 | boards provides), and also differential reading; that is the difference 29 | between the voltages applied to two adjacent channels. 30 | 31 | To do a normal straight reading use the analogRead() member function of 32 | the MCP3208 object. For instance, to read from channel 3: 33 | 34 | int myValue = myADC.analogRead(3); 35 | 36 | For differential reading channels are numbered 0 to 3 instead of 0 to 7 37 | and form the single-ended pairs 0+1, 2+3, 4+5 and 6+7. The analogReadDif() 38 | member function is used to read a differential value, so to read the 39 | voltage difference between inputs 4 and 5, you would use: 40 | 41 | int myValue = myADC.analogReadDif(2); 42 | 43 | Differential reading is done in either one or two stages depending on the 44 | voltages applied. If the voltage difference is positive then the channels 45 | are read simultaneously and the value returned. However, if the difference 46 | is negative the first reading will return an invalid value, so the channel 47 | assignments are then swapped and a new reading taken. The negative of this 48 | new value is then returned, thus giving you the full positive and negative 49 | difference ranges. 50 | 51 | For more detail on how the chip works you should consult the datasheet: 52 | 53 | http://ww1.microchip.com/downloads/en/DeviceDoc/21298c.pdf 54 | -------------------------------------------------------------------------------- /examples/Arduino/ADCDemo/ADCDemo.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | MCP3208 adc(10); 4 | 5 | void setup() { 6 | adc.begin(); 7 | Serial.begin(9600); 8 | } 9 | 10 | void loop() { 11 | char temp[10]; 12 | static int ctr = 0; 13 | if (ctr == 0) { 14 | Serial.println(); 15 | Serial.println(" Single Ended | Differential"); 16 | Serial.println(" 0 1 2 3 4 5 6 7 | 0 1 2 3"); 17 | Serial.println("--------------------------------------------------------------"); 18 | } 19 | ctr++; 20 | if (ctr == 10) { 21 | ctr = 0; 22 | } 23 | for (int i = 0; i < 8; i++) { 24 | sprintf(temp, "%5d", adc.analogRead(i)); 25 | Serial.print(temp); 26 | } 27 | Serial.print(" |"); 28 | for (int i = 0; i < 4; i++) { 29 | sprintf(temp, "%5d", adc.analogReadDif(i)); 30 | Serial.print(temp); 31 | } 32 | Serial.println(); 33 | delay(100); 34 | } 35 | -------------------------------------------------------------------------------- /examples/chipKIT/ADCDemo/ADCDemo.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | DSPI0 spi; 5 | MCP3208 adc(&spi, 10); 6 | 7 | void setup() { 8 | adc.begin(); 9 | Serial.begin(9600); 10 | } 11 | 12 | void loop() { 13 | char temp[10]; 14 | static int ctr = 0; 15 | if (ctr == 0) { 16 | Serial.println(); 17 | Serial.println(" Single Ended | Differential"); 18 | Serial.println(" 0 1 2 3 4 5 6 7 | 0 1 2 3"); 19 | Serial.println("--------------------------------------------------------------"); 20 | } 21 | ctr++; 22 | if (ctr == 10) { 23 | ctr = 0; 24 | } 25 | for (int i = 0; i < 8; i++) { 26 | sprintf(temp, "%5d", adc.analogRead(i)); 27 | Serial.print(temp); 28 | } 29 | Serial.print(" |"); 30 | for (int i = 0; i < 4; i++) { 31 | sprintf(temp, "%5d", adc.analogReadDif(i)); 32 | Serial.print(temp); 33 | } 34 | Serial.println(); 35 | delay(100); 36 | } 37 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=MCP3208 2 | version=1.0.11 3 | author=Matt Jenkins 4 | maintainer=Matt Jenkins 5 | sentence=MCP3208 ADC library 6 | paragraph=MCP3208\n=======\n\nArduino library for MCP3208 8 channel, 12 bit, ADC chip\n\nUsage\n-----\n\nThe library provides a constructor which takes a chip select pin number.\nTo use the library you must first create a new MCP3208 object with the\npin number. On the chipKIT boards you also get the option of passing a\nDSPI object to use a different SPI bus than the default:\n\nArduino or chipKIT:\n\n MCP3208 myADC(10);\n\nchipKIT only:\n\n DSPI1 mySPI;\n MCP3208 myADC(&mySPI, 10);\n\nYou can also miss out all parameters and the default chip select pin\nof 10 will be used.\n\nReading the ADC values can be done in tw 7 | url=https://github.com/MajenkoLibraries/MCP3208 8 | category=Signal Input/Output 9 | architectures=* 10 | includes=MCP3208.h 11 | -------------------------------------------------------------------------------- /src/MCP3208.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Majenko Technologies 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * 3. Neither the name of Majenko Technologies nor the names of its contributors may be used 16 | * to endorse or promote products derived from this software without 17 | * specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | 32 | #include 33 | 34 | void MCP3208::begin() { 35 | pinMode(_cs, OUTPUT); 36 | digitalWrite(_cs, HIGH); 37 | #ifdef __PIC32MX__ 38 | _spi->begin(); 39 | #else 40 | SPI.begin(); 41 | #endif 42 | } 43 | 44 | uint8_t MCP3208::spiTransfer(uint8_t i) { 45 | uint8_t d; 46 | #ifdef __PIC32MX__ 47 | d = _spi->transfer(i); 48 | #else 49 | d = SPI.transfer(i); 50 | #endif 51 | return d; 52 | } 53 | 54 | uint16_t MCP3208::analogRead(uint8_t pin) { 55 | uint8_t addr = 0b01100000 | ((pin & 0b111) << 2); 56 | digitalWrite(_cs, LOW); 57 | (void) spiTransfer(addr); 58 | uint8_t b1 = spiTransfer(0); 59 | uint8_t b2 = spiTransfer(0); 60 | digitalWrite(_cs, HIGH); 61 | 62 | return (b1 << 4) | (b2 >> 4); 63 | } 64 | 65 | int16_t MCP3208::analogReadDif(uint8_t pin) { 66 | uint8_t diff; 67 | uint8_t b1, b2; 68 | uint8_t addr = 0b01000000 | ((pin & 0b11) << 3); 69 | digitalWrite(_cs, LOW); 70 | (void) spiTransfer(addr); 71 | b1 = spiTransfer(0); 72 | b2 = spiTransfer(0); 73 | digitalWrite(_cs, HIGH); 74 | 75 | diff = (b1 << 4) | (b2 >> 4); 76 | if (diff > 0) { 77 | return diff; 78 | } 79 | addr = 0b01000100 | ((pin & 0b11) << 3); 80 | digitalWrite(_cs, LOW); 81 | (void) spiTransfer(addr); 82 | b1 = spiTransfer(0); 83 | b2 = spiTransfer(0); 84 | digitalWrite(_cs, HIGH); 85 | diff = (b1 << 4) | (b2 >> 4); 86 | return -diff; 87 | } 88 | -------------------------------------------------------------------------------- /src/MCP3208.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2014, Majenko Technologies 3 | * All rights reserved. 4 | * 5 | * Redistribution and use in source and binary forms, with or without modification, 6 | * are permitted provided that the following conditions are met: 7 | * 8 | * 1. Redistributions of source code must retain the above copyright notice, 9 | * this list of conditions and the following disclaimer. 10 | * 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, 12 | * this list of conditions and the following disclaimer in the documentation 13 | * and/or other materials provided with the distribution. 14 | * 15 | * 3. Neither the name of Majenko Technologies nor the names of its contributors may be used 16 | * to endorse or promote products derived from this software without 17 | * specific prior written permission. 18 | * 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 26 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 27 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 | */ 30 | 31 | 32 | #ifndef _MCP3208_H 33 | #define _MCP3208_H 34 | 35 | #if (ARDUINO >= 100) 36 | # include 37 | #else 38 | # include 39 | #endif 40 | 41 | #ifdef __PIC32MX__ 42 | #include 43 | #else 44 | #include 45 | #endif 46 | 47 | class MCP3208 { 48 | private: 49 | // Private functions and variables here. They can only be accessed 50 | // by functions within the class. 51 | uint8_t _cs; 52 | #ifdef __PIC32MX__ 53 | DSPI *_spi; 54 | #endif 55 | uint8_t spiTransfer(uint8_t); 56 | 57 | public: 58 | // Public functions and variables. These can be accessed from 59 | // outside the class. 60 | #ifdef __PIC32MX__ 61 | MCP3208() : _spi(new DSPI0), _cs(10) {} 62 | MCP3208(uint8_t cs) : _spi(new DSPI0), _cs(cs) {} 63 | MCP3208(DSPI *dspi) : _spi(dspi), _cs(10) {} 64 | MCP3208(DSPI *dspi, uint8_t cs) : _spi(dspi), _cs(cs) {} 65 | #else 66 | MCP3208() : _cs(10) {} 67 | MCP3208(uint8_t cs) : _cs(cs) {} 68 | #endif 69 | 70 | void begin(); 71 | uint16_t analogRead(uint8_t pin); 72 | int16_t analogReadDif(uint8_t pin); 73 | }; 74 | #endif 75 | -------------------------------------------------------------------------------- /uecide.json: -------------------------------------------------------------------------------- 1 | {"name":"MCP3208","provides":"MCP3208.h","license":"LICENSE.md","readme":"README.md","description":"MCP3208 ADC library","category":"IO","subcategory":"Analog","maintainer":"Matt Jenkins ","version":"1.0.11","family":"all","core":"all","install":{"examples":"\/","library.properties":"\/","src":"\/"}} --------------------------------------------------------------------------------