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

Committer:
pampt
Date:
Thu Feb 02 06:10:38 2017 +0000
Revision:
1:2921687d299e
Parent:
0:db4e3d0374fe
Changed MCP3221 I2C read to a 2-byte read from two 1-byte reads

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DaveStyles 0:db4e3d0374fe 1 /*
DaveStyles 0:db4e3d0374fe 2
DaveStyles 0:db4e3d0374fe 3 * ***** NOTE ***** **************************************************** *
DaveStyles 0:db4e3d0374fe 4 * THIS LIBRARY IS FOR AN MCP3221A5T-I/OT - WHICH HAS AN ADDRESS OF 101 *
DaveStyles 0:db4e3d0374fe 5 * ***** **** ***** **************************************************** *
DaveStyles 0:db4e3d0374fe 6
DaveStyles 0:db4e3d0374fe 7 * Copyright (c) 2012 dstyles, MIT License
DaveStyles 0:db4e3d0374fe 8 *
DaveStyles 0:db4e3d0374fe 9 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
DaveStyles 0:db4e3d0374fe 10 * and associated documentation files (the "Software"), to deal in the Software without restriction,
DaveStyles 0:db4e3d0374fe 11 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
DaveStyles 0:db4e3d0374fe 12 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
DaveStyles 0:db4e3d0374fe 13 * furnished to do so, subject to the following conditions:
DaveStyles 0:db4e3d0374fe 14 *
DaveStyles 0:db4e3d0374fe 15 * The above copyright notice and this permission notice shall be included in all copies or
DaveStyles 0:db4e3d0374fe 16 * substantial portions of the Software.
DaveStyles 0:db4e3d0374fe 17 *
DaveStyles 0:db4e3d0374fe 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
DaveStyles 0:db4e3d0374fe 19 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
DaveStyles 0:db4e3d0374fe 20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DaveStyles 0:db4e3d0374fe 21 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
DaveStyles 0:db4e3d0374fe 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
DaveStyles 0:db4e3d0374fe 23 */
DaveStyles 0:db4e3d0374fe 24
DaveStyles 0:db4e3d0374fe 25 /* Datasheet Information:
DaveStyles 0:db4e3d0374fe 26 *
DaveStyles 0:db4e3d0374fe 27 * The address byte is the first byte received following the
DaveStyles 0:db4e3d0374fe 28 * START condition from the master device. The first part
DaveStyles 0:db4e3d0374fe 29 * of the control byte consists of a 4-bit device code,
DaveStyles 0:db4e3d0374fe 30 * which is set to 1001 for the MCP3221. The device code
DaveStyles 0:db4e3d0374fe 31 * is followed by three address bits: A2, A1 and A0. The
DaveStyles 0:db4e3d0374fe 32 * default address bits are 101. Contact the Microchip
DaveStyles 0:db4e3d0374fe 33 * factory for additional address bit options. The address
DaveStyles 0:db4e3d0374fe 34 * bits allow up to eight MCP3221 devices on the same
DaveStyles 0:db4e3d0374fe 35 * bus and are used to determine which device is
DaveStyles 0:db4e3d0374fe 36 * accessed.
DaveStyles 0:db4e3d0374fe 37 *
DaveStyles 0:db4e3d0374fe 38 * The eighth bit of the slave address determines if the
DaveStyles 0:db4e3d0374fe 39 * master device wants to read conversion data or write to
DaveStyles 0:db4e3d0374fe 40 * the MCP3221. When set to a ‘1’, a read operation is
DaveStyles 0:db4e3d0374fe 41 * selected. When set to a ‘0’, a write operation is
DaveStyles 0:db4e3d0374fe 42 * selected. There are no writable registers on the
DaveStyles 0:db4e3d0374fe 43 * MCP3221. Therefore, this bit must be set to a ’1’ in
DaveStyles 0:db4e3d0374fe 44 * order to initiate a conversion.
DaveStyles 0:db4e3d0374fe 45 *
DaveStyles 0:db4e3d0374fe 46 * ***** NOTE ***** *****************************************************************************************
DaveStyles 0:db4e3d0374fe 47 * THIS LIBRARY IS FOR AN MCP3221A5T-I/OT - WHICH HAS AN ADDRESS OF 101 - The A5 bit denotes the address
DaveStyles 0:db4e3d0374fe 48 * ***** **** ***** *****************************************************************************************
DaveStyles 0:db4e3d0374fe 49 */
DaveStyles 0:db4e3d0374fe 50
DaveStyles 0:db4e3d0374fe 51
DaveStyles 0:db4e3d0374fe 52 #ifndef MCP3221_H
DaveStyles 0:db4e3d0374fe 53
DaveStyles 0:db4e3d0374fe 54 #define MCP3221_H
DaveStyles 0:db4e3d0374fe 55
DaveStyles 0:db4e3d0374fe 56 #include "mbed.h"
DaveStyles 0:db4e3d0374fe 57
DaveStyles 0:db4e3d0374fe 58 #define MCP3221_CONVERSE 0x9B //10011011 NOTE IT ENDS IN 1, this is the READ ADDRESS. This is all this device does.
DaveStyles 0:db4e3d0374fe 59 //It opens a conversation via this specific READ address
DaveStyles 0:db4e3d0374fe 60
DaveStyles 0:db4e3d0374fe 61 //Library for the MCP3221 12 BIT ADC.
DaveStyles 0:db4e3d0374fe 62
DaveStyles 0:db4e3d0374fe 63 class MCP3221
DaveStyles 0:db4e3d0374fe 64 {
DaveStyles 0:db4e3d0374fe 65 public:
DaveStyles 0:db4e3d0374fe 66
DaveStyles 0:db4e3d0374fe 67 /*
DaveStyles 0:db4e3d0374fe 68 Creates instance
DaveStyles 0:db4e3d0374fe 69 Connect module using I2C port pins sda and scl. The output is referenced to the supply voltage which can be
DaveStyles 0:db4e3d0374fe 70 2.7v to 5.0v. The read will return the correct voltage, if you supply the correct supplyVoltage when instantiating.
DaveStyles 0:db4e3d0374fe 71 */
DaveStyles 0:db4e3d0374fe 72 MCP3221(PinName sda, PinName scl, float supplyVoltage);
DaveStyles 0:db4e3d0374fe 73
DaveStyles 0:db4e3d0374fe 74 /*
DaveStyles 0:db4e3d0374fe 75 Destroys instance.
DaveStyles 0:db4e3d0374fe 76 */
DaveStyles 0:db4e3d0374fe 77 ~MCP3221();
DaveStyles 0:db4e3d0374fe 78
DaveStyles 0:db4e3d0374fe 79 /*
DaveStyles 0:db4e3d0374fe 80 Reads the analog register of the MCP3221 and converts it to a useable value. (a voltage)
DaveStyles 0:db4e3d0374fe 81 */
DaveStyles 0:db4e3d0374fe 82 float read();
DaveStyles 0:db4e3d0374fe 83
DaveStyles 0:db4e3d0374fe 84 private:
DaveStyles 0:db4e3d0374fe 85
DaveStyles 0:db4e3d0374fe 86 I2C i2c;
DaveStyles 0:db4e3d0374fe 87 float _supplyVoltage;
DaveStyles 0:db4e3d0374fe 88 char _data[2];
DaveStyles 0:db4e3d0374fe 89
DaveStyles 0:db4e3d0374fe 90 };
DaveStyles 0:db4e3d0374fe 91
DaveStyles 0:db4e3d0374fe 92 #endif
DaveStyles 0:db4e3d0374fe 93