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.

пре 8 година
пре 8 година
пре 8 година
пре 8 година
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. * plot the input value on the terminal as a sort of vertical scrolling
  10. * oscilloscope.
  11. *
  12. * Copyright (c) 2012-2013 Gordon Henderson.
  13. ***********************************************************************
  14. * This file is part of wiringPi:
  15. * https://github.com/WiringPi/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 <sys/ioctl.h>
  33. #include <stdlib.h>
  34. #include <math.h>
  35. // Gertboard D to A is an 8-bit unit.
  36. #define B_SIZE 256
  37. #include <wiringPi.h>
  38. #include <gertboard.h>
  39. int main (void)
  40. {
  41. double angle ;
  42. int i, inputValue ;
  43. int buffer [B_SIZE] ;
  44. int cols ;
  45. struct winsize w ;
  46. printf ("Raspberry Pi Gertboard SPI test program\n") ;
  47. printf ("=======================================\n") ;
  48. ioctl (fileno (stdin), TIOCGWINSZ, &w);
  49. cols = w.ws_col - 2 ;
  50. // Always initialise wiringPi. Use wiringPiSys() if you don't need
  51. // (or want) to run as root
  52. wiringPiSetupSys () ;
  53. // Initialise the Gertboard analog hardware at pin 100
  54. gertboardAnalogSetup (100) ;
  55. // Generate a Sine Wave and store in our buffer
  56. for (i = 0 ; i < B_SIZE ; ++i)
  57. {
  58. angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
  59. buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
  60. }
  61. // Loop, output the sine wave on analog out port 0, read it into A-D port 0
  62. // and display it on the screen
  63. for (;;)
  64. {
  65. for (i = 0 ; i < B_SIZE ; ++i)
  66. {
  67. analogWrite (100, buffer [i]) ;
  68. inputValue = analogRead (100) ;
  69. // We don't need to wory about the scale or sign - the analog hardware is
  70. // a 10-bit value, so 0-1023. Just scale this to our terminal
  71. printf ("%*s\n", (inputValue * cols) / 1023, "*") ;
  72. delay (2) ;
  73. }
  74. }
  75. return 0 ;
  76. }