Advertisement
mat8854

display_i2c_multiplex

Dec 18th, 2020
356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. #include <Wire.h>
  4. #include <LiquidCrystal_I2C.h>
  5.  
  6. int PIN_CLK_EN_1 = 1;
  7. int PIN_CLK_EN_2 = 2;
  8. int CLK_SWITCH_DELAY_US = 50;
  9.  
  10. // two displays with same I2C address
  11. LiquidCrystal_I2C lcd1 = LiquidCrystal_I2C(0x27, 16, 2);
  12. LiquidCrystal_I2C lcd2 = LiquidCrystal_I2C(0x27, 16, 2);
  13.  
  14. // enable display CLK line with some delay
  15. void enableDisplay(int num) {
  16.   num == 1 ? digitalWrite(PIN_CLK_EN_2, LOW) : digitalWrite(PIN_CLK_EN_1, LOW);
  17.   delayMicroseconds(CLK_SWITCH_DELAY_US);
  18.   num == 1 ? digitalWrite(PIN_CLK_EN_1, HIGH) : digitalWrite(PIN_CLK_EN_2, HIGH);
  19.   delayMicroseconds(CLK_SWITCH_DELAY_US);
  20. }
  21.  
  22. void setup() {
  23.   pinMode(PIN_CLK_EN_1, OUTPUT);
  24.   pinMode(PIN_CLK_EN_2, OUTPUT);
  25.   // disable both CLK lines
  26.   digitalWrite(PIN_CLK_EN_1, LOW);
  27.   digitalWrite(PIN_CLK_EN_2, LOW);
  28.  
  29.   enableDisplay(1);
  30.   lcd1.init();
  31.  
  32.   enableDisplay(2);
  33.   lcd2.init();
  34. }
  35.  
  36. void loop() {
  37.  int counter = 0;
  38.  
  39.  while(true) {
  40.  
  41.    enableDisplay(1);
  42.    lcd1.setCursor(0,0);
  43.    lcd1.print("Display 1, count =" + counter);
  44.  
  45.    enableDisplay(2);
  46.    lcd2.setCursor(0,0);
  47.    lcd2.print("Display 2, count =" + counter);
  48.  
  49.    counter++;
  50.  
  51.    delay(500);
  52.  }
  53. }
  54.  
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement