Changed MCP3221 I2C read from two 1-byte reads to one 2-byte read

Dependents:   DISCO-F746NG_LCD_TS_ADC

Fork of MCP3221 by David Styles

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP3221.h Source File

MCP3221.h

00001 /*
00002 
00003 * ***** NOTE ***** **************************************************** *
00004 * THIS LIBRARY IS FOR AN MCP3221A5T-I/OT - WHICH HAS AN ADDRESS OF  101 *
00005 * ***** **** ***** **************************************************** *
00006 
00007  * Copyright (c) 2012 dstyles, MIT License
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
00010  * and associated documentation files (the "Software"), to deal in the Software without restriction, 
00011  * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
00012  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in all copies or 
00016  * substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
00019  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
00020  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
00021  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
00022  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00023  */
00024  
00025 /* Datasheet Information:
00026 *
00027 * The address byte is the first byte received following the
00028 * START condition from the master device. The first part
00029 * of the control byte consists of a 4-bit device code,
00030 * which is set to 1001 for the MCP3221. The device code
00031 * is followed by three address bits: A2, A1 and A0. The
00032 * default address bits are 101. Contact the Microchip
00033 * factory for additional address bit options. The address
00034 * bits allow up to eight MCP3221 devices on the same
00035 * bus and are used to determine which device is
00036 * accessed.
00037 *
00038 * The eighth bit of the slave address determines if the
00039 * master device wants to read conversion data or write to
00040 * the MCP3221. When set to a ‘1’, a read operation is
00041 * selected. When set to a ‘0’, a write operation is
00042 * selected. There are no writable registers on the
00043 * MCP3221. Therefore, this bit must be set to a ’1’ in
00044 * order to initiate a conversion.
00045 *
00046 * ***** NOTE ***** *****************************************************************************************
00047 * THIS LIBRARY IS FOR AN MCP3221A5T-I/OT - WHICH HAS AN ADDRESS OF  101  - The A5 bit denotes the address
00048 * ***** **** ***** *****************************************************************************************
00049 */
00050 
00051 
00052 #ifndef MCP3221_H
00053 
00054 #define MCP3221_H
00055 
00056 #include "mbed.h"
00057 
00058 #define    MCP3221_CONVERSE 0x9B //10011011 NOTE IT ENDS IN 1, this is the READ ADDRESS. This is all this device does.
00059                                  //It opens a conversation via this specific READ address
00060 
00061 //Library for the MCP3221 12 BIT ADC.
00062 
00063 class MCP3221
00064 {
00065 public:
00066 
00067   /*
00068   Creates instance
00069   Connect module using I2C port pins sda and scl. The output is referenced to the supply voltage which can be
00070   2.7v to 5.0v. The read will return the correct voltage, if you supply the correct supplyVoltage when instantiating.
00071   */
00072   MCP3221(PinName sda, PinName scl, float supplyVoltage);
00073   
00074   /*
00075   Destroys instance.
00076   */ 
00077   ~MCP3221();
00078   
00079   /*
00080   Reads the analog register of the MCP3221 and converts it to a useable value. (a voltage) 
00081   */
00082   float read();
00083   
00084 private:
00085   
00086   I2C i2c;
00087   float _supplyVoltage;
00088   char _data[2];
00089 
00090 };
00091 
00092 #endif
00093