I2C Master for SDCC Range Slave Transaction

I’ve been integrating the first SDCC implementation of an I2C Master module with my current EE project and the results are positive. While this is but a small piece of a much larger puzzle, I’m pleased that I’ve been able to hit the ground running with this compiler. The C implementation is much shorter that the previous assembly language program… and easier to read. Below is the majority of the transaction with the master first sending a “stop ranging” command to the slave, followed by a write requesting the most recent range information. The last half of the transaction is the master read of two bytes of range data in MSB format.




The corrisponding C code is just this simple:

//Enable range sensor and wait
i2cMasterStart(5);
buffer[0] = EnableSensor;
result = i2cMasterWrite(RangeSensorA, (char*)&buffer, 1, 10);
i2cMasterStop(5);

//Measure range for 0.5 seconds at 8MHz
delay1mtcy(1);

if(result == _I2C_OK) {
   //Disable range sensor
   buffer[0] = DisableSensor;
   i2cMasterStart(5);
   result = i2cMasterWrite(RangeSensorA, (char*)&buffer, 1, 10);
   i2cMasterStop(5);

   if(result == _I2C_OK) {
      //Get range
      i2cMasterStart(5);
      buffer[0] = GetLatestRange;
      result = i2cMasterWrite(RangeSensorA, (char*)&buffer, 1, 10);
      if(result == _I2C_OK) {
         i2cMasterRepeatedStart(10);
         result = i2cMasterRead(RangeSensorA, (char*)&buffer, 2, 20);
      } //write result ok
      i2cMasterStop(5);
   }
}

delay1ktcy(100);

 

Leave a Reply

Your email address will not be published. Required fields are marked *

© Zillow, Inc., 2006-2016. Use is subject to Terms of Use
What's a Zestimate?