Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

pcf8591.c 2.6 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * pcf8591.c:
  3. * Extend wiringPi with the PCF8591 I2C GPIO Analog expander chip
  4. * The chip has 1 8-bit DAC and 4 x 8-bit ADCs
  5. * Copyright (c) 2013 Gordon Henderson
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://github.com/WiringPi/WiringPi/
  9. *
  10. * wiringPi is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * wiringPi is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with wiringPi.
  22. * If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include "wiringPi.h"
  28. #include "wiringPiI2C.h"
  29. #include "pcf8591.h"
  30. /*
  31. * myAnalogWrite:
  32. *********************************************************************************
  33. */
  34. static void myAnalogWrite (struct wiringPiNodeStruct *node, UNU int pin, int value)
  35. {
  36. unsigned char b [2] ;
  37. b [0] = 0x40 ;
  38. b [1] = value & 0xFF ;
  39. ssize_t bytes_written = write(node->fd, b, 2);
  40. if (bytes_written != 2) {
  41. perror("Error writing to file descriptor");
  42. }
  43. }
  44. /*
  45. * myAnalogRead:
  46. *********************************************************************************
  47. */
  48. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  49. {
  50. int x ;
  51. wiringPiI2CWrite (node->fd, 0x40 | ((pin - node->pinBase) & 3)) ;
  52. x = wiringPiI2CRead (node->fd) ; // Throw away the first read
  53. x = wiringPiI2CRead (node->fd) ;
  54. return x ;
  55. }
  56. /*
  57. * pcf8591Setup:
  58. * Create a new instance of a PCF8591 I2C GPIO interface. We know it
  59. * has 4 pins, (4 analog inputs and 1 analog output which we'll shadow
  60. * input 0) so all we need to know here is the I2C address and the
  61. * user-defined pin base.
  62. *********************************************************************************
  63. */
  64. int pcf8591Setup (const int pinBase, const int i2cAddress)
  65. {
  66. int fd ;
  67. struct wiringPiNodeStruct *node ;
  68. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  69. return FALSE ;
  70. node = wiringPiNewNode (pinBase, 4) ;
  71. node->fd = fd ;
  72. node->analogRead = myAnalogRead ;
  73. node->analogWrite = myAnalogWrite ;
  74. return TRUE ;
  75. }