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.
 
 
 
 
 

118 wiersze
3.2 KiB

  1. /*
  2. * mcp3422.c:
  3. * Extend wiringPi with the MCP3422 I2C ADC chip
  4. * Also works for the MCP3423 and MCP3224 (4 channel) chips
  5. * Copyright (c) 2013 Gordon Henderson
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://projects.drogon.net/raspberry-pi/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 <stdio.h>
  26. #include <stdint.h>
  27. #include <fcntl.h>
  28. #include <sys/ioctl.h>
  29. #include <linux/spi/spidev.h>
  30. #include <wiringPi.h>
  31. #include <wiringPiI2C.h>
  32. #include "mcp3422.h"
  33. /*
  34. * myAnalogRead:
  35. * Read a channel from the device
  36. *********************************************************************************
  37. */
  38. int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
  39. {
  40. unsigned char config, b0, b1, b2, b3 ;
  41. int value = 0 ;
  42. // One-shot mode, trigger plus the other configs.
  43. config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ;
  44. wiringPiI2CWrite (node->fd, config) ;
  45. switch (node->data0) // Sample rate
  46. {
  47. case MCP3422_SR_3_75: // 18 bits
  48. delay (270) ;
  49. b0 = wiringPiI2CRead (node->fd) ;
  50. b1 = wiringPiI2CRead (node->fd) ;
  51. b2 = wiringPiI2CRead (node->fd) ;
  52. b3 = wiringPiI2CRead (node->fd) ;
  53. value = ((b0 & 3) << 16) | (b1 << 8) | b2 ;
  54. break ;
  55. case MCP3422_SR_15: // 16 bits
  56. delay ( 70) ;
  57. b0 = wiringPiI2CRead (node->fd) ;
  58. b1 = wiringPiI2CRead (node->fd) ;
  59. b2 = wiringPiI2CRead (node->fd) ;
  60. value = (b0 << 8) | b1 ;
  61. break ;
  62. case MCP3422_SR_60: // 14 bits
  63. delay ( 17) ;
  64. b0 = wiringPiI2CRead (node->fd) ;
  65. b1 = wiringPiI2CRead (node->fd) ;
  66. b2 = wiringPiI2CRead (node->fd) ;
  67. value = ((b0 & 0x3F) << 8) | b1 ;
  68. break ;
  69. case MCP3422_SR_240: // 12 bits
  70. delay ( 5) ;
  71. b0 = wiringPiI2CRead (node->fd) ;
  72. b1 = wiringPiI2CRead (node->fd) ;
  73. b2 = wiringPiI2CRead (node->fd) ;
  74. value = ((b0 & 0x0F) << 8) | b1 ;
  75. break ;
  76. }
  77. return value ;
  78. }
  79. /*
  80. * mcp3422Setup:
  81. * Create a new wiringPi device node for the mcp3422
  82. *********************************************************************************
  83. */
  84. int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
  85. {
  86. int fd ;
  87. struct wiringPiNodeStruct *node ;
  88. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  89. return fd ;
  90. node = wiringPiNewNode (pinBase, 4) ;
  91. node->data0 = sampleRate ;
  92. node->data1 = gain ;
  93. node->analogRead = myAnalogRead ;
  94. return 0 ;
  95. }