Advertisement
elektronek

Vladimir Podhorsky - expander PCF8574 + LCD1602

May 31st, 2020
1,679
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     Original code from webpage
  3.     https://elger.org/wiki/projects/i2c_pcf8574_8bit_port_expander
  4.     Modified for 8 inputs and outputs + show info at LCD1602
  5.     (c)2020 Martin Blaha - elektronek.cz
  6. */
  7.  
  8. #include <Wire.h>
  9. #include <LiquidCrystal.h>
  10.  
  11. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  12.  
  13. byte iInput=0;
  14. byte iOutput=0;
  15.  
  16. void setup()
  17. {
  18.     Wire.begin();
  19.     lcd.begin(16, 2);
  20.     lcd.setCursor ( 0, 0 );
  21.     lcd.print("Vyber vstupu");
  22. }
  23.  
  24. void loop()
  25. {
  26.     Wire.requestFrom(33,1);// Begin transmission to PCF8574 with the buttons
  27.     if(Wire.available())   // If bytes are available to be recieved
  28.     {
  29.         iInput = Wire.read();// Read a byte
  30.     }
  31.  
  32.     if(iInput<255)         //If the value less than 255
  33.     {
  34.         lcd.setCursor ( 0, 1 );
  35.         if (iInput==254) // P0
  36.         {
  37.             iOutput = 1;
  38.             lcd.print("Vstup 1");
  39.         };
  40.         if (iInput==253) // P1
  41.         {
  42.             iOutput = 2;
  43.             lcd.print("Vstup 2");
  44.         };
  45.         if (iInput==251) // P2
  46.         {
  47.             iOutput = 4;
  48.             lcd.print("Vstup 3");
  49.         };
  50.         if (iInput==247) // P3
  51.         {
  52.             iOutput = 8;
  53.             lcd.print("Vstup 4");
  54.         };
  55.         if (iInput==239) // P4
  56.         {
  57.             iOutput = 16;
  58.             lcd.print("Vstup 5");
  59.         };
  60.         if (iInput==223) // P5
  61.         {
  62.             iOutput = 32;
  63.             lcd.print("Vstup 6");
  64.         };
  65.         if (iInput==191) // P6
  66.         {
  67.             iOutput = 64;
  68.             lcd.print("Vstup 7");
  69.         };
  70.         if (iInput==127) // P7
  71.         {
  72.             iOutput = 128;
  73.             lcd.print("Vstup 8");
  74.         };
  75.     }
  76.     Wire.beginTransmission(32);  //Begin transmission to PCF8574 (with the LEDs)
  77.     Wire.write(iOutput);         //Send data to PCF8574 (with the LEDs)
  78.     Wire.endTransmission();      //End Transmission to PCF8574 (with the LEDs)
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement