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.8 KiB

5 kuukautta sitten
5 kuukautta sitten
5 kuukautta sitten
5 kuukautta sitten
5 kuukautta sitten
5 kuukautta sitten
5 kuukautta sitten
5 kuukautta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. for (int loop = 0, end=3 ; loop<end ; ++loop) {
  44. int sleep = defaultsleep*(end-loop);
  45. printf("sleep %d ms:\n", sleep/1000);
  46. digitalWrite (RELAY0, HIGH);
  47. delayMicroseconds(sleep);
  48. CheckInversGPIO(RELAY0IN, -1, HIGH) ;
  49. digitalWrite (RELAY0, LOW);
  50. delayMicroseconds(sleep);
  51. CheckInversGPIO(RELAY0IN, -1, LOW);
  52. digitalWrite (RELAY1, HIGH);
  53. delayMicroseconds(sleep);
  54. CheckInversGPIO(RELAY1IN, -1, HIGH);
  55. digitalWrite (RELAY1, LOW);
  56. delayMicroseconds(sleep);
  57. CheckInversGPIO(RELAY1IN, -1, LOW);
  58. }
  59. const int OUT7 = PIFACE+7;
  60. const int OUT7IN = PIFACE+4;
  61. const int OUT6 = PIFACE+6;
  62. const int OUT6IN = PIFACE+5;
  63. printf("\nOUT6/7 sync:\n");
  64. delayMicroseconds(defaultsleep);
  65. for (int loop = 0, end=3 ; loop<end ; ++loop) {
  66. digitalWrite (OUT7, HIGH);
  67. delayMicroseconds(defaultsleep);
  68. CheckInversGPIO(OUT7IN, -1, HIGH);
  69. digitalWrite (OUT7, LOW);
  70. delayMicroseconds(defaultsleep);
  71. CheckInversGPIO(OUT7IN, -1, LOW);
  72. digitalWrite (OUT6, HIGH);
  73. delayMicroseconds(defaultsleep);
  74. CheckInversGPIO(OUT6IN, -1, HIGH);
  75. digitalWrite (OUT6, LOW);
  76. delayMicroseconds(defaultsleep);
  77. CheckInversGPIO(OUT6IN, -1, LOW);
  78. }
  79. printf("\nRelays sync:\n");
  80. for (int loop = 0, end=3 ; loop<end ; ++loop) {
  81. int sleep = defaultsleep*(end-loop);
  82. printf("sleep %d ms:\n", sleep/1000);
  83. digitalWrite (RELAY0, HIGH);
  84. digitalWrite (RELAY1, HIGH);
  85. delayMicroseconds(sleep);
  86. CheckInversGPIO(RELAY0IN, -1, HIGH) ;
  87. CheckInversGPIO(RELAY1IN, -1, HIGH) ;
  88. digitalWrite (RELAY0, LOW);
  89. digitalWrite (RELAY1, LOW);
  90. delayMicroseconds(sleep);
  91. CheckInversGPIO(RELAY0IN, -1, LOW) ;
  92. CheckInversGPIO(RELAY1IN, -1, LOW) ;
  93. }
  94. printf("\nInput pull up/down resistor:\n");
  95. for (int IN = 0 ; IN <= 7 ; ++IN) {
  96. if (4==IN || 5==IN) {
  97. continue; //4 & 5 connected from out to in -> test not possible
  98. }
  99. //6 & 7 connected from relais NO (normaly open) to in -> test possible
  100. delayMicroseconds(defaultsleep);
  101. pullUpDnControl (PIFACE + IN, PUD_UP) ;
  102. ReadUntilTimeout(PIFACE + IN, HIGH, 2) ;
  103. pullUpDnControl (PIFACE + IN, PUD_DOWN) ;
  104. // cool down very slowly, connect 680 kOhm pull down resistor to make ist faster
  105. ReadUntilTimeout(PIFACE + IN, LOW, 60) ;
  106. pullUpDnControl (PIFACE + IN, PUD_UP) ; // finally up
  107. ReadUntilTimeout(PIFACE + IN, HIGH, 2) ;
  108. }
  109. return UnitTestState();
  110. }