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.
 
 
 
 
 

77 wiersze
2.2 KiB

  1. /*
  2. * mcp3004.c:
  3. * Extend wiringPi with the MCP3004 SPI Analog to Digital convertor
  4. * Copyright (c) 2012-2013 Gordon Henderson
  5. *
  6. * Thanks also to "ShorTie" on IRC for some remote debugging help!
  7. ***********************************************************************
  8. * This file is part of wiringPi:
  9. * https://projects.drogon.net/raspberry-pi/wiringpi/
  10. *
  11. * wiringPi is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * wiringPi is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with wiringPi.
  23. * If not, see <http://www.gnu.org/licenses/>.
  24. ***********************************************************************
  25. */
  26. #include <wiringPi.h>
  27. #include <wiringPiSPI.h>
  28. #include "mcp3004.h"
  29. /*
  30. * myAnalogRead:
  31. * Return the analog value of the given pin
  32. *********************************************************************************
  33. */
  34. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  35. {
  36. unsigned char spiData [3] ;
  37. unsigned char chanBits ;
  38. int chan = pin - node->pinBase ;
  39. chanBits = 0b10000000 | (chan << 4) ;
  40. spiData [0] = 1 ; // Start bit
  41. spiData [1] = chanBits ;
  42. spiData [2] = 0 ;
  43. wiringPiSPIDataRW (node->fd, spiData, 3) ;
  44. return ((spiData [1] << 8) | spiData [2]) & 0x3FF ;
  45. }
  46. /*
  47. * mcp3004Setup:
  48. * Create a new wiringPi device node for an mcp3004 on the Pi's
  49. * SPI interface.
  50. *********************************************************************************
  51. */
  52. int mcp3004Setup (const int pinBase, int spiChannel)
  53. {
  54. struct wiringPiNodeStruct *node ;
  55. if (wiringPiSPISetup (spiChannel, 1000000) < 0)
  56. return -1 ;
  57. node = wiringPiNewNode (pinBase, 8) ;
  58. node->fd = spiChannel ;
  59. node->analogRead = myAnalogRead ;
  60. return 0 ;
  61. }