Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pcf8574.c 3.3 KiB

il y a 5 mois
il y a 5 mois
il y a 5 mois
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * pcf8574.c:
  3. * Extend wiringPi with the PCF8574 I2C GPIO expander chip
  4. * Copyright (c) 2013-2024 Gordon Henderson and contributors
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://github.com/WiringPi/WiringPi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with wiringPi.
  21. * If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <stdio.h>
  25. #include <pthread.h>
  26. #include "wiringPi.h"
  27. #include "wiringPiI2C.h"
  28. #include "pcf8574.h"
  29. /*
  30. * myPinMode:
  31. * The PCF8574 is a 8-Bit I/O Expander with Open-drain output.
  32. * The pins are effectively bi-directional,
  33. * however the pins should be driven high when used as an input pin...
  34. * So, we're effectively copying digitalWrite...
  35. *********************************************************************************
  36. */
  37. static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
  38. {
  39. int bit, old ;
  40. bit = 1 << ((pin - node->pinBase) & 7) ;
  41. old = node->data2 ;
  42. if (mode == OUTPUT)
  43. old &= (~bit) ; // Write bit to 0
  44. else
  45. old |= bit ; // Write bit to 1
  46. wiringPiI2CWrite (node->fd, old) ;
  47. node->data2 = old ;
  48. }
  49. /*
  50. * myDigitalWrite:
  51. *********************************************************************************
  52. */
  53. static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
  54. {
  55. int bit, old ;
  56. bit = 1 << ((pin - node->pinBase) & 7) ;
  57. old = node->data2 ;
  58. if (value == LOW)
  59. old &= (~bit) ;
  60. else
  61. old |= bit ;
  62. wiringPiI2CWrite (node->fd, old) ;
  63. node->data2 = old ;
  64. }
  65. /*
  66. * myDigitalRead:
  67. *********************************************************************************
  68. */
  69. static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
  70. {
  71. int mask, value ;
  72. mask = 1 << ((pin - node->pinBase) & 7) ;
  73. value = wiringPiI2CRead (node->fd) ;
  74. if ((value & mask) == 0)
  75. return LOW ;
  76. else
  77. return HIGH ;
  78. }
  79. /*
  80. * pcf8574Setup:
  81. * Create a new instance of a PCF8574 I2C GPIO interface. We know it
  82. * has 8 pins, so all we need to know here is the I2C address and the
  83. * user-defined pin base. Default address (A0-A3 low) is 0x20.
  84. *********************************************************************************
  85. */
  86. int pcf8574Setup (const int pinBase, const int i2cAddress)
  87. {
  88. int fd ;
  89. struct wiringPiNodeStruct *node ;
  90. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  91. return FALSE ;
  92. node = wiringPiNewNode (pinBase, 8) ;
  93. node->fd = fd ;
  94. node->pinMode = myPinMode ;
  95. node->digitalRead = myDigitalRead ;
  96. node->digitalWrite = myDigitalWrite ;
  97. node->data2 = wiringPiI2CRead (fd) ;
  98. return TRUE ;
  99. }