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_piface_test1.c 3.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // WiringPi piface program IN, OUT, PULL
  2. // Compile: gcc -Wall wiringpi_piface_test1.c -o wiringpi_piface_test1 -lwiringPi -lwiringPiDev
  3. #include "wpi_test.h"
  4. #include <piFace.h>
  5. // Use 200 as the pin-base for the PiFace board, and change all pins
  6. // for the LED and relays
  7. const int PIFACE = 200; //Mapped wiringpi IO address
  8. const int defaultsleep = 200000; // 200 ms
  9. void ReadUntilTimeout(int GPIO, int expect, int timeoutSec) {
  10. const int intervaluS = 250000; //250ms
  11. int in;
  12. const char* strexpect = expect ? "HIGH" : "LOW";
  13. for(int loop=0, end=(timeoutSec*1000000/intervaluS); loop<end; ++loop) {
  14. in = digitalRead(GPIO);
  15. if (in==expect) {
  16. printf( "took %g sec to set %s\n", loop*intervaluS/1000000.0, strexpect);
  17. break;
  18. }
  19. delayMicroseconds(intervaluS);
  20. printf(".");fflush(stdout);
  21. }
  22. if (in!=expect) {
  23. printf( "timeout reached %d sec to set %s\n", timeoutSec, strexpect);
  24. }
  25. CheckGPIO(GPIO, -1, expect) ;
  26. }
  27. int main (int argc, char *argv []) {
  28. int major, minor;
  29. wiringPiVersion(&major, &minor);
  30. printf("Testing piface functions with WiringPi %d.%d\n",major, minor);
  31. printf("------------------------------------------\n\n");
  32. // initialise wiringPi
  33. if (wiringPiSetupSys() == -1) {
  34. printf("wiringPiSetupSys failed\n\n");
  35. exit(EXIT_FAILURE);
  36. }
  37. piFaceSetup (PIFACE); // Setup the PiFace board with default addr 0, 0
  38. const int RELAY0 = PIFACE+0;
  39. const int RELAY0IN = PIFACE+6;
  40. const int RELAY1 = PIFACE+1;
  41. const int RELAY1IN = PIFACE+7;
  42. printf("\nRelays async:\n");
  43. pullUpDnControl(RELAY0IN, PUD_UP);
  44. pullUpDnControl(RELAY1IN, PUD_UP);
  45. for (int loop = 0, end=3 ; loop<end ; ++loop) {
  46. int sleep = defaultsleep*(end-loop);
  47. printf("sleep %d ms:\n", sleep/1000);
  48. digitalWrite (RELAY0, HIGH);
  49. delayMicroseconds(sleep);
  50. CheckInversGPIO(RELAY0IN, -1, HIGH) ;
  51. digitalWrite (RELAY0, LOW);
  52. delayMicroseconds(sleep);
  53. CheckInversGPIO(RELAY0IN, -1, LOW);
  54. digitalWrite (RELAY1, HIGH);
  55. delayMicroseconds(sleep);
  56. CheckInversGPIO(RELAY1IN, -1, HIGH);
  57. digitalWrite (RELAY1, LOW);
  58. delayMicroseconds(sleep);
  59. CheckInversGPIO(RELAY1IN, -1, LOW);
  60. }
  61. const int OUT7 = PIFACE+7;
  62. const int OUT7IN = PIFACE+4;
  63. const int OUT6 = PIFACE+6;
  64. const int OUT6IN = PIFACE+5;
  65. printf("\nOUT6/7 sync:\n");
  66. delayMicroseconds(defaultsleep);
  67. for (int loop = 0, end=3 ; loop<end ; ++loop) {
  68. digitalWrite (OUT7, HIGH);
  69. delayMicroseconds(defaultsleep);
  70. CheckInversGPIO(OUT7IN, -1, HIGH);
  71. digitalWrite (OUT7, LOW);
  72. delayMicroseconds(defaultsleep);
  73. CheckInversGPIO(OUT7IN, -1, LOW);
  74. digitalWrite (OUT6, HIGH);
  75. delayMicroseconds(defaultsleep);
  76. CheckInversGPIO(OUT6IN, -1, HIGH);
  77. digitalWrite (OUT6, LOW);
  78. delayMicroseconds(defaultsleep);
  79. CheckInversGPIO(OUT6IN, -1, LOW);
  80. }
  81. printf("\nRelays sync:\n");
  82. for (int loop = 0, end=3 ; loop<end ; ++loop) {
  83. int sleep = defaultsleep*(end-loop);
  84. printf("sleep %d ms:\n", sleep/1000);
  85. digitalWrite (RELAY0, HIGH);
  86. digitalWrite (RELAY1, HIGH);
  87. delayMicroseconds(sleep);
  88. CheckInversGPIO(RELAY0IN, -1, HIGH) ;
  89. CheckInversGPIO(RELAY1IN, -1, HIGH) ;
  90. digitalWrite (RELAY0, LOW);
  91. digitalWrite (RELAY1, LOW);
  92. delayMicroseconds(sleep);
  93. CheckInversGPIO(RELAY0IN, -1, LOW) ;
  94. CheckInversGPIO(RELAY1IN, -1, LOW) ;
  95. }
  96. printf("\nInput pull up/down resistor:\n");
  97. for (int IN = 0 ; IN <= 7 ; ++IN) {
  98. if (4==IN || 5==IN) {
  99. continue; //4 & 5 connected from out to in -> test not possible
  100. }
  101. //6 & 7 connected from relais NO (normaly open) to in -> test possible
  102. delayMicroseconds(defaultsleep);
  103. pullUpDnControl (PIFACE + IN, PUD_UP) ;
  104. ReadUntilTimeout(PIFACE + IN, HIGH, 2) ;
  105. pullUpDnControl (PIFACE + IN, PUD_DOWN) ;
  106. // cool down very slowly, connect 680 kOhm pull down resistor to make ist faster
  107. ReadUntilTimeout(PIFACE + IN, LOW, 60) ;
  108. pullUpDnControl (PIFACE + IN, PUD_UP) ; // finally up
  109. ReadUntilTimeout(PIFACE + IN, HIGH, 2) ;
  110. }
  111. return UnitTestState();
  112. }