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

max5322.c 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * max5322.c:
  3. * Extend wiringPi with the MAX5322 SPI Digital to Analog 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 "max5322.h"
  27. /*
  28. * myAnalogWrite:
  29. * Write analog value on the given pin
  30. *********************************************************************************
  31. */
  32. static void myAnalogWrite (struct wiringPiNodeStruct *node, int pin, int value)
  33. {
  34. unsigned char spiData [2] ;
  35. unsigned char chanBits, dataBits ;
  36. int chan = pin - node->pinBase ;
  37. if (chan == 0)
  38. chanBits = 0b01000000 ;
  39. else
  40. chanBits = 0b01010000 ;
  41. chanBits |= ((value >> 12) & 0x0F) ;
  42. dataBits = ((value ) & 0xFF) ;
  43. spiData [0] = chanBits ;
  44. spiData [1] = dataBits ;
  45. wiringPiSPIDataRW (node->fd, spiData, 2) ;
  46. }
  47. /*
  48. * max5322Setup:
  49. * Create a new wiringPi device node for an max5322 on the Pi's
  50. * SPI interface.
  51. *********************************************************************************
  52. */
  53. int max5322Setup (const int pinBase, int spiChannel)
  54. {
  55. struct wiringPiNodeStruct *node ;
  56. unsigned char spiData [2] ;
  57. if (wiringPiSPISetup (spiChannel, 8000000) < 0) // 10MHz Max
  58. return FALSE ;
  59. node = wiringPiNewNode (pinBase, 2) ;
  60. node->fd = spiChannel ;
  61. node->analogWrite = myAnalogWrite ;
  62. // Enable both DACs
  63. spiData [0] = 0b11100000 ;
  64. spiData [1] = 0 ;
  65. wiringPiSPIDataRW (node->fd, spiData, 2) ;
  66. return TRUE ;
  67. }