25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

131 lines
3.8 KiB

  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. }