選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 

111 行
2.9 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 <unistd.h>
  27. #include <stdint.h>
  28. #include <fcntl.h>
  29. #include <sys/ioctl.h>
  30. #include <linux/spi/spidev.h>
  31. #include <wiringPi.h>
  32. #include <wiringPiI2C.h>
  33. #include "mcp3422.h"
  34. /*
  35. * myAnalogRead:
  36. * Read a channel from the device
  37. *********************************************************************************
  38. */
  39. int myAnalogRead (struct wiringPiNodeStruct *node, int chan)
  40. {
  41. unsigned char config ;
  42. unsigned char buffer [4] ;
  43. int value = 0 ;
  44. // One-shot mode, trigger plus the other configs.
  45. config = 0x80 | ((chan - node->pinBase) << 5) | (node->data0 << 2) | (node->data1) ;
  46. wiringPiI2CWrite (node->fd, config) ;
  47. switch (node->data0) // Sample rate
  48. {
  49. case MCP3422_SR_3_75: // 18 bits
  50. delay (270) ;
  51. read (node->fd, buffer, 4) ;
  52. value = ((buffer [0] & 3) << 16) | (buffer [1] << 8) | buffer [0] ;
  53. break ;
  54. case MCP3422_SR_15: // 16 bits
  55. delay ( 70) ;
  56. read (node->fd, buffer, 3) ;
  57. value = (buffer [0] << 8) | buffer [1] ;
  58. break ;
  59. case MCP3422_SR_60: // 14 bits
  60. delay ( 17) ;
  61. read (node->fd, buffer, 3) ;
  62. value = ((buffer [0] & 0x3F) << 8) | buffer [1] ;
  63. break ;
  64. case MCP3422_SR_240: // 12 bits
  65. delay ( 5) ;
  66. read (node->fd, buffer, 3) ;
  67. value = ((buffer [0] & 0x0F) << 8) | buffer [0] ;
  68. break ;
  69. }
  70. return value ;
  71. }
  72. /*
  73. * mcp3422Setup:
  74. * Create a new wiringPi device node for the mcp3422
  75. *********************************************************************************
  76. */
  77. int mcp3422Setup (int pinBase, int i2cAddress, int sampleRate, int gain)
  78. {
  79. int fd ;
  80. struct wiringPiNodeStruct *node ;
  81. if ((fd = wiringPiI2CSetup (i2cAddress)) < 0)
  82. return fd ;
  83. node = wiringPiNewNode (pinBase, 4) ;
  84. node->data0 = sampleRate ;
  85. node->data1 = gain ;
  86. node->analogRead = myAnalogRead ;
  87. return 0 ;
  88. }