library for the motor driver
Dependents: 4180_final_project
Fork of libTCS34725 by
TCA9548.h@4:cc00e3842f1b, 2017-03-09 (annotated)
- Committer:
- mwilkens241
- Date:
- Thu Mar 09 18:42:41 2017 +0000
- Revision:
- 4:cc00e3842f1b
- Parent:
- TCS34725.h@3:afb107db7994
working mux
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mwilkens241 | 4:cc00e3842f1b | 1 | // TCA9548 I2C Mux |
mwilkens241 | 4:cc00e3842f1b | 2 | #ifndef MBED_TCA9548_H |
mwilkens241 | 4:cc00e3842f1b | 3 | #define MBED_TCA9548_H |
mwilkens241 | 3:afb107db7994 | 4 | |
mwilkens241 | 1:06c9bbbdb8b0 | 5 | #include "mbed.h" |
mwilkens241 | 0:4796574af790 | 6 | |
mwilkens241 | 0:4796574af790 | 7 | //These pins should be the only piece specific to the F030R8 |
mwilkens241 | 0:4796574af790 | 8 | #define SCL PB_8 |
mwilkens241 | 0:4796574af790 | 9 | #define SDA PB_9 |
mwilkens241 | 0:4796574af790 | 10 | |
mwilkens241 | 0:4796574af790 | 11 | //I2C Address |
mwilkens241 | 0:4796574af790 | 12 | |
mwilkens241 | 4:cc00e3842f1b | 13 | #define DEV_ADDR (0x70<<1) |
mwilkens241 | 0:4796574af790 | 14 | |
mwilkens241 | 4:cc00e3842f1b | 15 | // CHANNEL VALUES |
mwilkens241 | 4:cc00e3842f1b | 16 | #define CH0 0x01 |
mwilkens241 | 4:cc00e3842f1b | 17 | #define CH1 0x02 |
mwilkens241 | 4:cc00e3842f1b | 18 | #define CH2 0x04 |
mwilkens241 | 4:cc00e3842f1b | 19 | #define CH3 0x08 |
mwilkens241 | 4:cc00e3842f1b | 20 | #define CH4 0x10 |
mwilkens241 | 4:cc00e3842f1b | 21 | #define CH5 0x20 |
mwilkens241 | 4:cc00e3842f1b | 22 | #define CH6 0x40 |
mwilkens241 | 4:cc00e3842f1b | 23 | #define CH7 0x80 |
mwilkens241 | 0:4796574af790 | 24 | |
mwilkens241 | 0:4796574af790 | 25 | |
mwilkens241 | 4:cc00e3842f1b | 26 | /** TCA9548 control class. |
mwilkens241 | 3:afb107db7994 | 27 | * |
mwilkens241 | 3:afb107db7994 | 28 | * Example: |
mwilkens241 | 3:afb107db7994 | 29 | * @code |
mwilkens241 | 4:cc00e3842f1b | 30 | * //Perform readings on two i2c devices with the same address |
mwilkens241 | 4:cc00e3842f1b | 31 | * #include "TCA9548.h" |
mwilkens241 | 3:afb107db7994 | 32 | * #include "mbed.h" |
mwilkens241 | 4:cc00e3842f1b | 33 | * #include "VL6180.h" |
mwilkens241 | 3:afb107db7994 | 34 | * |
mwilkens241 | 4:cc00e3842f1b | 35 | * TCA9548 i2cMux(SDA, SCL); |
mwilkens241 | 4:cc00e3842f1b | 36 | * VL6180 laser1(SDA, SCL); // on SD0 and SC0 |
mwilkens241 | 4:cc00e3842f1b | 37 | * VL6180 laser2(SDA, SCL); // on SD1 and SC1 |
mwilkens241 | 3:afb107db7994 | 38 | * Serial pc(USBTX, USBRX); //USB serial |
mwilkens241 | 3:afb107db7994 | 39 | * |
mwilkens241 | 3:afb107db7994 | 40 | * int main() { |
mwilkens241 | 4:cc00e3842f1b | 41 | * float reading; |
mwilkens241 | 4:cc00e3842f1b | 42 | * i2cMux.ch(CH0); |
mwilkens241 | 4:cc00e3842f1b | 43 | * reading = laser1.read(); |
mwilkens241 | 4:cc00e3842f1b | 44 | * pc.printf("Laser 1: %f\n", reading); |
mwilkens241 | 4:cc00e3842f1b | 45 | * i2cMux.ch(CH1); //for two channels: i2cMux.ch(CH1 | CH5); |
mwilkens241 | 4:cc00e3842f1b | 46 | * pc.printf("Laser 2: %f\n", reading); |
mwilkens241 | 4:cc00e3842f1b | 47 | * reading = laser2.read(); |
mwilkens241 | 3:afb107db7994 | 48 | * |
mwilkens241 | 4:cc00e3842f1b | 49 | * while(1) { |
mwilkens241 | 3:afb107db7994 | 50 | * } |
mwilkens241 | 3:afb107db7994 | 51 | * } |
mwilkens241 | 3:afb107db7994 | 52 | * @endcode |
mwilkens241 | 3:afb107db7994 | 53 | */ |
mwilkens241 | 0:4796574af790 | 54 | |
mwilkens241 | 4:cc00e3842f1b | 55 | class TCA9548 { |
mwilkens241 | 3:afb107db7994 | 56 | private: |
mwilkens241 | 3:afb107db7994 | 57 | I2C i2c; |
mwilkens241 | 4:cc00e3842f1b | 58 | uint8_t channel; |
mwilkens241 | 4:cc00e3842f1b | 59 | void i2cWrite8(uint8_t addr, char data); |
mwilkens241 | 3:afb107db7994 | 60 | public: |
mwilkens241 | 3:afb107db7994 | 61 | /** Initialize object with default i2c pins */ |
mwilkens241 | 4:cc00e3842f1b | 62 | TCA9548(); |
mwilkens241 | 3:afb107db7994 | 63 | |
mwilkens241 | 3:afb107db7994 | 64 | /** Initialize object with specific i2c pins |
mwilkens241 | 3:afb107db7994 | 65 | * |
mwilkens241 | 3:afb107db7994 | 66 | * @param i2c_sda SDA pin |
mwilkens241 | 3:afb107db7994 | 67 | * @param i2c_scl SCL pin |
mwilkens241 | 3:afb107db7994 | 68 | */ |
mwilkens241 | 4:cc00e3842f1b | 69 | TCA9548(PinName i2c_sda, PinName i2c_scl); |
mwilkens241 | 3:afb107db7994 | 70 | |
mwilkens241 | 4:cc00e3842f1b | 71 | /** Boot up the mux and change to initial channel (defaults to 0) |
mwilkens241 | 3:afb107db7994 | 72 | * |
mwilkens241 | 4:cc00e3842f1b | 73 | * @param iCh Initial Channel |
mwilkens241 | 3:afb107db7994 | 74 | * @return |
mwilkens241 | 3:afb107db7994 | 75 | * 1 if failed |
mwilkens241 | 3:afb107db7994 | 76 | *` 0 if successful |
mwilkens241 | 3:afb107db7994 | 77 | */ |
mwilkens241 | 4:cc00e3842f1b | 78 | bool init(uint8_t iCh); |
mwilkens241 | 4:cc00e3842f1b | 79 | bool init(); |
mwilkens241 | 3:afb107db7994 | 80 | |
mwilkens241 | 4:cc00e3842f1b | 81 | /** Changes the i2c channel |
mwilkens241 | 3:afb107db7994 | 82 | * |
mwilkens241 | 4:cc00e3842f1b | 83 | * @param newCh New channel for mux |
mwilkens241 | 3:afb107db7994 | 84 | */ |
mwilkens241 | 4:cc00e3842f1b | 85 | void ch(uint8_t newCh); |
mwilkens241 | 3:afb107db7994 | 86 | }; |
mwilkens241 | 2:cc2c0831a763 | 87 | |
mwilkens241 | 3:afb107db7994 | 88 | #endif |