main.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. main.c
  3. raspberry pi zero GPIO read test
  4. For speedup run this example as root, either wis
  5. sudo ./main
  6. or by setting the superuser bit:
  7. sudo chown root:root ./main
  8. sudo chmod u+s ./main
  9. */
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <signal.h>
  13. #include "bcm2835.h"
  14. #include "raspi_gpio_hal.h"
  15. void sigtermhandler(int x)
  16. {
  17. puts("SIGTERM");
  18. bcm2835_close();
  19. exit(0);
  20. }
  21. void siginthandler(int x)
  22. {
  23. puts("SIGINT");
  24. bcm2835_close();
  25. exit(0);
  26. }
  27. int main(void)
  28. {
  29. int i = 0;
  30. int pins[4] = { 12, 13, 16, 17 };
  31. if (!bcm2835_init())
  32. exit(1);
  33. signal(SIGTERM, sigtermhandler);
  34. signal(SIGINT, siginthandler);
  35. atexit((void (*) (void))bcm2835_close);
  36. for( i = 0; i < sizeof(pins)/sizeof(*pins); i++ )
  37. {
  38. bcm2835_gpio_fsel(pins[i], BCM2835_GPIO_FSEL_INPT);
  39. //bcm2835_gpio_hen(pins[i]); // high level detect
  40. }
  41. for(;;)
  42. {
  43. delaynanoseconds(500000000UL);
  44. printf("%07d ", i);
  45. for( i = 0; i < sizeof(pins)/sizeof(*pins); i++ )
  46. {
  47. printf("%d: %d ", pins[i], bcm2835_gpio_lev(pins[i]));
  48. }
  49. puts("");
  50. /*
  51. for( i = 0; i < sizeof(pins)/sizeof(*pins); i++ )
  52. {
  53. if ( bcm2835_gpio_eds(pins[i]) ) // event detected
  54. {
  55. bcm2835_gpio_set_eds(pins[i]); // clear the event
  56. printf("Event: %d\n", pins[i]);
  57. }
  58. }
  59. */
  60. i++;
  61. }
  62. }