Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

126 рядки
3.3 KiB

  1. /*
  2. * mcp3422.c:
  3. * Extend wiringPi with the MCP3422/3/4 I2C ADC chip
  4. * This code assumes single-ended mode only.
  5. * Tested on actual hardware: 20th Feb 2016.
  6. * Copyright (c) 2013-2016 Gordon Henderson
  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 <stdio.h>
  27. #include <unistd.h>
  28. #include <stdint.h>
  29. #include <fcntl.h>
  30. #include <sys/ioctl.h>
  31. #include <wiringPi.h>
  32. #include <wiringPiI2C.h>
  33. #include "mcp3422.h"
  34. /*
  35. * waitForConversion:
  36. * Common code to wait for the ADC to finish conversion
  37. *********************************************************************************
  38. */
  39. void waitForConversion (int fd, unsigned char *buffer, int n)
  40. {
  41. for (;;)
  42. {
  43. read (fd, buffer, n) ;
  44. if ((buffer [n-1] & 0x80) == 0)
  45. break ;
  46. delay (1) ;
  47. }
  48. }
  49. /*
  50. * myAnalogRead:
  51. * Read a channel from the device
  52. *********************************************************************************
  53. */
  54. int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
  55. {
  56. unsigned char config ;
  57. unsigned char buffer [4] ;
  58. int value = 0 ;
  59. int realChan = (chan & 3) - node->pinBase ;
  60. // One-shot mode, trigger plus the other configs.
  61. config = 0x80 | (realChan << 5) | (node->data0 << 2) | (node->data1) ;
  62. wiringPiI2CWrite (node->fd, config) ;
  63. switch (node->data0) // Sample rate
  64. {
  65. case MCP3422_SR_3_75: // 18 bits
  66. waitForConversion (node->fd, &buffer [0], 4) ;
  67. value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [2] ;
  68. break ;
  69. case MCP3422_SR_15: // 16 bits
  70. waitForConversion (node->fd, buffer, 3) ;
  71. value = (buffer [0] << 8) | buffer [1] ;
  72. break ;
  73. case MCP3422_SR_60: // 14 bits
  74. waitForConversion (node->fd, buffer, 3) ;
  75. value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
  76. break ;
  77. case MCP3422_SR_240: // 12 bits - default
  78. waitForConversion (node->fd, buffer, 3) ;
  79. value = ((buffer [0] & 0x0F) << 8) | buffer [1] ;
  80. break ;
  81. }
  82. return value ;
  83. }
  84. /*
  85. * mcp3422Setup:
  86. * Create a new wiringPi device node for the mcp3422
  87. *********************************************************************************
  88. */
  89. int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
  90. {
  91. int fd ;
  92. struct wiringPiNodeStruct *node ;
  93. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  94. return FALSE ;
  95. node = wiringPiNewNode (pinBase, 4) ;
  96. node->fd = fd ;
  97. node->data0 = sampleRate ;
  98. node->data1 = gain ;
  99. node->analogRead = myAnalogRead ;
  100. return TRUE ;
  101. }