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.

wiringpi_test1_device.c 2.0 KiB

7 kuukautta sitten
7 kuukautta sitten
7 kuukautta sitten
7 kuukautta sitten
7 kuukautta sitten
7 kuukautta sitten
7 kuukautta sitten
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // WiringPi test program: Kernel char device interface / sysfs successor
  2. // Compile: gcc -Wall wiringpi_test1_device.c -o wiringpi_test1_device -lwiringPi
  3. #include <wiringPi.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <signal.h>
  7. #include <string.h>
  8. #include <time.h>
  9. #include <sys/time.h>
  10. const int GPIO = 19;
  11. const int GPIOIN = 26;
  12. const int ToggleValue = 4;
  13. void CheckGPIO(int out) {
  14. int in = digitalRead(GPIOIN);
  15. int read = digitalRead(GPIO);
  16. int pass = 0;
  17. if (out==in && in==read) {
  18. pass = 1;
  19. }
  20. printf("GPIO%d = %d (GPIO%d = %d) -> %s\n", GPIOIN, in, GPIO, read, pass ? "passed":"failed" );
  21. }
  22. void digitalWriteEx(int pin, int mode) {
  23. digitalWrite(pin, mode);
  24. printf("out = %d ", mode);
  25. delayMicroseconds(5000);
  26. CheckGPIO(mode);
  27. }
  28. void pullUpDnControlEx (int pin ,int mode) {
  29. pullUpDnControl (pin, mode);
  30. int out = mode==PUD_UP ? 1:0;
  31. printf("in = %4s ", mode==PUD_UP ? "up":"down");
  32. delayMicroseconds(5000);
  33. CheckGPIO(out);
  34. }
  35. int main (void) {
  36. printf("WiringPi GPIO test program 1 (using GPIO%d (output) and GPIO%d (input) via sys)\n", GPIO, GPIOIN);
  37. printf(" testing digitalWrite, digitalRead and pullUpDnControl\n");
  38. if (wiringPiSetupSys() == -1) {
  39. printf("wiringPiSetupGpioDevice failed\n\n");
  40. exit(EXIT_FAILURE);
  41. }
  42. pinMode(GPIOIN, INPUT);
  43. pinMode(GPIO, OUTPUT);
  44. printf("toggle %d times ...\n", ToggleValue);
  45. for (int loop=1; loop<ToggleValue; loop++) {
  46. digitalWriteEx(GPIO, LOW);
  47. delayMicroseconds(600000);
  48. digitalWriteEx(GPIO, HIGH);
  49. delayMicroseconds(600000);
  50. }
  51. digitalWrite(GPIO, LOW);
  52. printf("\nWiringPi GPIO test program (using GPIO%d (input pull up/down) and GPIO%d (input) via sys)\n", GPIO, GPIOIN);
  53. pullUpDnControl (GPIO, PUD_UP);
  54. pinMode(GPIO, INPUT);
  55. delayMicroseconds(3000000);
  56. pullUpDnControl (GPIOIN, PUD_OFF);
  57. for (int loop=1; loop<ToggleValue; loop++) {
  58. pullUpDnControlEx (GPIO, PUD_DOWN);
  59. delayMicroseconds(600000);
  60. pullUpDnControlEx (GPIO, PUD_UP);
  61. delayMicroseconds(600000);
  62. }
  63. //Error wrong direction - only for fun
  64. digitalWrite(GPIO, LOW);
  65. return(EXIT_SUCCESS);
  66. }