You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
пре 12 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * gertboard.c:
  3. * Access routines for the SPI devices on the Gertboard
  4. * Copyright (c) 2012 Gordon Henderson
  5. *
  6. * The Gertboard has:
  7. *
  8. * An MCP3002 dual-channel A to D convertor connected
  9. * to the SPI bus, selected by chip-select A, and:
  10. *
  11. * An MCP4802 dual-channel D to A convertor connected
  12. * to the SPI bus, selected via chip-select B.
  13. *
  14. ***********************************************************************
  15. * This file is part of wiringPi:
  16. * https://projects.drogon.net/raspberry-pi/wiringpi/
  17. *
  18. * wiringPi is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Lesser General Public License as
  20. * published by the Free Software Foundation, either version 3 of the
  21. * License, or (at your option) any later version.
  22. *
  23. * wiringPi is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Lesser General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Lesser General Public
  29. * License along with wiringPi.
  30. * If not, see <http://www.gnu.org/licenses/>.
  31. ***********************************************************************
  32. */
  33. #include <stdio.h>
  34. #include <stdint.h>
  35. #include <fcntl.h>
  36. #include <sys/ioctl.h>
  37. #include <linux/spi/spidev.h>
  38. #include "wiringPiSPI.h"
  39. #include "gertboard.h"
  40. // The A-D convertor won't run at more than 1MHz @ 3.3v
  41. #define SPI_ADC_SPEED 1000000
  42. #define SPI_DAC_SPEED 1000000
  43. #define SPI_A2D 0
  44. #define SPI_D2A 1
  45. /*
  46. * gertboardAnalogWrite:
  47. * Write an 8-bit data value to the MCP4802 Analog to digital
  48. * convertor on the Gertboard.
  49. *********************************************************************************
  50. */
  51. void gertboardAnalogWrite (int chan, int value)
  52. {
  53. uint8_t spiData [2] ;
  54. uint8_t chanBits, dataBits ;
  55. if (chan == 0)
  56. chanBits = 0x30 ;
  57. else
  58. chanBits = 0xB0 ;
  59. chanBits |= ((value >> 4) & 0x0F) ;
  60. dataBits = ((value << 4) & 0xF0) ;
  61. spiData [0] = chanBits ;
  62. spiData [1] = dataBits ;
  63. wiringPiSPIDataRW (SPI_D2A, spiData, 2) ;
  64. }
  65. /*
  66. * gertboardAnalogRead:
  67. * Return the analog value of the given channel (0/1).
  68. * The A/D is a 10-bit device
  69. *********************************************************************************
  70. */
  71. int gertboardAnalogRead (int chan)
  72. {
  73. uint8_t spiData [2] ;
  74. uint8_t chanBits ;
  75. if (chan == 0)
  76. chanBits = 0b11010000 ;
  77. else
  78. chanBits = 0b11110000 ;
  79. spiData [0] = chanBits ;
  80. spiData [1] = 0 ;
  81. wiringPiSPIDataRW (SPI_A2D, spiData, 2) ;
  82. return ((spiData [0] << 7) | (spiData [1] >> 1)) & 0x3FF ;
  83. }
  84. /*
  85. * gertboardSPISetup:
  86. * Initialise the SPI bus, etc.
  87. *********************************************************************************
  88. */
  89. int gertboardSPISetup (void)
  90. {
  91. if (wiringPiSPISetup (SPI_A2D, SPI_ADC_SPEED) < 0)
  92. return -1 ;
  93. if (wiringPiSPISetup (SPI_D2A, SPI_DAC_SPEED) < 0)
  94. return -1 ;
  95. return 0 ;
  96. }