gertboard.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * gertboard.c:
  3. * Simple test for the SPI bus on the Gertboard
  4. *
  5. * Hardware setup:
  6. * D/A port 0 jumpered to A/D port 0.
  7. *
  8. * We output a sine wave on D/A port 0 and sample A/D port 0. We then
  9. * copy this value to D/A port 1 and use a 'scope on both D/A ports
  10. * to check all's well.
  11. *
  12. * Copyright (c) 2012-2013 Gordon Henderson. <projects@drogon.net>
  13. ***********************************************************************
  14. * This file is part of wiringPi:
  15. * https://projects.drogon.net/raspberry-pi/wiringpi/
  16. *
  17. * wiringPi is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Lesser General Public License as published by
  19. * the Free Software Foundation, either version 3 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * wiringPi is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Lesser General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Lesser General Public License
  28. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  29. ***********************************************************************
  30. */
  31. #include <stdio.h>
  32. #include <stdint.h>
  33. #include <math.h>
  34. #define B_SIZE 200
  35. #undef DO_TIMING
  36. #include <wiringPi.h>
  37. #include <gertboard.h>
  38. int main (void)
  39. {
  40. double angle ;
  41. int i ;
  42. uint32_t x1 ;
  43. int buffer [B_SIZE] ;
  44. #ifdef DO_TIMING
  45. unsigned int now, then ;
  46. #endif
  47. printf ("Raspberry Pi Gertboard SPI test program\n") ;
  48. if (wiringPiSetupSys () < 0)
  49. return -1 ;
  50. if (gertboardSPISetup () < 0)
  51. return 1 ;
  52. // Generate a Sine Wave
  53. for (i = 0 ; i < B_SIZE ; ++i)
  54. {
  55. angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
  56. buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
  57. }
  58. for (;;)
  59. {
  60. #ifdef DO_TIMING
  61. then = millis () ;
  62. #endif
  63. for (i = 0 ; i < B_SIZE ; ++i)
  64. {
  65. gertboardAnalogWrite (0, buffer [i]) ;
  66. #ifndef DO_TIMING
  67. x1 = gertboardAnalogRead (0) ;
  68. gertboardAnalogWrite (1, x1 >> 2) ; // 10-bit A/D, 8-bit D/A
  69. #endif
  70. }
  71. #ifdef DO_TIMING
  72. now = millis () ;
  73. printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ;
  74. printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ;
  75. #endif
  76. }
  77. return 0 ;
  78. }