Advertisement
grist

Arduino I2C Wire library test

Feb 1st, 2013
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. /*
  2.  
  3.  
  4. Test harness for playing with I2C.
  5.  
  6.  
  7.  
  8. */
  9.  
  10. #include <Wire.h>
  11.  
  12. #define SLAVE_ADDR         0x01  // This has to match the address assigned to the slave in its code
  13. #define MAX_PACKET_SIZE    16    // max bytes to test sending at a time
  14. #define TEST_TRIES         10    // how many tests to perform for each number of bytes
  15.  
  16. // Holds the incoming data
  17. byte data[MAX_PACKET_SIZE];
  18.  
  19. // function prototypes
  20. int fillData(int bytes);
  21. void sendData(int bytes);
  22.  
  23. void setup()
  24. {
  25.     Wire.begin();
  26.     Serial.begin(9600);
  27. }
  28.  
  29. void loop()
  30. {
  31.     int total;
  32.     int bytes = 12;
  33.     int retries = 0;
  34.     unsigned long start_time, trans_time;
  35.     for(;;) {
  36.         Serial.println("-----------------");
  37.         Serial.println(retries++);
  38.         Serial.println("-----------------");
  39.         for (bytes=1;bytes<MAX_PACKET_SIZE;bytes++){
  40.             total = fillData(bytes);
  41.             start_time = micros();
  42.             sendData(bytes);
  43.             trans_time = micros() - start_time;
  44.             Serial.print("Total: ");
  45.             Serial.println(total);
  46.             Serial.print("Time: ");
  47.             Serial.println(trans_time);
  48.             delay(7000);
  49.         }
  50.     }
  51.  
  52. }
  53.  
  54.  
  55. int fillData(int bytes)
  56. { // generate some random bytes.
  57.     int total = 0;
  58.     for (int i=0;i<bytes;i++) {
  59.         data[i] = random(256);
  60.         total += data[i];
  61.     }
  62.     return total;
  63. }
  64.  
  65. void sendData(int bytes)
  66. { // send the bytes
  67.    int i, bytes_sent=0;
  68.    Wire.beginTransmission(SLAVE_ADDR);
  69.    for (i=0;i<bytes;i++) {
  70.        Wire.write(data[i]);
  71.        bytes_sent++;
  72.    }
  73.    Wire.endTransmission();
  74.    Serial.print("Bytes: ");
  75.    Serial.println(bytes_sent);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement