Having spent the better part of the day writing I2C routines for the PIC16F887 from scratch (because the supplied Hi-Tech libraries didn’t seem to work), I figured I might as well package them up into a library for future use, available here. (These were written for a PIC16F887 using Hi-Tech C, but they should be fairly easy to port to any other platform with a standard C compiler.)
Usage is fairly straightforward:
I2C_ReadRegister(char deviceAddress, char registerAddress);
I2C_WriteRegister(char deviceAddress, char registerAddress, char Data);
The routines are simple and straightforward: single-master-only and single-byte-transaction-only. I may get ambitious enough to update them later on…
I consider these routines to be either late alpha or early beta at this point (meaning that they seem quite reliable so far, and most of the major bugs appear to have been squashed.) Specifically, they have been tested with Sparkfun’s MMA8452Q Triple-Axis Accelerometer. If you notice any bugs or have suggestions for improvements, I’d appreciate a heads-up.
“Share and enjoy!”*