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.

gertboard.c 1.4 KiB

12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
12 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. */
  13. #include <stdio.h>
  14. #include <stdint.h>
  15. #include <math.h>
  16. #define B_SIZE 200
  17. #undef DO_TIMING
  18. #include <wiringPi.h>
  19. #include <gertboard.h>
  20. int main (void)
  21. {
  22. double angle ;
  23. int i ;
  24. uint32_t x1 ;
  25. int buffer [B_SIZE] ;
  26. #ifdef DO_TIMING
  27. unsigned int now, then ;
  28. #endif
  29. printf ("Raspberry Pi Gertboard SPI test program\n") ;
  30. if (wiringPiSetupSys () < 0)
  31. return -1 ;
  32. if (gertboardSPISetup () < 0)
  33. return 1 ;
  34. // Generate a Sine Wave
  35. for (i = 0 ; i < B_SIZE ; ++i)
  36. {
  37. angle = ((double)i / (double)B_SIZE) * M_PI * 2.0 ;
  38. buffer [i] = (int)rint ((sin (angle)) * 127.0 + 128.0) ;
  39. }
  40. for (;;)
  41. {
  42. #ifdef DO_TIMING
  43. then = millis () ;
  44. #endif
  45. for (i = 0 ; i < B_SIZE ; ++i)
  46. {
  47. gertboardAnalogWrite (0, buffer [i]) ;
  48. #ifndef DO_TIMING
  49. x1 = gertboardAnalogRead (0) ;
  50. gertboardAnalogWrite (1, x1 >> 2) ; // 10-bit A/D, 8-bit D/A
  51. #endif
  52. }
  53. #ifdef DO_TIMING
  54. now = millis () ;
  55. printf ("%4d mS, %9.7f S/sample", now - then, ((double)(now - then) / 1000.0) / (double)B_SIZE) ;
  56. printf (" -> %9.4f samples/sec \n", 1 / (((double)(now - then) / 1000.0) / (double)B_SIZE)) ;
  57. #endif
  58. }
  59. return 0 ;
  60. }