Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

127 linhas
3.2 KiB

  1. /*
  2. * pcf8574.c:
  3. * Extend wiringPi with the PCF8574 I2C GPIO expander chip
  4. * Copyright (c) 2013 Gordon Henderson
  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 an odd chip - the pins are effectively bi-directional,
  32. * however the pins should be drven high when used as an input pin...
  33. * So, we're effectively copying digitalWrite...
  34. *********************************************************************************
  35. */
  36. static void myPinMode (struct wiringPiNodeStruct *node, int pin, int mode)
  37. {
  38. int bit, old ;
  39. bit = 1 << ((pin - node->pinBase) & 7) ;
  40. old = node->data2 ;
  41. if (mode == OUTPUT)
  42. old &= (~bit) ; // Write bit to 0
  43. else
  44. old |= bit ; // Write bit to 1
  45. wiringPiI2CWrite (node->fd, old) ;
  46. node->data2 = old ;
  47. }
  48. /*
  49. * myDigitalWrite:
  50. *********************************************************************************
  51. */
  52. static void myDigitalWrite (struct wiringPiNodeStruct *node, int pin, int value)
  53. {
  54. int bit, old ;
  55. bit = 1 << ((pin - node->pinBase) & 7) ;
  56. old = node->data2 ;
  57. if (value == LOW)
  58. old &= (~bit) ;
  59. else
  60. old |= bit ;
  61. wiringPiI2CWrite (node->fd, old) ;
  62. node->data2 = old ;
  63. }
  64. /*
  65. * myDigitalRead:
  66. *********************************************************************************
  67. */
  68. static int myDigitalRead (struct wiringPiNodeStruct *node, int pin)
  69. {
  70. int mask, value ;
  71. mask = 1 << ((pin - node->pinBase) & 7) ;
  72. value = wiringPiI2CRead (node->fd) ;
  73. if ((value & mask) == 0)
  74. return LOW ;
  75. else
  76. return HIGH ;
  77. }
  78. /*
  79. * pcf8574Setup:
  80. * Create a new instance of a PCF8574 I2C GPIO interface. We know it
  81. * has 8 pins, so all we need to know here is the I2C address and the
  82. * user-defined pin base.
  83. *********************************************************************************
  84. */
  85. int pcf8574Setup (const int pinBase, const int i2cAddress)
  86. {
  87. int fd ;
  88. struct wiringPiNodeStruct *node ;
  89. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  90. return FALSE ;
  91. node = wiringPiNewNode (pinBase, 8) ;
  92. node->fd = fd ;
  93. node->pinMode = myPinMode ;
  94. node->digitalRead = myDigitalRead ;
  95. node->digitalWrite = myDigitalWrite ;
  96. node->data2 = wiringPiI2CRead (fd) ;
  97. return TRUE ;
  98. }