Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Test harness for playing with I2C.
- */
- #include <Wire.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
- // Holds the incoming data
- byte data[MAX_PACKET_SIZE];
- // function prototypes
- int fillData(int bytes);
- void sendData(int bytes);
- void setup()
- {
- Wire.begin();
- Serial.begin(9600);
- }
- void loop()
- {
- int total;
- int bytes = 12;
- int retries = 0;
- unsigned long start_time, trans_time;
- for(;;) {
- Serial.println("-----------------");
- Serial.println(retries++);
- Serial.println("-----------------");
- for (bytes=1;bytes<MAX_PACKET_SIZE;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
- int i, bytes_sent=0;
- Wire.beginTransmission(SLAVE_ADDR);
- for (i=0;i<bytes;i++) {
- Wire.write(data[i]);
- bytes_sent++;
- }
- Wire.endTransmission();
- Serial.print("Bytes: ");
- Serial.println(bytes_sent);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement