No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

91 líneas
2.5 KiB

  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 "wiringPi.h"
  27. #include "wiringPiI2C.h"
  28. #include "pcf8591.h"
  29. /*
  30. * myAnalogWrite:
  31. *********************************************************************************
  32. */
  33. static void myAnalogWrite (struct wiringPiNodeStruct *node, UNU int pin, int value)
  34. {
  35. unsigned char b [2] ;
  36. b [0] = 0x40 ;
  37. b [1] = value & 0xFF ;
  38. write (node->fd, b, 2) ;
  39. }
  40. /*
  41. * myAnalogRead:
  42. *********************************************************************************
  43. */
  44. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  45. {
  46. int x ;
  47. wiringPiI2CWrite (node->fd, 0x40 | ((pin - node->pinBase) & 3)) ;
  48. x = wiringPiI2CRead (node->fd) ; // Throw away the first read
  49. x = wiringPiI2CRead (node->fd) ;
  50. return x ;
  51. }
  52. /*
  53. * pcf8591Setup:
  54. * Create a new instance of a PCF8591 I2C GPIO interface. We know it
  55. * has 4 pins, (4 analog inputs and 1 analog output which we'll shadow
  56. * input 0) so all we need to know here is the I2C address and the
  57. * user-defined pin base.
  58. *********************************************************************************
  59. */
  60. int pcf8591Setup (const int pinBase, const int i2cAddress)
  61. {
  62. int fd ;
  63. struct wiringPiNodeStruct *node ;
  64. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  65. return FALSE ;
  66. node = wiringPiNewNode (pinBase, 4) ;
  67. node->fd = fd ;
  68. node->analogRead = myAnalogRead ;
  69. node->analogWrite = myAnalogWrite ;
  70. return TRUE ;
  71. }