I2C not yet integrated
Tested working with single and differential voltages.
Connect SCL (pin 11) to D15 Connect SDA (pin 10) to D14 Connect pin 16 to +5v Connect pin 9 to gnd
LT_I2C.h@5:8aa0f4e6788c, 2016-12-08 (annotated)
- Committer:
- lrdawg99
- Date:
- Thu Dec 08 21:48:29 2016 +0000
- Revision:
- 5:8aa0f4e6788c
- Parent:
- 2:c9e727dcd00e
scl sda switch
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lrdawg99 | 0:1473318f27b6 | 1 | |
lrdawg99 | 0:1473318f27b6 | 2 | /*! |
lrdawg99 | 0:1473318f27b6 | 3 | LT_I2C: Routines to communicate with ATmega328P's hardware I2C port. |
lrdawg99 | 0:1473318f27b6 | 4 | |
lrdawg99 | 0:1473318f27b6 | 5 | REVISION HISTORY |
lrdawg99 | 0:1473318f27b6 | 6 | $Revision: 3659 $ |
lrdawg99 | 0:1473318f27b6 | 7 | $Date: 2015-07-01 10:19:20 -0700 (Wed, 01 Jul 2015) $ |
lrdawg99 | 0:1473318f27b6 | 8 | |
lrdawg99 | 0:1473318f27b6 | 9 | Copyright (c) 2013, Linear Technology Corp.(LTC) |
lrdawg99 | 0:1473318f27b6 | 10 | All rights reserved. |
lrdawg99 | 0:1473318f27b6 | 11 | |
lrdawg99 | 0:1473318f27b6 | 12 | Redistribution and use in source and binary forms, with or without |
lrdawg99 | 0:1473318f27b6 | 13 | modification, are permitted provided that the following conditions are met: |
lrdawg99 | 0:1473318f27b6 | 14 | |
lrdawg99 | 0:1473318f27b6 | 15 | 1. Redistributions of source code must retain the above copyright notice, this |
lrdawg99 | 0:1473318f27b6 | 16 | list of conditions and the following disclaimer. |
lrdawg99 | 0:1473318f27b6 | 17 | 2. Redistributions in binary form must reproduce the above copyright notice, |
lrdawg99 | 0:1473318f27b6 | 18 | this list of conditions and the following disclaimer in the documentation |
lrdawg99 | 0:1473318f27b6 | 19 | and/or other materials provided with the distribution. |
lrdawg99 | 0:1473318f27b6 | 20 | |
lrdawg99 | 0:1473318f27b6 | 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
lrdawg99 | 0:1473318f27b6 | 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
lrdawg99 | 0:1473318f27b6 | 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
lrdawg99 | 0:1473318f27b6 | 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
lrdawg99 | 0:1473318f27b6 | 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
lrdawg99 | 0:1473318f27b6 | 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
lrdawg99 | 0:1473318f27b6 | 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
lrdawg99 | 0:1473318f27b6 | 28 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
lrdawg99 | 0:1473318f27b6 | 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
lrdawg99 | 0:1473318f27b6 | 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
lrdawg99 | 0:1473318f27b6 | 31 | |
lrdawg99 | 0:1473318f27b6 | 32 | The views and conclusions contained in the software and documentation are those |
lrdawg99 | 0:1473318f27b6 | 33 | of the authors and should not be interpreted as representing official policies, |
lrdawg99 | 0:1473318f27b6 | 34 | either expressed or implied, of Linear Technology Corp. |
lrdawg99 | 0:1473318f27b6 | 35 | |
lrdawg99 | 0:1473318f27b6 | 36 | The Linear Technology Linduino is not affiliated with the official Arduino team. |
lrdawg99 | 0:1473318f27b6 | 37 | However, the Linduino is only possible because of the Arduino team's commitment |
lrdawg99 | 0:1473318f27b6 | 38 | to the open-source community. Please, visit http://www.arduino.cc and |
lrdawg99 | 0:1473318f27b6 | 39 | http://store.arduino.cc , and consider a purchase that will help fund their |
lrdawg99 | 0:1473318f27b6 | 40 | ongoing work. |
lrdawg99 | 0:1473318f27b6 | 41 | */ |
lrdawg99 | 0:1473318f27b6 | 42 | |
lrdawg99 | 0:1473318f27b6 | 43 | /*! @file |
lrdawg99 | 0:1473318f27b6 | 44 | @ingroup LT_I2C |
lrdawg99 | 0:1473318f27b6 | 45 | Library Header File for LT_I2C: Routines to communicate with ATmega328P's hardware I2C port. |
lrdawg99 | 0:1473318f27b6 | 46 | */ |
lrdawg99 | 0:1473318f27b6 | 47 | |
lrdawg99 | 0:1473318f27b6 | 48 | #ifndef LT_I2C_H |
lrdawg99 | 0:1473318f27b6 | 49 | #define LT_I2C_H |
lrdawg99 | 0:1473318f27b6 | 50 | |
lrdawg99 | 0:1473318f27b6 | 51 | #include <stdint.h> |
lrdawg99 | 0:1473318f27b6 | 52 | //#include <Wire.h> |
lrdawg99 | 0:1473318f27b6 | 53 | |
lrdawg99 | 0:1473318f27b6 | 54 | //! @name HARDWARE I2C PRESCALER VALUES |
lrdawg99 | 0:1473318f27b6 | 55 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 56 | #define HARDWARE_I2C_PRESCALER_1 0 |
lrdawg99 | 0:1473318f27b6 | 57 | #define HARDWARE_I2C_PRESCALER_4 1 |
lrdawg99 | 0:1473318f27b6 | 58 | #define HARDWARE_I2C_PRESCALER_16 2 |
lrdawg99 | 0:1473318f27b6 | 59 | #define HARDWARE_I2C_PRESCALER_64 3 |
lrdawg99 | 0:1473318f27b6 | 60 | //! @} |
lrdawg99 | 0:1473318f27b6 | 61 | |
lrdawg99 | 0:1473318f27b6 | 62 | //! @name I2C READ and WRITE BITS |
lrdawg99 | 0:1473318f27b6 | 63 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 64 | //! Eighth bit (LSB) of I2C address indicates a "read" or "write". |
lrdawg99 | 0:1473318f27b6 | 65 | //! (The first seven bits are the 7-bit I2C address.) |
lrdawg99 | 0:1473318f27b6 | 66 | #define I2C_READ_BIT 0x01 |
lrdawg99 | 0:1473318f27b6 | 67 | #define I2C_WRITE_BIT 0x00 |
lrdawg99 | 0:1473318f27b6 | 68 | //! @} |
lrdawg99 | 0:1473318f27b6 | 69 | |
lrdawg99 | 0:1473318f27b6 | 70 | //! @name STATUS BITS |
lrdawg99 | 0:1473318f27b6 | 71 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 72 | #define STATUS_START 0x08 |
lrdawg99 | 0:1473318f27b6 | 73 | #define STATUS_REPEATED_START 0x10 |
lrdawg99 | 0:1473318f27b6 | 74 | #define STATUS_ADDRESS_WRITE_ACK 0x18 |
lrdawg99 | 0:1473318f27b6 | 75 | #define STATUS_ADDRESS_WRITE_NACK 0x20 |
lrdawg99 | 0:1473318f27b6 | 76 | #define STATUS_WRITE_ACK 0x28 |
lrdawg99 | 0:1473318f27b6 | 77 | #define STATUS_WRITE_NACK 0x30 |
lrdawg99 | 0:1473318f27b6 | 78 | #define STATUS_ARBITRATION_LOST 0x38 |
lrdawg99 | 0:1473318f27b6 | 79 | #define STATUS_ADDRESS_READ_ACK 0x40 |
lrdawg99 | 0:1473318f27b6 | 80 | #define STATUS_ADDRESS_READ_NACK 0x48 |
lrdawg99 | 0:1473318f27b6 | 81 | #define STATUS_READ_ACK 0x50 |
lrdawg99 | 0:1473318f27b6 | 82 | #define STATUS_READ_NACK 0x58 |
lrdawg99 | 0:1473318f27b6 | 83 | //! @} |
lrdawg99 | 0:1473318f27b6 | 84 | |
lrdawg99 | 0:1473318f27b6 | 85 | //! @name TIMEOUT AND DELAY IN US |
lrdawg99 | 0:1473318f27b6 | 86 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 87 | #define HW_I2C_DELAY 1 |
lrdawg99 | 0:1473318f27b6 | 88 | #define HW_I2C_TIMEOUT 20000 |
lrdawg99 | 0:1473318f27b6 | 89 | //! @} |
lrdawg99 | 0:1473318f27b6 | 90 | |
lrdawg99 | 0:1473318f27b6 | 91 | //! @name ACK OR NACK PARAMETER PASSED TO I2C_READ |
lrdawg99 | 0:1473318f27b6 | 92 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 93 | #define WITH_ACK 0 //!< Use with i2c_read(WITH_ACK) to read with an acknowledge |
lrdawg99 | 0:1473318f27b6 | 94 | #define WITH_NACK 1 //!< Use with i2c_read(WITH_NACK) to read without an acknowledge. Normally used after the last byte of a multi-byte read. |
lrdawg99 | 0:1473318f27b6 | 95 | //! @} |
lrdawg99 | 0:1473318f27b6 | 96 | |
lrdawg99 | 0:1473318f27b6 | 97 | //! @name OPTIONAL I2C Address MACRO |
lrdawg99 | 0:1473318f27b6 | 98 | //! @{ |
lrdawg99 | 0:1473318f27b6 | 99 | #define I2C_8ADDR(address) (address >> 1) //!< Use to convert an 8-bit I2C address to 7 bits. |
lrdawg99 | 0:1473318f27b6 | 100 | //! @} |
lrdawg99 | 0:1473318f27b6 | 101 | |
lrdawg99 | 2:c9e727dcd00e | 102 | #include "mbed.h" |
lrdawg99 | 0:1473318f27b6 | 103 | |
lrdawg99 | 2:c9e727dcd00e | 104 | class LT_I2C { |
lrdawg99 | 2:c9e727dcd00e | 105 | private: |
lrdawg99 | 2:c9e727dcd00e | 106 | I2C *i2c; |
lrdawg99 | 2:c9e727dcd00e | 107 | PinName sda; |
lrdawg99 | 2:c9e727dcd00e | 108 | PinName scl; |
lrdawg99 | 0:1473318f27b6 | 109 | |
lrdawg99 | 2:c9e727dcd00e | 110 | public: |
lrdawg99 | 2:c9e727dcd00e | 111 | LT_I2C(); |
lrdawg99 | 2:c9e727dcd00e | 112 | LT_I2C(PinName sda_, PinName scl_); |
lrdawg99 | 2:c9e727dcd00e | 113 | |
lrdawg99 | 2:c9e727dcd00e | 114 | //! Read a byte of data at register specified by "command", store in "value" |
lrdawg99 | 2:c9e727dcd00e | 115 | //! @return 0 on success, 1 on failure |
lrdawg99 | 2:c9e727dcd00e | 116 | int8_t i2c_read_byte_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 2:c9e727dcd00e | 117 | uint8_t command, //!< Command byte |
lrdawg99 | 2:c9e727dcd00e | 118 | uint8_t *value //!< Byte to be read |
lrdawg99 | 2:c9e727dcd00e | 119 | ); |
lrdawg99 | 2:c9e727dcd00e | 120 | |
lrdawg99 | 2:c9e727dcd00e | 121 | //! Write a byte of data to register specified by "command" |
lrdawg99 | 2:c9e727dcd00e | 122 | //! @return 0 on success, 1 on failure |
lrdawg99 | 2:c9e727dcd00e | 123 | int8_t i2c_write_byte_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 2:c9e727dcd00e | 124 | uint8_t command, //!< Command byte |
lrdawg99 | 2:c9e727dcd00e | 125 | uint8_t value //!< Byte to be written |
lrdawg99 | 2:c9e727dcd00e | 126 | ); |
lrdawg99 | 2:c9e727dcd00e | 127 | |
lrdawg99 | 2:c9e727dcd00e | 128 | //! Read a 16-bit word of data from register specified by "command" |
lrdawg99 | 2:c9e727dcd00e | 129 | //! @return 0 on success, 1 on failure |
lrdawg99 | 2:c9e727dcd00e | 130 | int8_t i2c_read_word_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 2:c9e727dcd00e | 131 | uint8_t command, //!< Command byte |
lrdawg99 | 2:c9e727dcd00e | 132 | uint16_t *value //!< Word to be read |
lrdawg99 | 2:c9e727dcd00e | 133 | ); |
lrdawg99 | 2:c9e727dcd00e | 134 | |
lrdawg99 | 2:c9e727dcd00e | 135 | //! Write a 16-bit word of data to register specified by "command" |
lrdawg99 | 2:c9e727dcd00e | 136 | //! @return 0 on success, 1 on failure |
lrdawg99 | 2:c9e727dcd00e | 137 | int8_t i2c_write_word_data(uint8_t address, //!< 7-bit I2C address |
lrdawg99 | 2:c9e727dcd00e | 138 | uint8_t command, //!< Command byte |
lrdawg99 | 2:c9e727dcd00e | 139 | uint16_t value //!< Word to be written |
lrdawg99 | 2:c9e727dcd00e | 140 | ); |
lrdawg99 | 2:c9e727dcd00e | 141 | }; |
lrdawg99 | 0:1473318f27b6 | 142 | #endif // LT_I2C_H |