TCA9548A 8 channel I2C switch with reset
Dependents: 4012Code PWM SBra_Programme_Mbed
tca9548a.h@0:5bd73cede544, 2016-05-24 (annotated)
- Committer:
- akashvibhute
- Date:
- Tue May 24 06:07:37 2016 +0000
- Revision:
- 0:5bd73cede544
- Child:
- 1:768491b5e55d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
akashvibhute | 0:5bd73cede544 | 1 | /** TCA9548A library |
akashvibhute | 0:5bd73cede544 | 2 | * |
akashvibhute | 0:5bd73cede544 | 3 | * @author Akash Vibhute |
akashvibhute | 0:5bd73cede544 | 4 | * @author < akash . roboticist [at] gmail . com > |
akashvibhute | 0:5bd73cede544 | 5 | * @version 0.1 |
akashvibhute | 0:5bd73cede544 | 6 | * @date May/24/2016 |
akashvibhute | 0:5bd73cede544 | 7 | * |
akashvibhute | 0:5bd73cede544 | 8 | * @section LICENSE |
akashvibhute | 0:5bd73cede544 | 9 | * |
akashvibhute | 0:5bd73cede544 | 10 | * Copyright (c) 2015 Akash Vibhute |
akashvibhute | 0:5bd73cede544 | 11 | * |
akashvibhute | 0:5bd73cede544 | 12 | * Licensed under the Apache License, Version 2.0 (the "License"); |
akashvibhute | 0:5bd73cede544 | 13 | * you may not use this file except in compliance with the License. |
akashvibhute | 0:5bd73cede544 | 14 | * You may obtain a copy of the License at |
akashvibhute | 0:5bd73cede544 | 15 | * http://www.apache.org/licenses/LICENSE-2.0 |
akashvibhute | 0:5bd73cede544 | 16 | * |
akashvibhute | 0:5bd73cede544 | 17 | * Unless required by applicable law or agreed to in writing, software |
akashvibhute | 0:5bd73cede544 | 18 | * distributed under the License is distributed on an "AS IS" BASIS, |
akashvibhute | 0:5bd73cede544 | 19 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
akashvibhute | 0:5bd73cede544 | 20 | * See the License for the specific language governing permissions and |
akashvibhute | 0:5bd73cede544 | 21 | * limitations under the License. |
akashvibhute | 0:5bd73cede544 | 22 | * |
akashvibhute | 0:5bd73cede544 | 23 | * The above copyright notice and this permission notice shall be included in |
akashvibhute | 0:5bd73cede544 | 24 | * all copies or substantial portions of the Software. |
akashvibhute | 0:5bd73cede544 | 25 | * |
akashvibhute | 0:5bd73cede544 | 26 | * |
akashvibhute | 0:5bd73cede544 | 27 | */ |
akashvibhute | 0:5bd73cede544 | 28 | |
akashvibhute | 0:5bd73cede544 | 29 | #ifndef TCA9548A_H |
akashvibhute | 0:5bd73cede544 | 30 | #define TCA9548A_H |
akashvibhute | 0:5bd73cede544 | 31 | |
akashvibhute | 0:5bd73cede544 | 32 | #include "mbed.h" |
akashvibhute | 0:5bd73cede544 | 33 | |
akashvibhute | 0:5bd73cede544 | 34 | /** TCA9548A class |
akashvibhute | 0:5bd73cede544 | 35 | * |
akashvibhute | 0:5bd73cede544 | 36 | * @section DESCRIPTION |
akashvibhute | 0:5bd73cede544 | 37 | * TCA9548A: Low-Voltage 8-Channel I2C Switch With Reset |
akashvibhute | 0:5bd73cede544 | 38 | * The TCA9548A device has eight bidirectional translating switches that can be |
akashvibhute | 0:5bd73cede544 | 39 | * controlled through the I2C bus. The SCL/SDA upstream pair fans out to eight |
akashvibhute | 0:5bd73cede544 | 40 | * downstream pairs, or channels. Any individual SCn/SDn channel or combination |
akashvibhute | 0:5bd73cede544 | 41 | * of channels can be selected, determined by the contents of the programmable |
akashvibhute | 0:5bd73cede544 | 42 | * control register. |
akashvibhute | 0:5bd73cede544 | 43 | * |
akashvibhute | 0:5bd73cede544 | 44 | * The TCA9548A has a user 8 selectable address in the range 0x70 thru 0x77 (7-bit), |
akashvibhute | 0:5bd73cede544 | 45 | * based on the configuration of Addr pins (A0, A1, A2). Default address is 0x70 for |
akashvibhute | 0:5bd73cede544 | 46 | * A0:A2 pull down. |
akashvibhute | 0:5bd73cede544 | 47 | * |
akashvibhute | 0:5bd73cede544 | 48 | * Datasheet for TCA9548A can be found at: |
akashvibhute | 0:5bd73cede544 | 49 | * http://www.ti.com/lit/ds/symlink/tca9548a.pdf |
akashvibhute | 0:5bd73cede544 | 50 | * |
akashvibhute | 0:5bd73cede544 | 51 | * Example: |
akashvibhute | 0:5bd73cede544 | 52 | * @code |
akashvibhute | 0:5bd73cede544 | 53 | * #include "mbed.h" |
akashvibhute | 0:5bd73cede544 | 54 | * #include "LM75B.h" |
akashvibhute | 0:5bd73cede544 | 55 | * #include "TCA9548A.h" |
akashvibhute | 0:5bd73cede544 | 56 | * |
akashvibhute | 0:5bd73cede544 | 57 | * TCA9546A i2c_sw(I2C_SDA, I2C_SCL); //default address 0x70 applied |
akashvibhute | 0:5bd73cede544 | 58 | * |
akashvibhute | 0:5bd73cede544 | 59 | * int main() |
akashvibhute | 0:5bd73cede544 | 60 | * { |
akashvibhute | 0:5bd73cede544 | 61 | * // By default TCA9548A performs a power on reset and all downstream ports are deselected |
akashvibhute | 0:5bd73cede544 | 62 | * |
akashvibhute | 0:5bd73cede544 | 63 | * i2c_sw.select(0); // select the channel 0 |
akashvibhute | 0:5bd73cede544 | 64 | * LM75B tmp0(I2C_SDA, I2C_SCL); // making instance after a branch of I2C bus (which is connecting the LM75B) enabled |
akashvibhute | 0:5bd73cede544 | 65 | * |
akashvibhute | 0:5bd73cede544 | 66 | * i2c_sw.select(1); // select the channel 1 |
akashvibhute | 0:5bd73cede544 | 67 | * LM75B tmp1(I2C_SDA, I2C_SCL); // making instance after a branch of I2C bus (which is connecting the LM75B) enabled |
akashvibhute | 0:5bd73cede544 | 68 | * |
akashvibhute | 0:5bd73cede544 | 69 | * while(1) { |
akashvibhute | 0:5bd73cede544 | 70 | * |
akashvibhute | 0:5bd73cede544 | 71 | * i2c_sw.select(0); // select the channel 0 |
akashvibhute | 0:5bd73cede544 | 72 | * printf( "%.3f\r\n", tmp0.read() ); |
akashvibhute | 0:5bd73cede544 | 73 | * |
akashvibhute | 0:5bd73cede544 | 74 | * i2c_sw.select(1); // select the channel 1 |
akashvibhute | 0:5bd73cede544 | 75 | * printf( "%.3f\r\n", tmp1.read() ); |
akashvibhute | 0:5bd73cede544 | 76 | * |
akashvibhute | 0:5bd73cede544 | 77 | * wait( 1.0 ); |
akashvibhute | 0:5bd73cede544 | 78 | * } |
akashvibhute | 0:5bd73cede544 | 79 | * } |
akashvibhute | 0:5bd73cede544 | 80 | * @endcode |
akashvibhute | 0:5bd73cede544 | 81 | */ |
akashvibhute | 0:5bd73cede544 | 82 | |
akashvibhute | 0:5bd73cede544 | 83 | class TCA9548A |
akashvibhute | 0:5bd73cede544 | 84 | { |
akashvibhute | 0:5bd73cede544 | 85 | public: |
akashvibhute | 0:5bd73cede544 | 86 | |
akashvibhute | 0:5bd73cede544 | 87 | /** Create a TCA9546A instance connected to specified I2C pins with specified address |
akashvibhute | 0:5bd73cede544 | 88 | * |
akashvibhute | 0:5bd73cede544 | 89 | * @param sda I2C-bus SDA pin |
akashvibhute | 0:5bd73cede544 | 90 | * @param scl I2C-bus SCL pin |
akashvibhute | 0:5bd73cede544 | 91 | * @param i2c_address I2C-bus address (default: 0x70) |
akashvibhute | 0:5bd73cede544 | 92 | * @param reset TCA9546A reset pin (default: NC for simple pullup connected) |
akashvibhute | 0:5bd73cede544 | 93 | * @param hz I2C bus frequency (default: 400kHz) |
akashvibhute | 0:5bd73cede544 | 94 | */ |
akashvibhute | 0:5bd73cede544 | 95 | TCA9548A( PinName sda, PinName scl, uint8_t i2c_address = 0x70, PinName resetPin = NC, uint32_t hz = 400000 ); |
akashvibhute | 0:5bd73cede544 | 96 | |
akashvibhute | 0:5bd73cede544 | 97 | /** Selecting a channel |
akashvibhute | 0:5bd73cede544 | 98 | * |
akashvibhute | 0:5bd73cede544 | 99 | * Switch to commended downstream I2C channel |
akashvibhute | 0:5bd73cede544 | 100 | * |
akashvibhute | 0:5bd73cede544 | 101 | * @param channel channel number |
akashvibhute | 0:5bd73cede544 | 102 | */ |
akashvibhute | 0:5bd73cede544 | 103 | void select(uint8_t channel); |
akashvibhute | 0:5bd73cede544 | 104 | |
akashvibhute | 0:5bd73cede544 | 105 | /** Reset TCA9546A |
akashvibhute | 0:5bd73cede544 | 106 | * |
akashvibhute | 0:5bd73cede544 | 107 | * Reset switch |
akashvibhute | 0:5bd73cede544 | 108 | */ |
akashvibhute | 0:5bd73cede544 | 109 | void reset(); |
akashvibhute | 0:5bd73cede544 | 110 | |
akashvibhute | 0:5bd73cede544 | 111 | |
akashvibhute | 0:5bd73cede544 | 112 | private: |
akashvibhute | 0:5bd73cede544 | 113 | I2C i2c_; |
akashvibhute | 0:5bd73cede544 | 114 | DigitalOut reset_pin; |
akashvibhute | 0:5bd73cede544 | 115 | uint8_t i2c_addr; |
akashvibhute | 0:5bd73cede544 | 116 | |
akashvibhute | 0:5bd73cede544 | 117 | }; |
akashvibhute | 0:5bd73cede544 | 118 | |
akashvibhute | 0:5bd73cede544 | 119 | #endif // TCA9548A_H |