Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

wiringpi_i2c_test1_pcf8574.c 2.9 KiB

5 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // WiringPi test program: I2C functions (need PCF8574)
  2. // Compile: gcc -Wall wiringpi_i2c_test1.c -o wiringpi_i2c_test1 -lwiringPi
  3. #include "wpi_test.h"
  4. #include "pcf8574.h"
  5. #include "wiringPiI2C.h"
  6. const int pinBase = 1020;
  7. const int i2cAdress = 0x20;
  8. int ShowAll() {
  9. int in;
  10. int value = 0;
  11. printf("pin: 0 1 2 3 4 5 6 7\nval: ");
  12. for (int pin=0; pin<=7; ++pin) {
  13. in = digitalRead(pinBase + pin);
  14. printf("%d ", in);
  15. if(in==HIGH) { value |= (0x01<<pin); }
  16. }
  17. printf(" = 0x%02X\n", value);
  18. return value;
  19. }
  20. void testPin(const char* msg, int fd, int pin , int value) {
  21. printf("%s:\n", msg);
  22. int in = digitalRead(pinBase + pin);
  23. CheckSame("digitalRead", in, value);
  24. int expect = HIGH==value ? (0x1<<pin) : 0;
  25. int pinmask = 0x01<<pin;
  26. int i2cread = wiringPiI2CRead(fd);
  27. CheckSame("wiringPiI2CRead", i2cread & pinmask, expect);
  28. //printf("Value = 0x%X\n",i2cread);
  29. uint8_t i2cvalue = HIGH==value ? 0x00 : 0xFF;
  30. int result = wiringPiI2CRawRead(fd, &i2cvalue, 1);
  31. CheckSame("wiringPiI2CRawRead result", result, 1);
  32. CheckSame("wiringPiI2CRawRead", i2cvalue & pinmask, expect);
  33. //printf("Value = 0x%X\n",i2cvalue);
  34. }
  35. int main (void) {
  36. int major, minor;
  37. wiringPiVersion(&major, &minor);
  38. printf("Testing I2C functions with PCF8574 (WiringPi %d.%d)\n",major, minor);
  39. printf("-------------------------------------------------\n\n");
  40. int ret = pcf8574Setup (pinBase, i2cAdress);
  41. if (ret!=1) {
  42. FailAndExitWithErrno("pcf8574Setup", ret);
  43. }
  44. int fd = wiringPiI2CSetup (i2cAdress);
  45. if (fd<=0) {
  46. FailAndExitWithErrno("wiringPiI2CSetup", fd);
  47. }
  48. CheckSame("I2C fd", fd, 4);
  49. ShowAll();
  50. int pin = 3;
  51. testPin("Test pin 3 high", fd, pin , HIGH);
  52. testPin("Test pin 4 high", fd, pin+1, HIGH);
  53. digitalWrite(pinBase + pin, LOW);
  54. testPin("Test pin 3 low", fd, pin , LOW);
  55. testPin("Test pin 4 high", fd, pin+1, HIGH);
  56. ShowAll();
  57. digitalWrite(pinBase + pin, HIGH);
  58. testPin("Test pin 3 high", fd, pin , HIGH);
  59. testPin("Test pin 4 high", fd, pin+1, HIGH);
  60. ShowAll();
  61. printf("\nwiringPiI2CReadReg8:\n");
  62. int i2cin, expect;
  63. i2cin = wiringPiI2CReadReg8(fd, 0x00);
  64. expect = ShowAll();
  65. CheckSame("all low wiringPiI2CReadReg8", i2cin, expect);
  66. i2cin = wiringPiI2CReadReg8(fd, 0xFF);
  67. expect =ShowAll();
  68. CheckSame("all high wiringPiI2CReadReg8", i2cin, expect);
  69. printf("\nwiringPiI2CReadBlockData:\n");
  70. uint8_t value;
  71. int result;
  72. value = 0xFF;
  73. result = wiringPiI2CReadBlockData(fd, 0x00, &value, 1);
  74. CheckSame("wiringPiI2CReadBlockData result", result, 1);
  75. expect = ShowAll();
  76. CheckSame("all high wiringPiI2CReadBlockData", value, expect);
  77. printf("\n");
  78. value = 0x00;
  79. result = wiringPiI2CReadBlockData(fd, 0xFF, &value, 1);
  80. CheckSame("wiringPiI2CReadBlockData result", result, 1);
  81. expect = ShowAll();
  82. CheckSame("all low wiringPiI2CReadBlockData", value, expect);
  83. return UnitTestState();
  84. }