I˛C is a bi-directional two-wire (data and clock) serial bus that provides a communication link between integrated circuits. Examples of simple I˛C-compatible devices found in embedded systems include EEPROMs, thermal sensors, and real-time clocks.
ZDev provides I˛C master driver APIs to access I˛C devices. The I˛C
functions are declared in i2c.h
. The following example measures the
distance using SRF02, an I˛C based ultra sonic range finder.
Distance measurement with SRF02.
#include <board.h>
#include <delay.h>
#include <i2c.h>
int main()
{
int distance;
/* The 7-bit I2C address from datasheet. */
uint8_t addr = 0x78;
uint8_t buf[2];
board_init();
board_stdout(LCD_STDOUT);
delay_init();
lcd_init();
i2c_init();
while(1) {
/*
* Write 0x51 to register 0x0.
* Starts real ranging in cm.
*/
buf[0] = 0x0;
buf[1] = 0x51;
i2c_tx(addr, buf, 2);
mdelay(66);
/*
* Read 0x2 and 0x3 to get the
* distance hi-byte and low-byte.
*/
buf[0] = 0x2;
i2c_tx(addr, buf, 1);
i2c_rx(addr, buf, 2);
distance = ((buf[0] << 8) + buf[1]);
printf("Range = %dcm\n", distance);
mdelay(1000);
}
}