您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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://github.com/WiringPi/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. ssize_t bytes_read = read(fd, buffer, n);
  43. if (bytes_read != n) {
  44. perror("Error reading from file descriptor");
  45. return;
  46. }
  47. if ((buffer[n - 1] & 0x80) == 0)
  48. break;
  49. delay(1);
  50. }
  51. }
  52. /*
  53. * myAnalogRead:
  54. * Read a channel from the device
  55. *********************************************************************************
  56. */
  57. int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
  58. {
  59. unsigned char config ;
  60. unsigned char buffer [4] ;
  61. int value = 0 ;
  62. int realChan = (chan & 3) - node->pinBase ;
  63. // One-shot mode, trigger plus the other configs.
  64. config = 0x80 | (realChan << 5) | (node->data0 << 2) | (node->data1) ;
  65. wiringPiI2CWrite (node->fd, config) ;
  66. switch (node->data0) // Sample rate
  67. {
  68. case MCP3422_SR_3_75: // 18 bits
  69. waitForConversion (node->fd, &buffer [0], 4) ;
  70. value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [2] ;
  71. break ;
  72. case MCP3422_SR_15: // 16 bits
  73. waitForConversion (node->fd, buffer, 3) ;
  74. value = (buffer [0] << 8) | buffer [1] ;
  75. break ;
  76. case MCP3422_SR_60: // 14 bits
  77. waitForConversion (node->fd, buffer, 3) ;
  78. value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
  79. break ;
  80. case MCP3422_SR_240: // 12 bits - default
  81. waitForConversion (node->fd, buffer, 3) ;
  82. value = ((buffer [0] & 0x0F) << 8) | buffer [1] ;
  83. break ;
  84. }
  85. return value ;
  86. }
  87. /*
  88. * mcp3422Setup:
  89. * Create a new wiringPi device node for the mcp3422
  90. *********************************************************************************
  91. */
  92. int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
  93. {
  94. int fd ;
  95. struct wiringPiNodeStruct *node ;
  96. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  97. return FALSE ;
  98. node = wiringPiNewNode (pinBase, 4) ;
  99. node->fd = fd ;
  100. node->data0 = sampleRate ;
  101. node->data1 = gain ;
  102. node->analogRead = myAnalogRead ;
  103. return TRUE ;
  104. }