25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

max31855.c 2.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * max31855.c:
  3. * Extend wiringPi with the max31855 SPI Analog to Digital convertor
  4. * Copyright (c) 2012-2013 Gordon Henderson
  5. ***********************************************************************
  6. * This file is part of wiringPi:
  7. * https://projects.drogon.net/raspberry-pi/wiringpi/
  8. *
  9. * wiringPi is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * wiringPi is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with wiringPi.
  21. * If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. #include <wiringPi.h>
  25. #include <wiringPiSPI.h>
  26. #include "max31855.h"
  27. /*
  28. * myAnalogRead:
  29. * Return the analog value of the given pin
  30. * Note: The chip really only has one read "channel", but we're faking it
  31. * here so we can read the error registers. Channel 0 will be the data
  32. * channel, and 1 is the error register code.
  33. * Note: Temperature returned is temp in C * 4, so divide result by 4
  34. *********************************************************************************
  35. */
  36. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  37. {
  38. unsigned int spiData ;
  39. int temp ;
  40. int chan = pin - node->pinBase ;
  41. wiringPiSPIDataRW (node->fd, (unsigned char *)&spiData, 4) ;
  42. if (chan == 0) // Read temp in C
  43. {
  44. spiData >>= 18 ;
  45. temp = spiData & 0x3FFF ; // Bottom 13 bits
  46. if ((spiData & 0x2000) != 0) // Negative
  47. temp = -temp ;
  48. return temp ;
  49. }
  50. else // Return error bits
  51. return spiData & 0x7 ;
  52. }
  53. /*
  54. * max31855Setup:
  55. * Create a new wiringPi device node for an max31855 on the Pi's
  56. * SPI interface.
  57. *********************************************************************************
  58. */
  59. int max31855Setup (const int pinBase, int spiChannel)
  60. {
  61. struct wiringPiNodeStruct *node ;
  62. if (wiringPiSPISetup (spiChannel, 5000000) < 0) // 5MHz - prob 4 on the Pi
  63. return -1 ;
  64. node = wiringPiNewNode (pinBase, 2) ;
  65. node->fd = spiChannel ;
  66. node->analogRead = myAnalogRead ;
  67. return 0 ;
  68. }