Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Test harness for playing with I2C.
- */
- #include <I2C.h>
- #define SLAVE_ADDR 0x01 // This has to match the address assigned to the slave in its code
- #define MAX_PACKET_SIZE 16 // max bytes to test sending at a time
- #define TEST_TRIES 10 // how many tests to perform for each number of bytes
- #define END_MARKER 255
- // Holds the outgoing data
- byte data[MAX_PACKET_SIZE];
- // function prototypes
- int fillData(int bytes);
- void setup()
- {
- I2c.begin();
- I2c.setSpeed(1); // 0 = 100MHz, 1 = 400MHz
- Serial.begin(9600);
- }
- void loop()
- {
- int total;
- int retries = 10;
- unsigned long start_time, trans_time;
- for(int i=0;i<retries;i++) {
- Serial.println("-----------------");
- Serial.println(retries++);
- Serial.println("-----------------");
- for (int bytes=1;bytes<MAX_PACKET_SIZE-1;bytes++) {
- total = fillData(bytes);
- start_time = micros();
- sendData(bytes);
- trans_time = micros() - start_time;
- Serial.print("Total: ");
- Serial.println(total);
- Serial.print("Time: ");
- Serial.println(trans_time);
- delay(7000);
- }
- }
- }
- int fillData(int bytes)
- { // generate some random bytes.
- int total = 0;
- for (int i=0;i<bytes;i++) {
- data[i] = random(256);
- total += data[i];
- }
- return total;
- }
- void sendData(int bytes)
- { // send the bytes. Hacking a 0 as the first byte and forcing the
- // END_MARKER in. No error checking seeing we're interested in speed.
- data[bytes] = END_MARKER;
- I2c.write((int)SLAVE_ADDR, 0, data, bytes +1);
- Serial.print("Bytes: ");
- Serial.println(bytes + 2); // Includes the start byte and end marker
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement