PaHUB_TCA9548A.ino 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. Description: Use PaHUB Unit to expand multiple I2C devices and scan the I2C addresses of the slave devices in order.
  3. */
  4. #include <Wire.h>
  5. #include <M5Stack.h>
  6. #include "ClosedCube_TCA9548A.h"
  7. #define FRONT 2
  8. #define X_LOCAL 100
  9. #define Y_LOCAL 35
  10. #define X_OFFSET 160
  11. #define Y_OFFSET 34
  12. #define PaHub_I2C_ADDRESS 0x70
  13. ClosedCube::Wired::TCA9548A tca9548a;
  14. void setup()
  15. {
  16. M5.begin();
  17. M5.Power.begin();
  18. Wire.begin();
  19. M5.Lcd.fillScreen(TFT_BLACK);
  20. tca9548a.address(PaHub_I2C_ADDRESS);
  21. M5.Lcd.setTextFont(4);
  22. M5.Lcd.setCursor(70, 0, 4);
  23. M5.Lcd.setTextColor(YELLOW,TFT_BLACK);
  24. M5.Lcd.println(("PaHUB Example"));
  25. M5.Lcd.setTextColor(TFT_WHITE,TFT_BLACK);
  26. }
  27. void PaHUB(void){
  28. uint8_t returnCode = 0;
  29. uint8_t address;
  30. for( uint8_t channel=0; channel<TCA9548A_MAX_CHANNELS; channel++ ) {
  31. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL + Y_OFFSET*channel , FRONT);
  32. M5.Lcd.printf(" ");
  33. M5.Lcd.setCursor(X_LOCAL, Y_LOCAL + Y_OFFSET*channel , FRONT);
  34. M5.Lcd.printf("CH%d : ",channel);
  35. returnCode = tca9548a.selectChannel(channel);
  36. if( returnCode == 0 ) {
  37. for(address = 0x01; address < 0x7F; address++ ) {
  38. Wire.beginTransmission(address);
  39. returnCode = Wire.endTransmission();
  40. if (returnCode == 0) {
  41. Serial.print("I2C device = ");
  42. M5.Lcd.printf("0X%X ",address);
  43. }
  44. }
  45. }
  46. delay(200);
  47. }
  48. }
  49. void loop()
  50. {
  51. PaHUB();
  52. }