Friday, November 9, 2012

Doing some Timing Tests

Today I mocked up a simple Arduino 'sketch' with the goal of seeing how the One Wire and the I2C libraries would work with each other, and also to do some timing tests.  It consists of a simple loop, ran 100 times for each 'device'.  For the I2C interface I 'print'  "Hello World" and check to see if there is any key pressed on the remote LCD display.  For the One_wire I ask for the temperature from one device.  And for fun I added a simple printing of 'Hello World' via the serial port.

Now, I do not actually have any One Wire devices so I suspect part of the long OneWire timing is that it times-out while awaiting a reply from the DS18B20 temperature sensors (that is not there).  But that is OK, cause this gives me a feeling for how long a fault condition will take.

And speaking of Fault Conditions, I have seen some concern over the default I2C lib; that it might be possible to hang under some conditions.   There is an improved lib available I am interested in using but it is not compatible with the LCD driver and would make 'printing' to it more code intensive.  But, to be honest I am looking to use it anyway.

So, here are the results (remember, each item is 100x cycles):

  • I2C          =      338ms
  • One Wire = 12,433ms
  • RS-232    =   2,129ms


Clearly the One Wire is a TIME PIG.  But then I expected that.  And again, this is a fault time-out situation.  Will be interesting to see what the times are when there is an actual OneWire device!

Till then, it confirms my intention to sample the OneWire device in rotation during the main-loop (as opposed to every one each loop through).  I do not expect temperature to change that fast, and by distributing the times out - even a 124mS/device would not be that bad.





Code following:
=======================================================================

/*
  Testing timing needed for Serial output

     RS-232, I2C, and one-wire using common libs.
   
   
 */

// include the library code:
#include <LCDI2Cw.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>



#define LOOP_COUNT    100        // Do 100 examples of each output and time the total time needed.

long  I2C_TIME;
long  ONE_WIRE_TIME;
long  SERIAL_TIME;


unsigned char i2cAddress = 0x4C;  // LCD module I2C address

// initialize the library with the number of columns and rows
LCDI2Cw lcd(20, 4, i2cAddress);


// Data wire is plugged into port 10 on the Arduino (pin-16 on Amtel CPU)
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);



void setup() {
  // Set up the Output ports.
  lcd.begin();
  Serial.begin(9600);
 
  Serial.println("Dallas Temperature IC Control Library Demo");
 // Start up the library
  sensors.begin();

}

void loop() {

  // Do the I2C 1st

  I2C_TIME = millis();
  for (int i=0; i <= LOOP_COUNT; i++) {
      lcd.print("  hello, world!  ");  // Print a message to the LCD.
      lcd.println(i);
      lcd.keypad();                    // Just check if there is any LCD key pressed.

  }
  I2C_TIME = millis() - I2C_TIME;


  // Now do RS-232 output

  SERIAL_TIME = millis();
  for (int i=0; i <= LOOP_COUNT; i++) {
      Serial.print("  hello, world!  ");  // Print a message via the RS-232.
      Serial.println (i);
      Serial.available();                 // Just check to see if a serial key is available.
  }
   SERIAL_TIME = millis() - SERIAL_TIME;
 

  // And now One Wire!
  ONE_WIRE_TIME = millis();
  for (int i=0; i <= LOOP_COUNT; i++) {
       sensors.requestTemperatures(); // Send the command to get temperatures
  sensors.getTempCByIndex(0); // And get it!
  }
  ONE_WIRE_TIME = millis() - ONE_WIRE_TIME;


 
  // set the cursor to line 1, column 0
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  // print the number of seconds since reset:
  lcd.print("I2C Time = ");
  lcd.println(I2C_TIME);
  lcd.print("One Wire Time = ");
  lcd.println(ONE_WIRE_TIME);
  lcd.print("Serial Time = ");
  lcd.println(SERIAL_TIME);

  Serial.print("I2C Time = ");
  Serial.println(I2C_TIME);
  Serial.print("One Wire Time = ");
  Serial.println(ONE_WIRE_TIME);
  Serial.print("Serial Time = ");
  Serial.println(SERIAL_TIME);
 


}

No comments:

Post a Comment